[PATCH 2 of 6 json-style] templatefilters.json: stabilize output

Gregory Szorc gregory.szorc at gmail.com
Wed Dec 31 16:45:32 CST 2014


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1420062535 28800
#      Wed Dec 31 13:48:55 2014 -0800
# Node ID 94f5d1af421d623685e7c88da54872a2dedef21f
# Parent  18172660090c5794d147cdd95ee4e8c42adeb954
templatefilters.json: stabilize output

The json filter was previously iterating over keys in an object in an
undefined order. Let's throw a sorted() in there so output is
consistent.

It's somewhat frightening that there are no tests for the json filter.
Subsequent commits will add them, so we pass on the opportunity to add
them here.

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -198,9 +198,9 @@ def json(obj):
     elif isinstance(obj, unicode):
         return '"%s"' % jsonescape(obj)
     elif util.safehasattr(obj, 'keys'):
         out = []
-        for k, v in obj.iteritems():
+        for k, v in sorted(obj.iteritems()):
             s = '%s: %s' % (json(k), json(v))
             out.append(s)
         return '{' + ', '.join(out) + '}'
     elif util.safehasattr(obj, '__iter__'):


More information about the Mercurial-devel mailing list