Patchwork [1,of,3] parser: make buildargsdict() precompute position where keyword args start

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 9, 2017, 2:38 p.m.
Message ID <95725b2c0e04033fbe0a.1483972693@mimosa>
Download mbox | patch
Permalink /patch/18142/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 9, 2017, 2:38 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1483942521 -32400
#      Mon Jan 09 15:15:21 2017 +0900
# Node ID 95725b2c0e04033fbe0a54e1884fd347fdff18b2
# Parent  c2bd2f77965b7d23d05575596e4b30c04d5d5adf
parser: make buildargsdict() precompute position where keyword args start

This prepares for adding *varargs support. See the next patch.
Augie Fackler - Jan. 12, 2017, 3:58 a.m.
On Mon, Jan 09, 2017 at 11:38:13PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1483942521 -32400
> #      Mon Jan 09 15:15:21 2017 +0900
> # Node ID 95725b2c0e04033fbe0a54e1884fd347fdff18b2
> # Parent  c2bd2f77965b7d23d05575596e4b30c04d5d5adf
> parser: make buildargsdict() precompute position where keyword args start

These are queued as ffd324eaf994 and children. Thanks!

>
> This prepares for adding *varargs support. See the next patch.
>
> diff --git a/mercurial/parser.py b/mercurial/parser.py
> --- a/mercurial/parser.py
> +++ b/mercurial/parser.py
> @@ -96,17 +96,18 @@ def buildargsdict(trees, funcname, keys,
>      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})
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/parser.py b/mercurial/parser.py
--- a/mercurial/parser.py
+++ b/mercurial/parser.py
@@ -96,17 +96,18 @@  def buildargsdict(trees, funcname, keys,
     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})