[Python-checkins] python/dist/src/Python compile.c,2.268,2.269
gvanrossum@users.sourceforge.net
gvanrossum@users.sourceforge.net
Mon, 23 Dec 2002 08:33:49 -0800
Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1:/tmp/cvs-serv23151
Modified Files:
compile.c
Log Message:
Add warning for assignment to None, True and False. This is patch
549213 by Jeremy (checking in for him since he's away and busy).
Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.268
retrieving revision 2.269
diff -C2 -d -r2.268 -r2.269
*** compile.c 18 Dec 2002 01:18:44 -0000 2.268
--- compile.c 23 Dec 2002 16:33:46 -0000 2.269
***************
*** 576,579 ****
--- 576,580 ----
static int issue_warning(const char *, const char *, int);
+ static int symtable_warn(struct symtable *, char *);
static int com_init(struct compiling *, const char *);
static void com_free(struct compiling *);
***************
*** 1007,1010 ****
--- 1008,1069 ----
}
+ static int num_literals = 3;
+ static char *literal_names[] = {"None", "True", "False"};
+ static PyObject *literals;
+
+ static int
+ init_literals(void)
+ {
+ int i;
+
+ literals = PyDict_New();
+ if (literals == NULL)
+ return -1;
+ for (i = 0; i < num_literals; i++)
+ if (PyDict_SetItemString(literals, literal_names[i],
+ Py_None) < 0)
+ return -1;
+ return 0;
+ }
+
+ static int
+ check_literal(PyObject *name, struct compiling *c, struct symtable *st)
+ {
+ /* check for literal names that will become keywords in the future */
+ if (literals == NULL && init_literals() < 0)
+ return -1;
+ if (!PyString_Check(name))
+ return -1;
+ if (PyDict_GetItem(literals, name)) {
+ char buf[1024];
+ PyOS_snprintf(buf, sizeof(buf), "'%s' may become a keyword",
+ PyString_AS_STRING(name));
+ if (c && (issue_warning(buf, c->c_filename, c->c_lineno) < 0))
+ return -1;
+ if (st && (symtable_warn(st, buf) < 0))
+ return -1;
+ }
+ return 0;
+ }
+
+ static int
+ check_literal_str(const char *name, struct compiling *c, struct symtable *st)
+ {
+ int i;
+
+ for (i = 0; i < num_literals; i++)
+ if (strcmp(literal_names[i], name) == 0) {
+ char buf[1024];
+ PyOS_snprintf(buf, sizeof(buf),
+ "'%s' may become a keyword", name);
+ if (c &&
+ issue_warning(buf, c->c_filename, c->c_lineno) < 0)
+ return -1;
+ if (st && symtable_warn(st, buf) < 0)
+ return -1;
+ }
+ return 0;
+ }
+
static void
com_addop_varname(struct compiling *c, int kind, char *name)
***************
*** 1064,1067 ****
--- 1123,1134 ----
Py_DECREF(v);
+ if (kind == VAR_STORE || kind == VAR_DELETE) {
+ if (check_literal(v, c, NULL) < 0) {
+ c->c_errors++;
+ i = 255;
+ goto done;
+ }
+ }
+
switch (kind) {
case VAR_LOAD:
***************
*** 1131,1134 ****
--- 1198,1202 ----
chars on the total length of dotted names, I just can't be
bothered right now */
+
if (TYPE(n) == STAR)
name = "*";
***************
*** 1139,1142 ****
--- 1207,1214 ----
for (i = 0; i < NCH(n); i += 2) {
char *s = STR(CHILD(n, i));
+ if (check_literal_str(s, c, NULL) < 0) {
+ name = NULL;
+ break;
+ }
if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
com_error(c, PyExc_MemoryError,
***************
*** 1154,1157 ****
--- 1226,1231 ----
REQ(n, NAME);
name = STR(n);
+ if (check_literal_str(name, c, NULL) < 0)
+ name = NULL;
}
com_addop_name(c, op, name);
***************
*** 2973,2979 ****
tup = PyTuple_New((NCH(n) - 2)/2);
for (i = 3; i < NCH(n); i += 2) {
PyTuple_SET_ITEM(tup, (i-3)/2,
! PyString_FromString(STR(
! CHILD(CHILD(n, i), 0))));
}
}
--- 3047,3057 ----
tup = PyTuple_New((NCH(n) - 2)/2);
for (i = 3; i < NCH(n); i += 2) {
+ char *s = STR(CHILD(CHILD(n, i), 0));
+ if (check_literal_str(s, c, NULL) < 0) {
+ c->c_errors++;
+ return;
+ }
PyTuple_SET_ITEM(tup, (i-3)/2,
! PyString_FromString(s));
}
}
***************
*** 3915,3952 ****
com_arglist(struct compiling *c, node *n)
{
! int nch, i, narg;
int complex = 0;
- char nbuf[30];
REQ(n, varargslist);
/* varargslist:
(fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
! nch = NCH(n);
! /* Enter all arguments in table of locals */
! for (i = 0, narg = 0; i < nch; i++) {
! node *ch = CHILD(n, i);
! node *fp;
! if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
! break;
! REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
! fp = CHILD(ch, 0);
! if (TYPE(fp) != NAME) {
! PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
complex = 1;
- }
- narg++;
- /* all name updates handled by symtable */
- if (++i >= nch)
break;
! ch = CHILD(n, i);
! if (TYPE(ch) == EQUAL)
! i += 2;
! else
! REQ(ch, COMMA);
! }
if (complex) {
/* Generate code for complex arguments only after
having counted the simple arguments */
int ilocal = 0;
! for (i = 0; i < nch; i++) {
node *ch = CHILD(n, i);
node *fp;
--- 3993,4013 ----
com_arglist(struct compiling *c, node *n)
{
! int i;
int complex = 0;
REQ(n, varargslist);
/* varargslist:
(fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
! /* Check if the argument list includes nested tuples */
! for (i = 0; i < NCH(n); i++)
! if (TYPE(CHILD(n, i)) == LPAR) {
complex = 1;
break;
! }
! /* If it does, generate code to unpack them. */
if (complex) {
/* Generate code for complex arguments only after
having counted the simple arguments */
int ilocal = 0;
! for (i = 0; i < NCH(n); i++) {
node *ch = CHILD(n, i);
node *fp;
***************
*** 3961,3965 ****
}
ilocal++;
! if (++i >= nch)
break;
ch = CHILD(n, i);
--- 4022,4026 ----
}
ilocal++;
! if (++i >= NCH(n))
break;
ch = CHILD(n, i);
***************
*** 5360,5364 ****
{
int i, complex = -1, ext = 0;
! node *c = NULL;
if (TYPE(n) == parameters) {
--- 5421,5425 ----
{
int i, complex = -1, ext = 0;
! node *c = NULL, *ch = NULL;
if (TYPE(n) == parameters) {
***************
*** 5377,5383 ****
continue;
}
! if (TYPE(CHILD(c, 0)) == NAME)
symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
! else {
char nbuf[30];
PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
--- 5438,5449 ----
continue;
}
! ch = CHILD(c, 0);
! if (TYPE(ch) == NAME) {
! if (check_literal_str(STR(ch), NULL, st) < 0) {
! st->st_errors++;
! return;
! }
symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
! } else {
char nbuf[30];
PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);