Details
- Reviewers
- None
- Group Reviewers
hg-reviewers - Commits
- rHG99e00e5c4746: py3: convert to/from bytes/unicode for json.(dump|load)s in debugcallconduit
Diff Detail
- Repository
- rHG Mercurial
- Lint
Automatic diff as part of commit; lint not applicable. - Unit
Automatic diff as part of commit; unit tests not applicable.
Event Timeline
- 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.(load|dump)s only returns/accepts unicode strings
+ params = pycompat.rapply(lambda x:
+ encoding.unitolocal(x) if isinstance(x, pycompat.unicode) else x,
+ json.loads(ui.fin.read())
+ )
Perhaps, the input data has to be converted to unicode. IIRC old Python 3
versions don't accept bytes.
+ 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))
Maybe we can use templatefilters.json() to dump internal (i.e. no unicode)
dict.
Ah, yeah you're right, it only started accepting bytes in 3.6.
Ah, we can indeed (thanks for the pointer), with one caveat: the formatting.
The existing output is nicely formatted for human readability, with each entry on a new line and indented appropriately. templatefilters.json() isn't capable of that.
Do we mind losing that? It's also tested for in test-phabricator.t. Compare before:
{ "cursor": { "after": null, "before": null, "limit": 100, "order": null }, "data": [], "maps": {}, "query": { "queryKey": null } }
After:
{"cursor": {"after": null, "before": null, "limit": 100, "order": null}, "data": [], "maps": {}, "query": {"queryKey": null}}
Ah, we can indeed (thanks for the pointer), with one caveat: the formatting. The existing output is nicely formatted for human readability, with each entry on a new line and indented appropriately. `templatefilters.json()` isn't capable of that. Do we mind losing that? It's also tested for in test-phabricator.t. Compare before:
Good point. Let's not change the formatting as it is a debug command and
we'll probably want to read the output.
Make compatible with Py3.5, separate comments about json functions so they are more readable.