comparison hgext/phabricator.py @ 41971: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 289d82a070e9
comparison
equal deleted inserted replaced
41970:51ba9fbcca52 41971:99e00e5c4746
238 """call Conduit API 238 """call Conduit API
239 239
240 Call parameters are read from stdin as a JSON blob. Result will be written 240 Call parameters are read from stdin as a JSON blob. Result will be written
241 to stdout as a JSON blob. 241 to stdout as a JSON blob.
242 """ 242 """
243 params = json.loads(ui.fin.read()) 243 # json.loads only accepts bytes from 3.6+
244 result = callconduit(repo, name, params) 244 rawparams = encoding.unifromlocal(ui.fin.read())
245 s = json.dumps(result, sort_keys=True, indent=2, separators=(b',', b': ')) 245 # json.loads only returns unicode strings
246 ui.write(b'%s\n' % s) 246 params = pycompat.rapply(lambda x:
247 encoding.unitolocal(x) if isinstance(x, pycompat.unicode) else x,
248 json.loads(rawparams)
249 )
250 # json.dumps only accepts unicode strings
251 result = pycompat.rapply(lambda x:
252 encoding.unifromlocal(x) if isinstance(x, bytes) else x,
253 callconduit(repo, name, params)
254 )
255 s = json.dumps(result, sort_keys=True, indent=2, separators=(u',', u': '))
256 ui.write(b'%s\n' % encoding.unitolocal(s))
247 257
248 def getrepophid(repo): 258 def getrepophid(repo):
249 """given callsign, return repository PHID or None""" 259 """given callsign, return repository PHID or None"""
250 # developer config: phabricator.repophid 260 # developer config: phabricator.repophid
251 repophid = repo.ui.config(b'phabricator', b'repophid') 261 repophid = repo.ui.config(b'phabricator', b'repophid')