changeset 30752:ffd324eaf994

parser: make buildargsdict() precompute position where keyword args start This prepares for adding *varargs support. See the next patch.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 09 Jan 2017 15:15:21 +0900
parents e882c7bb5a0b
children c3a3896a9fa8
files mercurial/parser.py
diffstat 1 files changed, 5 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/parser.py	Wed Jan 11 07:40:52 2017 +0800
+++ b/mercurial/parser.py	Mon Jan 09 15:15:21 2017 +0900
@@ -96,17 +96,18 @@
     Invalid keywords or too many positional arguments are rejected, but
     missing arguments are just omitted.
     """
+    kwstart = next((i for i, x in enumerate(trees) if x[0] == keyvaluenode),
+                   len(trees))
     if len(trees) > len(keys):
         raise error.ParseError(_("%(func)s takes at most %(nargs)d arguments")
                                % {'func': funcname, 'nargs': len(keys)})
     args = {}
     # consume positional arguments
-    for k, x in zip(keys, trees):
-        if x[0] == keyvaluenode:
-            break
+    for k, x in zip(keys, trees[:kwstart]):
         args[k] = x
+    assert len(args) == kwstart
     # remainder should be keyword arguments
-    for x in trees[len(args):]:
+    for x in trees[kwstart:]:
         if x[0] != keyvaluenode or x[1][0] != keynode:
             raise error.ParseError(_("%(func)s got an invalid argument")
                                    % {'func': funcname})