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

Skip to content

Commit c176132

Browse files
committed
Warn about global statement at the module level.
Do better accounting for global variables.
1 parent 56ba72a commit c176132

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

Python/compile.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4178,7 +4178,7 @@ symtable_check_shadow(struct symtable *st, PyObject *name, int flags)
41784178
{
41794179
char buf[500];
41804180
PyObject *children, *v;
4181-
PySymtableEntryObject *child;
4181+
PySymtableEntryObject *child = NULL;
41824182
int i;
41834183

41844184
if (!(flags & DEF_BOUND))
@@ -4202,7 +4202,9 @@ symtable_check_shadow(struct symtable *st, PyObject *name, int flags)
42024202
if (!(cflags & DEF_BOUND))
42034203
break;
42044204
}
4205-
4205+
4206+
assert(child != NULL);
4207+
42064208
sprintf(buf, "local name '%.100s' in '%.100s' shadows "
42074209
"use of '%.100s' as global in nested scope '%.100s'",
42084210
PyString_AS_STRING(name),
@@ -4328,6 +4330,10 @@ symtable_load_symbols(struct compiling *c)
43284330
if (PyDict_SetItem(c->c_globals, name,
43294331
implicit) < 0)
43304332
goto fail;
4333+
v = PyInt_FromLong(flags);
4334+
if (PyDict_SetItem(st->st_global, name, v))
4335+
goto fail;
4336+
Py_DECREF(v);
43314337
}
43324338
}
43334339
}
@@ -4360,6 +4366,7 @@ symtable_init()
43604366
st->st_nscopes = 0;
43614367
st->st_errors = 0;
43624368
st->st_tmpname = 0;
4369+
st->st_global_star = 0;
43634370
st->st_private = NULL;
43644371
return st;
43654372
fail:
@@ -4922,6 +4929,12 @@ symtable_global(struct symtable *st, node *n)
49224929
{
49234930
int i;
49244931

4932+
if (st->st_nscopes == 1) {
4933+
if (symtable_warn(st,
4934+
"global statement has no meaning at module level") < 0)
4935+
return;
4936+
}
4937+
49254938
for (i = 1; i < NCH(n); i += 2) {
49264939
char *name = STR(CHILD(n, i));
49274940
int flags;
@@ -4991,6 +5004,8 @@ symtable_import(struct symtable *st, node *n)
49915004
}
49925005
if (TYPE(CHILD(n, 3)) == STAR) {
49935006
st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
5007+
if (st->st_nscopes == 1)
5008+
st->st_global_star = 1;
49945009
} else {
49955010
for (i = 3; i < NCH(n); i += 2) {
49965011
node *c = CHILD(n, i);

0 commit comments

Comments
 (0)