summaryrefslogtreecommitdiffstats
path: root/Python/ceval.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1999-06-22 14:47:32 (GMT)
committerGuido van Rossum <guido@python.org>1999-06-22 14:47:32 (GMT)
commit87460821755ba72294b0c95b7d130b922d2a7ea4 (patch)
tree0f0a205db4141203afe732f9aeafaeeddaf36386 /Python/ceval.c
parent43128905befc05001219d79715448191a04b8ab9 (diff)
downloadcpython-87460821755ba72294b0c95b7d130b922d2a7ea4.zip
cpython-87460821755ba72294b0c95b7d130b922d2a7ea4.tar.gz
cpython-87460821755ba72294b0c95b7d130b922d2a7ea4.tar.bz2
Patch by Tim Peters:
Introduce a new builtin exception, UnboundLocalError, raised when ceval.c tries to retrieve or delete a local name that isn't bound to a value. Currently raises NameError, which makes this behavior a FAQ since the same error is raised for "missing" global names too: when the user has a global of the same name as the unbound local, NameError makes no sense to them. Even in the absence of shadowing, knowing whether a bogus name is local or global is a real aid to quick understanding. Example: D:\src\PCbuild>type local.py x = 42 def f(): print x x = 13 return x f() D:\src\PCbuild>python local.py Traceback (innermost last): File "local.py", line 8, in ? f() File "local.py", line 4, in f print x UnboundLocalError: x D:\src\PCbuild> Note that UnboundLocalError is a subclass of NameError, for compatibility with existing class-exception code that may be trying to catch this as a NameError. Unfortunately, I see no way to make this wholly compatible with -X (see comments in bltinmodule.c): under -X, [UnboundLocalError is an alias for NameError --GvR]. [The ceval.c patch differs slightly from the second version that Tim submitted; I decided not to raise UnboundLocalError for DELETE_NAME, only for DELETE_LOCAL. DELETE_NAME is only generated at the module level, and since at that level a NameError is raised for referencing an undefined name, it should also be raised for deleting one.]
Diffstat (limited to 'Python/ceval.c')
-rw-r--r--Python/ceval.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index 6218397..1c51ccf 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1320,7 +1320,7 @@ eval_code2(co, globals, locals,
case LOAD_FAST:
x = GETLOCAL(oparg);
if (x == NULL) {
- PyErr_SetObject(PyExc_NameError,
+ PyErr_SetObject(PyExc_UnboundLocalError,
PyTuple_GetItem(co->co_varnames,
oparg));
break;
@@ -1338,7 +1338,7 @@ eval_code2(co, globals, locals,
case DELETE_FAST:
x = GETLOCAL(oparg);
if (x == NULL) {
- PyErr_SetObject(PyExc_NameError,
+ PyErr_SetObject(PyExc_UnboundLocalError,
PyTuple_GetItem(co->co_varnames,
oparg));
break;