[PATCH 3 of 9] parser: preserve order of keyword arguments

Yuya Nishihara yuya at tcha.org
Wed Apr 12 11:53:19 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1491706707 -32400
#      Sun Apr 09 11:58:27 2017 +0900
# Node ID a773ef212277598db714833be5c21297021a26ab
# Parent  46f15fc2b294842ae24c51f8b9eedcbf6852d920
parser: preserve order of keyword arguments

This helps building dict(key1=value1, ...) in deterministic way.

diff --git a/mercurial/parser.py b/mercurial/parser.py
--- a/mercurial/parser.py
+++ b/mercurial/parser.py
@@ -154,7 +154,7 @@ def buildargsdict(trees, funcname, argsp
                                  "arguments")
                                % {'func': funcname,
                                   'nargs': len(poskeys) + len(keys)})
-    args = {}
+    args = util.sortdict()
     # consume positional arguments
     for k, x in zip(poskeys, trees[:kwstart]):
         args[k] = x
@@ -165,7 +165,7 @@ def buildargsdict(trees, funcname, argsp
             args[k] = x
     # remainder should be keyword arguments
     if optkey:
-        args[optkey] = {}
+        args[optkey] = util.sortdict()
     for x in trees[kwstart:]:
         if x[0] != keyvaluenode or x[1][0] != keynode:
             raise error.ParseError(_("%(func)s got an invalid argument")
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -474,15 +474,15 @@ def _buildfuncargs(exp, context, curmeth
     ...     x = _parseexpr(expr)
     ...     n = getsymbol(x[1])
     ...     return _buildfuncargs(x[2], context, exprmethods, n, argspec)
-    >>> sorted(fargs('a(l=1, k=2)', 'k l m').keys())
-    ['k', 'l']
+    >>> fargs('a(l=1, k=2)', 'k l m').keys()
+    ['l', 'k']
     >>> args = fargs('a(opts=1, k=2)', '**opts')
-    >>> args.keys(), sorted(args['opts'].keys())
-    (['opts'], ['k', 'opts'])
+    >>> args.keys(), args['opts'].keys()
+    (['opts'], ['opts', 'k'])
     """
     def compiledict(xs):
-        return dict((k, compileexp(x, context, curmethods))
-                    for k, x in xs.iteritems())
+        return util.sortdict((k, compileexp(x, context, curmethods))
+                             for k, x in xs.iteritems())
     def compilelist(xs):
         return [compileexp(x, context, curmethods) for x in xs]
 
@@ -494,7 +494,7 @@ def _buildfuncargs(exp, context, curmeth
     _poskeys, varkey, _keys, optkey = argspec = parser.splitargspec(argspec)
     treeargs = parser.buildargsdict(getlist(exp), funcname, argspec,
                                     keyvaluenode='keyvalue', keynode='symbol')
-    compargs = {}
+    compargs = util.sortdict()
     if varkey:
         compargs[varkey] = compilelist(treeargs.pop(varkey))
     if optkey:


More information about the Mercurial-devel mailing list