mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
#4617: Previously it was illegal to delete a name from the local
namespace if it occurs as a free variable in a nested block. This limitation of the compiler has been lifted, and a new opcode introduced (DELETE_DEREF). This sample was valid in 2.6, but fails to compile in 3.x without this change:: >>> def f(): ... def print_error(): ... print(e) ... try: ... something ... except Exception as e: ... print_error() ... # implicit "del e" here This sample has always been invalid in Python, and now works:: >>> def outer(x): ... def inner(): ... return x ... inner() ... del x There is no need to bump the PYC magic number: the new opcode is used for code that did not compile before.
This commit is contained in:
parent
4785916d62
commit
ba117ef7e9
12 changed files with 113 additions and 45 deletions
|
@ -217,13 +217,6 @@ class ScopeTests(unittest.TestCase):
|
|||
return f
|
||||
""")
|
||||
|
||||
check_syntax_error(self, """if 1:
|
||||
def f(x):
|
||||
def g():
|
||||
return x
|
||||
del x # can't del name
|
||||
""")
|
||||
|
||||
check_syntax_error(self, """if 1:
|
||||
def f():
|
||||
def g():
|
||||
|
@ -272,6 +265,28 @@ class ScopeTests(unittest.TestCase):
|
|||
self.assertRaises(UnboundLocalError, errorInOuter)
|
||||
self.assertRaises(NameError, errorInInner)
|
||||
|
||||
def testUnboundLocal_AfterDel(self):
|
||||
# #4617: It is now legal to delete a cell variable.
|
||||
# The following functions must obviously compile,
|
||||
# and give the correct error when accessing the deleted name.
|
||||
def errorInOuter():
|
||||
y = 1
|
||||
del y
|
||||
print(y)
|
||||
def inner():
|
||||
return y
|
||||
|
||||
def errorInInner():
|
||||
def inner():
|
||||
return y
|
||||
y = 1
|
||||
del y
|
||||
inner()
|
||||
|
||||
self.assertRaises(UnboundLocalError, errorInOuter)
|
||||
self.assertRaises(NameError, errorInInner)
|
||||
|
||||
def testUnboundLocal_AugAssign(self):
|
||||
# test for bug #1501934: incorrect LOAD/STORE_GLOBAL generation
|
||||
exec("""if 1:
|
||||
global_x = 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue