[Python-checkins] r65177 - in python/trunk: Lib/test/test_scope.py Misc/NEWS Objects/frameobject.c

amaury.forgeotdarc python-checkins at python.org
Tue Jul 22 00:00:38 CEST 2008


Author: amaury.forgeotdarc
Date: Tue Jul 22 00:00:38 2008
New Revision: 65177
Log:
Issue2378: pdb would delete free variables when stepping into a class statement.
The problem was introduced by r53954, the correction is to restore the symmetry between
PyFrame_FastToLocals and PyFrame_LocalsToFast
Modified:
 python/trunk/Lib/test/test_scope.py
 python/trunk/Misc/NEWS
 python/trunk/Objects/frameobject.c
Modified: python/trunk/Lib/test/test_scope.py
==============================================================================
--- python/trunk/Lib/test/test_scope.py	(original)
+++ python/trunk/Lib/test/test_scope.py	Tue Jul 22 00:00:38 2008
@@ -519,6 +519,24 @@
 self.assert_("x" not in varnames)
 self.assert_("y" in varnames)
 
+ def testLocalsClass_WithTrace(self):
+ # Issue23728: after the trace function returns, the locals()
+ # dictionary is used to update all variables, this used to
+ # include free variables. But in class statements, free
+ # variables are not inserted...
+ import sys
+ sys.settrace(lambda a,b,c:None)
+ try:
+ x = 12
+
+ class C:
+ def f(self):
+ return x
+
+ assert x == 12 # Used to raise UnboundLocalError
+ finally:
+ sys.settrace(None)
+
 def testBoundAndFree(self):
 # var is bound and free in class
 
Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Tue Jul 22 00:00:38 2008
@@ -12,6 +12,10 @@
 Core and Builtins
 -----------------
 
+- Issue #2378: An unexpected UnboundLocalError or NameError could appear when
+ the python debugger steps into a class statement: the free variables (local
+ variables defined in an outer scope) would be deleted from the outer scope.
+
 Library
 -------
 
Modified: python/trunk/Objects/frameobject.c
==============================================================================
--- python/trunk/Objects/frameobject.c	(original)
+++ python/trunk/Objects/frameobject.c	Tue Jul 22 00:00:38 2008
@@ -904,9 +904,12 @@
 	if (ncells || nfreevars) {
 		dict_to_map(co->co_cellvars, ncells,
 			 locals, fast + co->co_nlocals, 1, clear);
-		dict_to_map(co->co_freevars, nfreevars,
-			 locals, fast + co->co_nlocals + ncells, 1, 
-			 clear);
+		/* Same test as in PyFrame_FastToLocals() above. */
+		if (co->co_flags & CO_OPTIMIZED) {
+			dict_to_map(co->co_freevars, nfreevars,
+			 locals, fast + co->co_nlocals + ncells, 1, 
+			 clear);
+		}
 	}
 	PyErr_Restore(error_type, error_value, error_traceback);
 }


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /