Patchwork D6113: py3: convert to/from bytes/unicode for json.(dump|load)s in debugcallconduit

login
register
mail settings
Submitter phabricator
Date March 16, 2019, 11:54 a.m.
Message ID <4412ed5b8012453e43457fabe9436e09@localhost.localdomain>
Download mbox | patch
Permalink /patch/39293/
State Not Applicable
Headers show

Comments

phabricator - March 16, 2019, 11:54 a.m.
Kwan updated this revision to Diff 14523.
Kwan added a comment.


  Make compatible with Py3.5, separate comments about json functions so they are more readable.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D6113?vs=14426&id=14523

REVISION DETAIL
  https://phab.mercurial-scm.org/D6113

AFFECTED FILES
  hgext/phabricator.py

CHANGE DETAILS




To: Kwan, #hg-reviewers
Cc: yuja, mercurial-devel

Patch

diff --git a/hgext/phabricator.py b/hgext/phabricator.py
--- a/hgext/phabricator.py
+++ b/hgext/phabricator.py
@@ -239,10 +239,20 @@ 
     Call parameters are read from stdin as a JSON blob. Result will be written
     to stdout as a JSON blob.
     """
-    params = json.loads(ui.fin.read())
-    result = callconduit(repo, name, params)
-    s = json.dumps(result, sort_keys=True, indent=2, separators=(b',', b': '))
-    ui.write(b'%s\n' % s)
+    # json.loads only accepts bytes from 3.6+
+    rawparams = encoding.unifromlocal(ui.fin.read())
+    # json.loads only returns unicode strings
+    params = pycompat.rapply(lambda x:
+        encoding.unitolocal(x) if isinstance(x, pycompat.unicode) else x,
+        json.loads(rawparams)
+    )
+    # json.dumps only accepts unicode strings
+    result = pycompat.rapply(lambda x:
+        encoding.unifromlocal(x) if isinstance(x, bytes) else x,
+        callconduit(repo, name, params)
+    )
+    s = json.dumps(result, sort_keys=True, indent=2, separators=(u',', u': '))
+    ui.write(b'%s\n' % encoding.unitolocal(s))
 
 def getrepophid(repo):
     """given callsign, return repository PHID or None"""