summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_scope.py18
-rw-r--r--Misc/NEWS4
-rw-r--r--Objects/frameobject.c9
3 files changed, 28 insertions, 3 deletions
diff --git a/Lib/test/test_scope.py b/Lib/test/test_scope.py
index cd2d98c..3914ed0 100644
--- a/Lib/test/test_scope.py
+++ b/Lib/test/test_scope.py
@@ -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
diff --git a/Misc/NEWS b/Misc/NEWS
index 03f80a5..7946633 100644
--- 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
-------
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index d89d72d..079c831 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -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);
}