Skip to content

Commit

Permalink
Make UnboundLocalError message same as in CPython
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek committed May 28, 2011
1 parent bc0a799 commit bf1e213
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
7 changes: 7 additions & 0 deletions Cython/Compiler/Code.py
Expand Up @@ -1419,6 +1419,13 @@ def put_error_if_neg(self, pos, value):
# return self.putln("if (unlikely(%s < 0)) %s" % (value, self.error_goto(pos))) # TODO this path is almost _never_ taken, yet this macro makes is slower!
return self.putln("if (%s < 0) %s" % (value, self.error_goto(pos)))

def put_error_if_unbound(self, pos, entry):
self.put('if (unlikely(!%s)) { '
'PyErr_SetString(PyExc_UnboundLocalError, "'
"local variable '%s' referenced before assignment"
'"); %s }' %
(entry.cname, entry.name, self.error_goto(pos)))

def set_error_info(self, pos):
self.funcstate.should_declare_error_indicator = True
if self.c_line_in_traceback:
Expand Down
9 changes: 4 additions & 5 deletions Cython/Compiler/ExprNodes.py
Expand Up @@ -1558,9 +1558,9 @@ def generate_result_code(self, code):

elif entry.is_local or entry.in_closure or entry.from_closure:
if entry.type.is_pyobject:
if (self.cf_maybe_null or self.cf_is_null) and not self.allow_null:
code.putln('if (unlikely(!%s)) { PyErr_SetString(PyExc_UnboundLocalError, "%s"); %s }' %
(entry.cname, entry.name, code.error_goto(self.pos)))
if (self.cf_maybe_null or self.cf_is_null) \
and not self.allow_null:
code.put_error_if_unbound(self.pos, entry)

def generate_assignment_code(self, rhs, code):
#print "NameNode.generate_assignment_code:", self.name ###
Expand Down Expand Up @@ -1693,8 +1693,7 @@ def generate_deletion_code(self, code):
elif self.entry.type.is_pyobject:
if not self.cf_is_null:
if self.cf_maybe_null:
code.putln('if (unlikely(!%s)) { PyErr_SetString(PyExc_UnboundLocalError, "%s"); %s }' %
(self.entry.cname, self.entry.name, code.error_goto(self.pos)))
code.put_error_if_unbound(self.pos, self.entry)
code.put_decref(self.result(), self.ctype())
code.putln('%s = NULL;' % self.result())
else:
Expand Down
18 changes: 9 additions & 9 deletions tests/run/uninitialized.pyx
Expand Up @@ -8,7 +8,7 @@ def conditional(cond):
>>> conditional(False)
Traceback (most recent call last):
...
UnboundLocalError: a
UnboundLocalError: local variable 'a' referenced before assignment
"""
if cond:
a = []
Expand All @@ -21,7 +21,7 @@ def inside_loop(iter):
>>> inside_loop([])
Traceback (most recent call last):
...
UnboundLocalError: i
UnboundLocalError: local variable 'i' referenced before assignment
"""
for i in iter:
pass
Expand All @@ -34,7 +34,7 @@ def try_except(cond):
>>> try_except(False)
Traceback (most recent call last):
...
UnboundLocalError: a
UnboundLocalError: local variable 'a' referenced before assignment
"""
try:
if cond:
Expand All @@ -50,7 +50,7 @@ def try_finally(cond):
>>> try_finally(False)
Traceback (most recent call last):
...
UnboundLocalError: a
UnboundLocalError: local variable 'a' referenced before assignment
"""
try:
if cond:
Expand All @@ -66,7 +66,7 @@ def deleted(cond):
>>> deleted(True)
Traceback (most recent call last):
...
UnboundLocalError: a
UnboundLocalError: local variable 'a' referenced before assignment
"""
a = {}
if cond:
Expand All @@ -80,7 +80,7 @@ def test_nested(cond):
>>> test_nested(False)
Traceback (most recent call last):
...
UnboundLocalError: a
UnboundLocalError: local variable 'a' referenced before assignment
"""
if cond:
def a():
Expand All @@ -94,7 +94,7 @@ def test_outer(cond):
>>> test_outer(False)
Traceback (most recent call last):
...
UnboundLocalError: a
UnboundLocalError: local variable 'a' referenced before assignment
"""
if cond:
a = {}
Expand All @@ -109,7 +109,7 @@ def test_inner(cond):
>>> test_inner(False)
Traceback (most recent call last):
...
UnboundLocalError: a
UnboundLocalError: local variable 'a' referenced before assignment
"""
if cond:
a = {}
Expand All @@ -124,7 +124,7 @@ def test_class(cond):
>>> test_class(False)
Traceback (most recent call last):
...
UnboundLocalError: A
UnboundLocalError: local variable 'A' referenced before assignment
"""
if cond:
class A:
Expand Down

0 comments on commit bf1e213

Please sign in to comment.