]> granicus.if.org Git - python/commitdiff
Issue2378: pdb would delete free variables when stepping into a class statement.
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Mon, 21 Jul 2008 22:00:38 +0000 (22:00 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Mon, 21 Jul 2008 22:00:38 +0000 (22:00 +0000)
The problem was introduced by r53954, the correction is to restore the symmetry between
PyFrame_FastToLocals and PyFrame_LocalsToFast

Lib/test/test_scope.py
Misc/NEWS
Objects/frameobject.c

index cd2d98c075c4634fd42da28caf855d034fe3f8b1..3914ed0520a371ba49d84bf650b4878ec41c9cb6 100644 (file)
@@ -519,6 +519,24 @@ self.assert_(X.passed)
         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
 
index 03f80a5c2fc935f40b51c662bd0d6778799a5d6d..794663354b1e28f0e96955d7ac3edfc4b47c1c9b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 2.6 beta 3?
 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
 -------
 
index d89d72d6b4d12e0290494846136b4ae9f53795ce..079c83171b1e891e5128f5a9b164febe9683590b 100644 (file)
@@ -904,9 +904,12 @@ PyFrame_LocalsToFast(PyFrameObject *f, int clear)
        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);
 }