Merged revisions 70801,70809 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70801 | jeremy.hylton | 2009-03-31 09:17:03 -0400 (Tue, 31 Mar 2009) | 3 lines
Add is_declared_global() which distinguishes between implicit and
explicit global variables.
........
r70809 | jeremy.hylton | 2009-03-31 09:48:15 -0400 (Tue, 31 Mar 2009) | 14 lines
Global statements from one function leaked into parallel functions.
Re http://bugs.python.org/issue4315
The symbol table used the same name dictionaries to recursively
analyze each of its child blocks, even though the dictionaries are
modified during analysis. The fix is to create new temporary
dictionaries via the analyze_child_block(). The only information that
needs to propagate back up is the names of the free variables.
Add more comments and break out a helper function. This code doesn't
get any easier to understand when you only look at it once a year.
........
diff --git a/Lib/symtable.py b/Lib/symtable.py
index 9d58618..0a25073 100644
--- a/Lib/symtable.py
+++ b/Lib/symtable.py
@@ -201,6 +201,9 @@
DeprecationWarning, 2)
return False
+ 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 bb186d1..36177ea 100644
--- a/Lib/test/test_scope.py
+++ b/Lib/test/test_scope.py
@@ -632,6 +632,30 @@
f() # used to crash the interpreter...
+ 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 in local_ns, global_ns
+ self.assertEqual(2, global_ns["result2"])
+ self.assertEqual(9, global_ns["result9"])
def test_main():
diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py
index f1fa18a..3b61a95 100644
--- a/Lib/test/test_symtable.py
+++ b/Lib/test/test_symtable.py
@@ -114,7 +114,9 @@
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())