storage.
+.. opcode:: DELETE_DEREF (i)
+
+ Empties the cell contained in slot *i* of the cell and free variable storage.
+ Used by the :keyword:`del` statement.
+
+
.. opcode:: SET_LINENO (lineno)
This opcode is obsolete.
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
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:
(See :issue:`8188`.)
+* Previously it was illegal to delete a name from the local namespace if it
+ occurs as a free variable in a nested block::
+
+ >>> def outer(x):
+ ... def inner():
+ ... return x
+ ... inner()
+ ... del x
+
+ This is now allowed. Remember that the target of an :keyword:`except` clause
+ is cleared, so this code which used to work with Python 2.6, raised a
+ :exc:`SyntaxError` with Python 3.1 and now works again::
+
+ >>> def f():
+ ... def print_error():
+ ... print(e)
+ ... try:
+ ... something
+ ... except Exception as e:
+ ... print_error()
+ ... # implicit "del e" here
+
+ (See :issue:`4617`.)
+
+
New, Improved, and Deprecated Modules
=====================================
#define LOAD_CLOSURE 135 /* Load free variable from closure */
#define LOAD_DEREF 136 /* Load and dereference from closure cell */
#define STORE_DEREF 137 /* Store into cell */
+#define DELETE_DEREF 138 /* Delete closure cell */
/* The next 3 opcodes must be contiguous and satisfy
(CALL_FUNCTION_VAR - CALL_FUNCTION) & 3 == 1 */
hasfree.append(136)
def_op('STORE_DEREF', 137)
hasfree.append(137)
+def_op('DELETE_DEREF', 138)
+hasfree.append(138)
def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
obj = wr()
self.assertTrue(obj is None, "%s" % obj)
+ def test_exception_target_in_nested_scope(self):
+ # issue 4617: This used to raise a SyntaxError
+ # "can not delete variable 'e' referenced in nested scope"
+ def print_error():
+ e
+ try:
+ something
+ except Exception as e:
+ print_error()
+ # implicit "del e" here
+
def test_generator_leaking(self):
# Test that generator exception state doesn't leak into the calling
# frame
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():
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
def test_break_outside_loop(self):
self._check_error("break", "outside loop")
- def test_delete_deref(self):
- source = """if 1:
- def foo(x):
- def bar():
- print(x)
- del x
- """
- self._check_error(source, "nested scope")
-
def test_unexpected_indent(self):
self._check_error("foo()\n bar()\n", "unexpected indent",
subclass=IndentationError)
Core and Builtins
-----------------
+- Issue #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).
+
- Issue #9804: ascii() now always represents unicode surrogate pairs as
a single ``\UXXXXXXXX``, regardless of whether the character is printable
or not. Also, the "backslashreplace" error handler now joins surrogate
static PyObject * import_from(PyObject *, PyObject *);
static int import_all_from(PyObject *, PyObject *);
static void format_exc_check_arg(PyObject *, const char *, PyObject *);
+static void format_exc_unbound(PyCodeObject *co, int oparg);
static PyObject * unicode_concatenate(PyObject *, PyObject *,
PyFrameObject *, unsigned char *);
static PyObject * special_lookup(PyObject *, char *, PyObject **);
);
break;
+ TARGET(DELETE_DEREF)
+ x = freevars[oparg];
+ if (PyCell_GET(x) != NULL) {
+ PyCell_Set(x, NULL);
+ continue;
+ }
+ err = -1;
+ format_exc_unbound(co, oparg);
+ break;
+
TARGET(LOAD_CLOSURE)
x = freevars[oparg];
Py_INCREF(x);
DISPATCH();
}
err = -1;
- /* Don't stomp existing exception */
- if (PyErr_Occurred())
- break;
- if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
- v = PyTuple_GET_ITEM(co->co_cellvars,
- oparg);
- format_exc_check_arg(
- PyExc_UnboundLocalError,
- UNBOUNDLOCAL_ERROR_MSG,
- v);
- } else {
- v = PyTuple_GET_ITEM(co->co_freevars, oparg -
- PyTuple_GET_SIZE(co->co_cellvars));
- format_exc_check_arg(PyExc_NameError,
- UNBOUNDFREE_ERROR_MSG, v);
- }
+ format_exc_unbound(co, oparg);
break;
TARGET(STORE_DEREF)
PyErr_Format(exc, format_str, obj_str);
}
+static void
+format_exc_unbound(PyCodeObject *co, int oparg)
+{
+ PyObject *name;
+ /* Don't stomp existing exception */
+ if (PyErr_Occurred())
+ return;
+ if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
+ name = PyTuple_GET_ITEM(co->co_cellvars,
+ oparg);
+ format_exc_check_arg(
+ PyExc_UnboundLocalError,
+ UNBOUNDLOCAL_ERROR_MSG,
+ name);
+ } else {
+ name = PyTuple_GET_ITEM(co->co_freevars, oparg -
+ PyTuple_GET_SIZE(co->co_cellvars));
+ format_exc_check_arg(PyExc_NameError,
+ UNBOUNDFREE_ERROR_MSG, name);
+ }
+}
+
static PyObject *
unicode_concatenate(PyObject *v, PyObject *w,
PyFrameObject *f, unsigned char *next_instr)
return 1;
case STORE_DEREF:
return -1;
+ case DELETE_DEREF:
+ return 0;
default:
fprintf(stderr, "opcode = %d\n", opcode);
Py_FatalError("opcode_stack_effect()");
case AugLoad:
case AugStore:
break;
- case Del:
- PyErr_Format(PyExc_SyntaxError,
- "can not delete variable '%S' referenced "
- "in nested scope",
- name);
- Py_DECREF(mangled);
- return 0;
+ case Del: op = DELETE_DEREF; break;
case Param:
default:
PyErr_SetString(PyExc_SystemError,
&&TARGET_LOAD_CLOSURE,
&&TARGET_LOAD_DEREF,
&&TARGET_STORE_DEREF,
- &&_unknown_opcode,
+ &&TARGET_DELETE_DEREF,
&&_unknown_opcode,
&&TARGET_CALL_FUNCTION_VAR,
&&TARGET_CALL_FUNCTION_KW,