diff --git a/Lib/test/test_scope.py b/Lib/test/test_scope.py index b1549ef43eb..ab9e812a98f 100644 --- a/Lib/test/test_scope.py +++ b/Lib/test/test_scope.py @@ -647,6 +647,13 @@ result2 = h() self.assertEqual(2, global_ns["result2"]) self.assertEqual(9, global_ns["result9"]) + def testTopIsNotSignificant(self): + # See #9997. + def top(a): + pass + def b(): + global a + def test_main(): with check_warnings(("import \* only allowed at module level", diff --git a/Misc/NEWS b/Misc/NEWS index d8f2f1b3d62..c2b674b2452 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 2.7.1? Core and Builtins ----------------- +- Issue #9997: Don't let the name "top" have special significance in scope + resolution. + - Issue #9862: Compensate for broken PIPE_BUF in AIX by hard coding its value as the default 512 when compiling on AIX. diff --git a/Python/symtable.c b/Python/symtable.c index 98d1e11564f..c18b39ecf15 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -847,7 +847,7 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block, st->st_cur = ste_new(st, name, block, ast, lineno); if (st->st_cur == NULL) return 0; - if (name == GET_IDENTIFIER(top)) + if (block == ModuleBlock) st->st_global = st->st_cur->ste_symbols; if (prev) { if (PyList_Append(prev->ste_children,