Change error message raised when free variable is not yet bound. It

now raises NameError instead of UnboundLocalError, because the var in
question is definitely not local.  (This affects test_scope.py)

Also update the recent fix by Ping using get_func_name().  Replace
tests of get_func_name() return value with call to get_func_desc() to
match all the other uses.
This commit is contained in:
Jeremy Hylton 2001-04-13 16:51:46 +00:00
parent 62effc1127
commit c76770c68c
2 changed files with 18 additions and 11 deletions

View file

@ -291,7 +291,7 @@ else:
try: try:
errorInInner() errorInInner()
except UnboundLocalError: except NameError:
pass pass
else: else:
raise TestFailed raise TestFailed
@ -435,3 +435,4 @@ d = f(2)(4)
verify(d.has_key('h')) verify(d.has_key('h'))
del d['h'] del d['h']
verify(d == {'x': 2, 'y': 7, 'w': 6}) verify(d == {'x': 2, 'y': 7, 'w': 6})

View file

@ -83,6 +83,9 @@ static void format_exc_check_arg(PyObject *, char *, PyObject *);
"global name '%.200s' is not defined" "global name '%.200s' is not defined"
#define UNBOUNDLOCAL_ERROR_MSG \ #define UNBOUNDLOCAL_ERROR_MSG \
"local variable '%.200s' referenced before assignment" "local variable '%.200s' referenced before assignment"
#define UNBOUNDFREE_ERROR_MSG \
"free variable '%.200s' referenced before assignment" \
" in enclosing scope"
/* Dynamic execution profile */ /* Dynamic execution profile */
#ifdef DYNAMIC_EXECUTION_PROFILE #ifdef DYNAMIC_EXECUTION_PROFILE
@ -1693,18 +1696,22 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
x = freevars[oparg]; x = freevars[oparg];
w = PyCell_Get(x); w = PyCell_Get(x);
if (w == NULL) { if (w == NULL) {
if (oparg < f->f_ncells) if (oparg < f->f_ncells) {
v = PyTuple_GetItem(co->co_cellvars, v = PyTuple_GetItem(co->co_cellvars,
oparg); oparg);
else format_exc_check_arg(
PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG,
v);
} else {
v = PyTuple_GetItem( v = PyTuple_GetItem(
co->co_freevars, co->co_freevars,
oparg - f->f_ncells); oparg - f->f_ncells);
format_exc_check_arg(
format_exc_check_arg( PyExc_NameError,
PyExc_UnboundLocalError, UNBOUNDFREE_ERROR_MSG,
UNBOUNDLOCAL_ERROR_MSG, v);
v); }
err = -1; err = -1;
break; break;
} }
@ -2883,11 +2890,10 @@ call_method(PyObject *func, PyObject *arg, PyObject *kw)
return NULL; return NULL;
} }
if (!ok) { if (!ok) {
char* fn = get_func_name(func);
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"unbound method %s%smust be " "unbound method %s%s must be "
"called with instance as first argument", "called with instance as first argument",
fn ? fn : "", fn ? "() " : ""); get_func_name(func), get_func_desc(func));
return NULL; return NULL;
} }
Py_INCREF(arg); Py_INCREF(arg);