mirror of
https://github.com/python/cpython.git
synced 2025-10-17 04:08:28 +00:00
Check for duplicate keyword arguments at compile time.
This commit is contained in:
parent
8f49e12a0e
commit
635abd24f0
1 changed files with 20 additions and 12 deletions
|
@ -282,6 +282,7 @@ com_error(c, exc, msg)
|
||||||
object *v;
|
object *v;
|
||||||
char buffer[30];
|
char buffer[30];
|
||||||
char *s;
|
char *s;
|
||||||
|
c->c_errors++;
|
||||||
if (c->c_lineno <= 1) {
|
if (c->c_lineno <= 1) {
|
||||||
/* Unknown line number or single interactive command */
|
/* Unknown line number or single interactive command */
|
||||||
err_setstr(exc, msg);
|
err_setstr(exc, msg);
|
||||||
|
@ -296,7 +297,6 @@ com_error(c, exc, msg)
|
||||||
strcat(s, buffer);
|
strcat(s, buffer);
|
||||||
err_setval(exc, v);
|
err_setval(exc, v);
|
||||||
DECREF(v);
|
DECREF(v);
|
||||||
c->c_errors++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -913,23 +913,23 @@ com_slice(c, n, op)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static void
|
||||||
com_argument(c, n, inkeywords)
|
com_argument(c, n, pkeywords)
|
||||||
struct compiling *c;
|
struct compiling *c;
|
||||||
node *n; /* argument */
|
node *n; /* argument */
|
||||||
int inkeywords;
|
object **pkeywords;
|
||||||
{
|
{
|
||||||
node *m;
|
node *m;
|
||||||
REQ(n, argument); /* [test '='] test; really [ keyword '='] keyword */
|
REQ(n, argument); /* [test '='] test; really [ keyword '='] keyword */
|
||||||
if (NCH(n) == 1) {
|
if (NCH(n) == 1) {
|
||||||
if (inkeywords) {
|
if (*pkeywords != NULL) {
|
||||||
com_error(c, SyntaxError,
|
com_error(c, SyntaxError,
|
||||||
"non-keyword arg after keyword arg");
|
"non-keyword arg after keyword arg");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
com_node(c, CHILD(n, 0));
|
com_node(c, CHILD(n, 0));
|
||||||
}
|
}
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
m = n;
|
m = n;
|
||||||
do {
|
do {
|
||||||
|
@ -940,15 +940,22 @@ com_argument(c, n, inkeywords)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
object *v = newstringobject(STR(m));
|
object *v = newstringobject(STR(m));
|
||||||
if (v == NULL)
|
if (v != NULL && *pkeywords == NULL)
|
||||||
|
*pkeywords = newdictobject();
|
||||||
|
if (v == NULL || *pkeywords == NULL)
|
||||||
c->c_errors++;
|
c->c_errors++;
|
||||||
else {
|
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));
|
com_addoparg(c, LOAD_CONST, com_addconst(c, v));
|
||||||
DECREF(v);
|
DECREF(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
com_node(c, CHILD(n, 2));
|
com_node(c, CHILD(n, 2));
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -960,18 +967,19 @@ com_call_function(c, n)
|
||||||
com_addoparg(c, CALL_FUNCTION, 0);
|
com_addoparg(c, CALL_FUNCTION, 0);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int inkeywords, i, na, nk;
|
object *keywords = NULL;
|
||||||
|
int i, na, nk;
|
||||||
REQ(n, arglist);
|
REQ(n, arglist);
|
||||||
inkeywords = 0;
|
|
||||||
na = 0;
|
na = 0;
|
||||||
nk = 0;
|
nk = 0;
|
||||||
for (i = 0; i < NCH(n); i += 2) {
|
for (i = 0; i < NCH(n); i += 2) {
|
||||||
inkeywords = com_argument(c, CHILD(n, i), inkeywords);
|
com_argument(c, CHILD(n, i), &keywords);
|
||||||
if (!inkeywords)
|
if (keywords == NULL)
|
||||||
na++;
|
na++;
|
||||||
else
|
else
|
||||||
nk++;
|
nk++;
|
||||||
}
|
}
|
||||||
|
XDECREF(keywords);
|
||||||
if (na > 255 || nk > 255) {
|
if (na > 255 || nk > 255) {
|
||||||
com_error(c, SyntaxError, "more than 255 arguments");
|
com_error(c, SyntaxError, "more than 255 arguments");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue