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

login
register
mail settings
Submitter Gregory Szorc
Date Dec. 31, 2014, 10:45 p.m.
Message ID <94f5d1af421d623685e7.1420065932@gps-mbp.local>
Download mbox | patch
Permalink /patch/7287/
State Accepted
Commit a9f826c3eaf9dc9743571fd40d3f32b761b0132f
Headers show

Comments

Gregory Szorc - Dec. 31, 2014, 10:45 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@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.
Matt Mackall - Jan. 1, 2015, 11:27 p.m.
On Wed, 2014-12-31 at 14:45 -0800, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1420062535 28800
> #      Wed Dec 31 13:48:55 2014 -0800
> # Node ID 94f5d1af421d623685e7c88da54872a2dedef21f
> # Parent  18172660090c5794d147cdd95ee4e8c42adeb954
> templatefilters.json: stabilize output

I've queued these first two patches for default, thanks.

Patch

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__'):