diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/symtable.py | 3 | ||||
-rw-r--r-- | Lib/test/test_scope.py | 26 | ||||
-rw-r--r-- | Lib/test/test_symtable.py | 2 |
3 files changed, 29 insertions, 2 deletions
diff --git a/Lib/symtable.py b/Lib/symtable.py index 0d73870..7548a7f 100644 --- a/Lib/symtable.py +++ b/Lib/symtable.py @@ -191,6 +191,9 @@ class Symbol(object): def is_global(self): return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)) + def is_declared_global(self): + return bool(self.__scope == GLOBAL_EXPLICIT) + def is_local(self): return bool(self.__flags & DEF_BOUND) diff --git a/Lib/test/test_scope.py b/Lib/test/test_scope.py index 65d87cf..fb1e26f 100644 --- a/Lib/test/test_scope.py +++ b/Lib/test/test_scope.py @@ -618,7 +618,6 @@ self.assert_(X.passed) self.assertEqual(dec(), 0) def testNonLocalMethod(self): - def f(x): class c: def inc(self): @@ -630,13 +629,36 @@ self.assert_(X.passed) x -= 1 return x return c() - c = f(0) self.assertEqual(c.inc(), 1) self.assertEqual(c.inc(), 2) self.assertEqual(c.dec(), 1) self.assertEqual(c.dec(), 0) + def testGlobalInParallelNestedFunctions(self): + # A symbol table bug leaked the global statement from one + # function to other nested functions in the same block. + # This test verifies that a global statement in the first + # function does not affect the second function. + CODE = """def f(): + y = 1 + def g(): + global y + return y + def h(): + return y + 1 + return g, h +y = 9 +g, h = f() +result9 = g() +result2 = h() +""" + local_ns = {} + global_ns = {} + exec(CODE, local_ns, global_ns) + self.assertEqual(2, global_ns["result2"]) + self.assertEqual(9, global_ns["result9"]) + def testNonLocalClass(self): def f(x): diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py index 7e0206d..45b7be8 100644 --- a/Lib/test/test_symtable.py +++ b/Lib/test/test_symtable.py @@ -88,7 +88,9 @@ class SymtableTest(unittest.TestCase): def test_globals(self): self.assertTrue(self.spam.lookup("glob").is_global()) + self.assertFalse(self.spam.lookup("glob").is_declared_global()) self.assertTrue(self.spam.lookup("bar").is_global()) + self.assertTrue(self.spam.lookup("bar").is_declared_global()) self.assertFalse(self.internal.lookup("x").is_global()) self.assertFalse(self.Mine.lookup("instance_var").is_global()) |