Commit 5716d1ef authored by Vitja Makarov's avatar Vitja Makarov

Add utility for unbound locals exception and use it inside CCodeWriter.put_error_if_unbound

parent c87c0ba8
...@@ -1420,16 +1420,17 @@ class CCodeWriter(object): ...@@ -1420,16 +1420,17 @@ class CCodeWriter(object):
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): def put_error_if_unbound(self, pos, entry):
self.put('if (unlikely(!%s)) { PyErr_SetString(' % entry.cname) import ExprNodes
if entry.from_closure: if entry.from_closure:
self.put('PyExc_NameError, "') func = '__Pyx_RaiseClosureNameError'
self.put("free variable '%s' referenced before assignment in enclosing scope" % self.globalstate.use_utility_code(
entry.name) ExprNodes.raise_closure_name_error_utility_code)
else: else:
self.put('PyExc_UnboundLocalError, "') func = '__Pyx_RaiseUnboundLocalError'
self.put("local variable '%s' referenced before assignment" % self.globalstate.use_utility_code(
entry.name) ExprNodes.raise_unbound_local_error_utility_code)
self.putln('"); %s }' % self.error_goto(pos)) self.put('if (unlikely(!%s)) { %s("%s"); %s }' % (
entry.cname, func, 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
......
...@@ -8280,6 +8280,26 @@ static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void) { ...@@ -8280,6 +8280,26 @@ static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void) {
} }
''') ''')
raise_unbound_local_error_utility_code = UtilityCode(
proto = """
static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname);
""",
impl = """
static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname) {
PyErr_Format(PyExc_UnboundLocalError, "local variable '%s' referenced before assignment", varname);
}
""")
raise_closure_name_error_utility_code = UtilityCode(
proto = """
static CYTHON_INLINE void __Pyx_RaiseClosureNameError(const char *varname);
""",
impl = """
static CYTHON_INLINE void __Pyx_RaiseClosureNameError(const char *varname) {
PyErr_Format(PyExc_NameError, "free variable '%s' referenced before assignment in enclosing scope", varname);
}
""")
#------------------------------------------------------------------------------------ #------------------------------------------------------------------------------------
getitem_dict_utility_code = UtilityCode( getitem_dict_utility_code = UtilityCode(
......
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