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): ...@@ -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 (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))) 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): def set_error_info(self, pos):
self.funcstate.should_declare_error_indicator = True self.funcstate.should_declare_error_indicator = True
if self.c_line_in_traceback: if self.c_line_in_traceback:
......
...@@ -1558,9 +1558,9 @@ class NameNode(AtomicExprNode): ...@@ -1558,9 +1558,9 @@ class NameNode(AtomicExprNode):
elif entry.is_local or entry.in_closure or entry.from_closure: elif entry.is_local or entry.in_closure or entry.from_closure:
if entry.type.is_pyobject: if entry.type.is_pyobject:
if (self.cf_maybe_null or self.cf_is_null) and not self.allow_null: if (self.cf_maybe_null or self.cf_is_null) \
code.putln('if (unlikely(!%s)) { PyErr_SetString(PyExc_UnboundLocalError, "%s"); %s }' % and not self.allow_null:
(entry.cname, entry.name, code.error_goto(self.pos))) code.put_error_if_unbound(self.pos, entry)
def generate_assignment_code(self, rhs, code): def generate_assignment_code(self, rhs, code):
#print "NameNode.generate_assignment_code:", self.name ### #print "NameNode.generate_assignment_code:", self.name ###
...@@ -1693,8 +1693,7 @@ class NameNode(AtomicExprNode): ...@@ -1693,8 +1693,7 @@ class NameNode(AtomicExprNode):
elif self.entry.type.is_pyobject: elif self.entry.type.is_pyobject:
if not self.cf_is_null: if not self.cf_is_null:
if self.cf_maybe_null: if self.cf_maybe_null:
code.putln('if (unlikely(!%s)) { PyErr_SetString(PyExc_UnboundLocalError, "%s"); %s }' % code.put_error_if_unbound(self.pos, self.entry)
(self.entry.cname, self.entry.name, code.error_goto(self.pos)))
code.put_decref(self.result(), self.ctype()) code.put_decref(self.result(), self.ctype())
code.putln('%s = NULL;' % self.result()) code.putln('%s = NULL;' % self.result())
else: else:
......
...@@ -8,7 +8,7 @@ def conditional(cond): ...@@ -8,7 +8,7 @@ def conditional(cond):
>>> conditional(False) >>> conditional(False)
Traceback (most recent call last): Traceback (most recent call last):
... ...
UnboundLocalError: a UnboundLocalError: local variable 'a' referenced before assignment
""" """
if cond: if cond:
a = [] a = []
...@@ -21,7 +21,7 @@ def inside_loop(iter): ...@@ -21,7 +21,7 @@ def inside_loop(iter):
>>> inside_loop([]) >>> inside_loop([])
Traceback (most recent call last): Traceback (most recent call last):
... ...
UnboundLocalError: i UnboundLocalError: local variable 'i' referenced before assignment
""" """
for i in iter: for i in iter:
pass pass
...@@ -34,7 +34,7 @@ def try_except(cond): ...@@ -34,7 +34,7 @@ def try_except(cond):
>>> try_except(False) >>> try_except(False)
Traceback (most recent call last): Traceback (most recent call last):
... ...
UnboundLocalError: a UnboundLocalError: local variable 'a' referenced before assignment
""" """
try: try:
if cond: if cond:
...@@ -50,7 +50,7 @@ def try_finally(cond): ...@@ -50,7 +50,7 @@ def try_finally(cond):
>>> try_finally(False) >>> try_finally(False)
Traceback (most recent call last): Traceback (most recent call last):
... ...
UnboundLocalError: a UnboundLocalError: local variable 'a' referenced before assignment
""" """
try: try:
if cond: if cond:
...@@ -66,7 +66,7 @@ def deleted(cond): ...@@ -66,7 +66,7 @@ def deleted(cond):
>>> deleted(True) >>> deleted(True)
Traceback (most recent call last): Traceback (most recent call last):
... ...
UnboundLocalError: a UnboundLocalError: local variable 'a' referenced before assignment
""" """
a = {} a = {}
if cond: if cond:
...@@ -80,7 +80,7 @@ def test_nested(cond): ...@@ -80,7 +80,7 @@ def test_nested(cond):
>>> test_nested(False) >>> test_nested(False)
Traceback (most recent call last): Traceback (most recent call last):
... ...
UnboundLocalError: a UnboundLocalError: local variable 'a' referenced before assignment
""" """
if cond: if cond:
def a(): def a():
...@@ -94,7 +94,7 @@ def test_outer(cond): ...@@ -94,7 +94,7 @@ def test_outer(cond):
>>> test_outer(False) >>> test_outer(False)
Traceback (most recent call last): Traceback (most recent call last):
... ...
UnboundLocalError: a UnboundLocalError: local variable 'a' referenced before assignment
""" """
if cond: if cond:
a = {} a = {}
...@@ -109,7 +109,7 @@ def test_inner(cond): ...@@ -109,7 +109,7 @@ def test_inner(cond):
>>> test_inner(False) >>> test_inner(False)
Traceback (most recent call last): Traceback (most recent call last):
... ...
UnboundLocalError: a UnboundLocalError: local variable 'a' referenced before assignment
""" """
if cond: if cond:
a = {} a = {}
...@@ -124,7 +124,7 @@ def test_class(cond): ...@@ -124,7 +124,7 @@ def test_class(cond):
>>> test_class(False) >>> test_class(False)
Traceback (most recent call last): Traceback (most recent call last):
... ...
UnboundLocalError: A UnboundLocalError: local variable 'A' referenced before assignment
""" """
if cond: if cond:
class A: 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