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

Kwan (Ian Moody) phabricator at mercurial-scm.org
Thu Mar 14 11:18:33 EDT 2019


Kwan added a comment.


  In https://phab.mercurial-scm.org/D6113#88992, @yuja wrote:
  
  > >   -    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.
  
  
  Ah, yeah you're right, it only started accepting bytes in 3.6 <https://docs.python.org/3/library/json.html#json.loads>.
  
  In https://phab.mercurial-scm.org/D6113#88992, @yuja wrote:
  
  > >   +    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,  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}}

REPOSITORY
  rHG Mercurial

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

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


More information about the Mercurial-devel mailing list