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):
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(' % entry.cname)
import ExprNodes
if entry.from_closure:
self.put('PyExc_NameError, "')
self.put("free variable '%s' referenced before assignment in enclosing scope" %
entry.name)
func = '__Pyx_RaiseClosureNameError'
self.globalstate.use_utility_code(
ExprNodes.raise_closure_name_error_utility_code)
else:
self.put('PyExc_UnboundLocalError, "')
self.put("local variable '%s' referenced before assignment" %
entry.name)
self.putln('"); %s }' % self.error_goto(pos))
func = '__Pyx_RaiseUnboundLocalError'
self.globalstate.use_utility_code(
ExprNodes.raise_unbound_local_error_utility_code)
self.put('if (unlikely(!%s)) { %s("%s"); %s }' % (
entry.cname, func, entry.name, self.error_goto(pos)))
def set_error_info(self, pos):
self.funcstate.should_declare_error_indicator = True
......
......@@ -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(
......
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