diff options
| author | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2010-09-10 21:39:53 +0000 |
|---|---|---|
| committer | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2010-09-10 21:39:53 +0000 |
| commit | f4136c2c8d5c2470640d04074c10b9e576c0e4fb (patch) | |
| tree | 8bf27e5552741a2bf9b10349751b88b14bdc9986 /Python/compile.c | |
| parent | 624de501c80fe591a04202cc6474c1f81c4ec1e7 (diff) | |
| download | cpython-f4136c2c8d5c2470640d04074c10b9e576c0e4fb.tar.gz | |
#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.
Diffstat (limited to 'Python/compile.c')
| -rw-r--r-- | Python/compile.c | 10 |
1 files changed, 3 insertions, 7 deletions
diff --git a/Python/compile.c b/Python/compile.c index 6963a151a7..5341e6b535 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -857,6 +857,8 @@ opcode_stack_effect(int opcode, int oparg) return 1; case STORE_DEREF: return -1; + case DELETE_DEREF: + return 0; default: fprintf(stderr, "opcode = %d\n", opcode); Py_FatalError("opcode_stack_effect()"); @@ -2506,13 +2508,7 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) 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, |
