Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be77cf7

Browse files
committed
Add warnings about undefined "global"
SF bug #233532 XXX Can't figure out how to write test cases that work with warnings
1 parent a35c688 commit be77cf7

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Python/compile.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4853,6 +4853,35 @@ symtable_global(struct symtable *st, node *n)
48534853

48544854
for (i = 1; i < NCH(n); i += 2) {
48554855
char *name = STR(CHILD(n, i));
4856+
int flags;
4857+
4858+
flags = symtable_lookup(st, name);
4859+
if (flags && flags != DEF_GLOBAL) {
4860+
char buf[500];
4861+
if (flags & DEF_PARAM) {
4862+
PyErr_Format(PyExc_SyntaxError,
4863+
"name '%.400s' is local and global",
4864+
PyString_AS_STRING(name));
4865+
set_error_location(st->st_filename,
4866+
st->st_cur->ste_lineno);
4867+
st->st_errors++;
4868+
return;
4869+
} else if (flags & DEF_LOCAL) {
4870+
sprintf(buf, GLOBAL_AFTER_ASSIGN, name);
4871+
if (PyErr_Warn(PyExc_SyntaxWarning,
4872+
buf) < 0) {
4873+
/* XXX set line number? */
4874+
st->st_errors++;
4875+
}
4876+
} else {
4877+
sprintf(buf, GLOBAL_AFTER_USE, name);
4878+
if (PyErr_Warn(PyExc_SyntaxWarning,
4879+
buf) < 0) {
4880+
/* XXX set line number? */
4881+
st->st_errors++;
4882+
}
4883+
}
4884+
}
48564885
symtable_add_def(st, name, DEF_GLOBAL);
48574886
}
48584887
}

0 commit comments

Comments
 (0)