Commit bf1e2133 authored by Vitja Makarov's avatar Vitja Makarov

Make UnboundLocalError message same as in CPython

parent bc0a7990
......@@ -1419,6 +1419,13 @@ class CCodeWriter(object):
# 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:
......
......@@ -1558,9 +1558,9 @@ class NameNode(AtomicExprNode):
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 ###
......@@ -1693,8 +1693,7 @@ class NameNode(AtomicExprNode):
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:
......
......@@ -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 = []
......@@ -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
......@@ -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:
......@@ -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:
......@@ -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:
......@@ -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():
......@@ -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 = {}
......@@ -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 = {}
......@@ -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:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment