Patchwork [2,of,2,STABLE] formatter: handle lists in json output

login
register
mail settings
Submitter timeless
Date April 21, 2016, 6:09 a.m.
Message ID <ea574ef953fba7f9c1d6.1461218947@gcc2-power8.osuosl.org>
Download mbox | patch
Permalink /patch/14755/
State Deferred
Headers show

Comments

timeless - April 21, 2016, 6:09 a.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1461211019 0
#      Thu Apr 21 03:56:59 2016 +0000
# Branch stable
# Node ID ea574ef953fba7f9c1d6a434267dc10b17972549
# Parent  24fa6b119f160b3adf25c1018bb322ca4db9b948
# Available At bb://timeless/mercurial-crew
#              hg pull bb://timeless/mercurial-crew -r ea574ef953fb
formatter: handle lists in json output

evolve crashes sending lists to _jsonifyobj because encoding.jsonescape
does not expect lists...
Yuya Nishihara - April 21, 2016, 2:28 p.m.
On Thu, 21 Apr 2016 06:09:07 +0000, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1461211019 0
> #      Thu Apr 21 03:56:59 2016 +0000
> # Branch stable
> # Node ID ea574ef953fba7f9c1d6a434267dc10b17972549
> # Parent  24fa6b119f160b3adf25c1018bb322ca4db9b948
> # Available At bb://timeless/mercurial-crew
> #              hg pull bb://timeless/mercurial-crew -r ea574ef953fb
> formatter: handle lists in json output

(issue5217)

> evolve crashes sending lists to _jsonifyobj because encoding.jsonescape
> does not expect lists...

I think that's why "evolve --list" doesn't support the formatter option yet.

These patches might be okay, but I'll have to struggle with the templater
stuff to get confident. Since this problem shouldn't be visible, I'd rather
revisit it after 3.8.

And we'll need tests.

Patch

diff -r 24fa6b119f16 -r ea574ef953fb mercurial/formatter.py
--- a/mercurial/formatter.py	Thu Apr 21 03:53:41 2016 +0000
+++ b/mercurial/formatter.py	Thu Apr 21 03:56:59 2016 +0000
@@ -110,9 +110,7 @@ 
         self._ui.write(cPickle.dumps(self._data))
 
 def _jsonifyobj(v):
-    if isinstance(v, tuple):
-        return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']'
-    elif v is None:
+    if v is None:
         return 'null'
     elif v is True:
         return 'true'
@@ -120,11 +118,18 @@ 
         return 'false'
     elif isinstance(v, (int, float)):
         return str(v)
+    elif isinstance(v, str):
+        return '"%s"' % encoding.jsonescape(v)
     elif isinstance(v, dict):
         return ('{' + ', '.join('%s: %s' %
             (_jsonifyobj(k), _jsonifyobj(v[k])) for k in v) +
                 '}')
     else:
+        try:
+            i = iter(v)
+            return '[' + ', '.join(_jsonifyobj(e) for e in i) + ']'
+        except TypeError:
+            pass
         return '"%s"' % encoding.jsonescape(v)
 
 class jsonformatter(baseformatter):