#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:
Amaury Forgeot d'Arc 2010-09-10 21:39:53 +00:00
parent 4785916d62
commit ba117ef7e9
12 changed files with 113 additions and 45 deletions

View file

@ -388,11 +388,6 @@ namespace, depending on whether the name occurs in a :keyword:`global` statement
in the same code block. If the name is unbound, a :exc:`NameError` exception
will be raised.
.. index:: pair: free; variable
It is illegal to delete a name from the local namespace if it occurs as a free
variable in a nested block.
.. index:: pair: attribute; deletion
Deletion of attribute references, subscriptions and slicings is passed to the
@ -400,6 +395,10 @@ primary object involved; deletion of a slicing is in general equivalent to
assignment of an empty slice of the right type (but even this is determined by
the sliced object).
.. versionchanged:: 3.2
Previously it was illegal to delete a name from the local namespace if it
occurs as a free variable in a nested block.
.. _return: