changeset 41976:99e00e5c4746

py3: convert to/from bytes/unicode for json.(dump|load)s in debugcallconduit Differential Revision: https://phab.mercurial-scm.org/D6113
author Ian Moody <moz-ian@perix.co.uk>
date Sat, 09 Mar 2019 02:18:49 +0000
parents 51ba9fbcca52
children 7c86caee3323
files hgext/phabricator.py
diffstat 1 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/phabricator.py	Fri Mar 08 18:30:12 2019 +0000
+++ b/hgext/phabricator.py	Sat Mar 09 02:18:49 2019 +0000
@@ -240,10 +240,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"""