diff options
author | Guido van Rossum <guido@python.org> | 1997-01-06 22:56:52 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-01-06 22:56:52 (GMT) |
commit | 635abd24f05642dad5f96549fbbd01113b96b6b3 (patch) | |
tree | 36235dc200b6c8f1c0a25609108004424b1a1062 /Python | |
parent | 8f49e12a0e0805ec61690263ef1ef15340293980 (diff) | |
download | cpython-635abd24f05642dad5f96549fbbd01113b96b6b3.zip cpython-635abd24f05642dad5f96549fbbd01113b96b6b3.tar.gz cpython-635abd24f05642dad5f96549fbbd01113b96b6b3.tar.bz2 |
Check for duplicate keyword arguments at compile time.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/compile.c | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/Python/compile.c b/Python/compile.c index adb0d09..5429e80 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -282,6 +282,7 @@ com_error(c, exc, msg) object *v; char buffer[30]; char *s; + c->c_errors++; if (c->c_lineno <= 1) { /* Unknown line number or single interactive command */ err_setstr(exc, msg); @@ -296,7 +297,6 @@ com_error(c, exc, msg) strcat(s, buffer); err_setval(exc, v); DECREF(v); - c->c_errors++; } @@ -913,23 +913,23 @@ com_slice(c, n, op) } } -static int -com_argument(c, n, inkeywords) +static void +com_argument(c, n, pkeywords) struct compiling *c; node *n; /* argument */ - int inkeywords; + object **pkeywords; { node *m; REQ(n, argument); /* [test '='] test; really [ keyword '='] keyword */ if (NCH(n) == 1) { - if (inkeywords) { + if (*pkeywords != NULL) { com_error(c, SyntaxError, "non-keyword arg after keyword arg"); } else { com_node(c, CHILD(n, 0)); } - return 0; + return; } m = n; do { @@ -940,15 +940,22 @@ com_argument(c, n, inkeywords) } else { object *v = newstringobject(STR(m)); - if (v == NULL) + if (v != NULL && *pkeywords == NULL) + *pkeywords = newdictobject(); + if (v == NULL || *pkeywords == NULL) c->c_errors++; else { + if (dict2lookup(*pkeywords, v) != NULL) + com_error(c, SyntaxError, + "duplicate keyword argument"); + else + if (dict2insert(*pkeywords, v, v) != 0) + c->c_errors++; com_addoparg(c, LOAD_CONST, com_addconst(c, v)); DECREF(v); } } com_node(c, CHILD(n, 2)); - return 1; } static void @@ -960,18 +967,19 @@ com_call_function(c, n) com_addoparg(c, CALL_FUNCTION, 0); } else { - int inkeywords, i, na, nk; + object *keywords = NULL; + int i, na, nk; REQ(n, arglist); - inkeywords = 0; na = 0; nk = 0; for (i = 0; i < NCH(n); i += 2) { - inkeywords = com_argument(c, CHILD(n, i), inkeywords); - if (!inkeywords) + com_argument(c, CHILD(n, i), &keywords); + if (keywords == NULL) na++; else nk++; } + XDECREF(keywords); if (na > 255 || nk > 255) { com_error(c, SyntaxError, "more than 255 arguments"); } |