Commit 6b1c2ef1 authored by Stefan Behnel's avatar Stefan Behnel

simplify import-from code a little by moving the name lookup into a utility function

parent 95553775
...@@ -6764,8 +6764,6 @@ class FromImportStatNode(StatNode): ...@@ -6764,8 +6764,6 @@ class FromImportStatNode(StatNode):
else: else:
coerced_item = self.item.coerce_to(target.type, env) coerced_item = self.item.coerce_to(target.type, env)
self.interned_items.append((name, target, coerced_item)) self.interned_items.append((name, target, coerced_item))
if self.interned_items:
env.use_utility_code(raise_import_error_utility_code)
return self return self
def generate_execution_code(self, code): def generate_execution_code(self, code):
...@@ -6778,21 +6776,16 @@ class FromImportStatNode(StatNode): ...@@ -6778,21 +6776,16 @@ class FromImportStatNode(StatNode):
code.error_goto(self.pos))) code.error_goto(self.pos)))
item_temp = code.funcstate.allocate_temp(py_object_type, manage_ref=True) item_temp = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
self.item.set_cname(item_temp) self.item.set_cname(item_temp)
code.globalstate.use_utility_code( if self.interned_items:
UtilityCode.load_cached("PyObjectGetAttrStr", "ObjectHandling.c")) code.globalstate.use_utility_code(
UtilityCode.load_cached("ImportFrom", "ImportExport.c"))
for name, target, coerced_item in self.interned_items: for name, target, coerced_item in self.interned_items:
cname = code.intern_identifier(name)
code.putln( code.putln(
'%s = __Pyx_PyObject_GetAttrStr(%s, %s);' % ( '%s = __Pyx_ImportFrom(%s, %s); %s' % (
item_temp, item_temp,
self.module.py_result(), self.module.py_result(),
cname)) code.intern_identifier(name),
code.putln('if (%s == NULL) {' % item_temp) code.error_goto_if_null(item_temp, self.pos)))
code.putln(
'if (PyErr_ExceptionMatches(PyExc_AttributeError)) '
'__Pyx_RaiseImportError(%s);' % cname)
code.putln(code.error_goto_if_null(item_temp, self.pos))
code.putln('}')
code.put_gotref(item_temp) code.put_gotref(item_temp)
if coerced_item is None: if coerced_item is None:
target.generate_assignment_code(self.item, code) target.generate_assignment_code(self.item, code)
...@@ -8325,20 +8318,3 @@ init=""" ...@@ -8325,20 +8318,3 @@ init="""
a quiet NaN. */ a quiet NaN. */
memset(&%(PYX_NAN)s, 0xFF, sizeof(%(PYX_NAN)s)); memset(&%(PYX_NAN)s, 0xFF, sizeof(%(PYX_NAN)s));
""" % vars(Naming)) """ % vars(Naming))
#------------------------------------------------------------------------------------
raise_import_error_utility_code = UtilityCode(
proto = '''
static CYTHON_INLINE void __Pyx_RaiseImportError(PyObject *name);
''',
impl = '''
static CYTHON_INLINE void __Pyx_RaiseImportError(PyObject *name) {
#if PY_MAJOR_VERSION < 3
PyErr_Format(PyExc_ImportError, "cannot import name %.230s",
PyString_AsString(name));
#else
PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
#endif
}
''')
...@@ -100,6 +100,32 @@ bad: ...@@ -100,6 +100,32 @@ bad:
return module; return module;
} }
/////////////// ImportFrom.proto ///////////////
static PyObject* __Pyx_ImportFrom(PyObject* module, PyObject* name); /*proto*/
/////////////// ImportFrom ///////////////
//@requires: ObjectHandling.c::PyObjectGetAttrStr
static PyObject* __Pyx_ImportFrom(PyObject* module, PyObject* name) {
PyObject* value = __Pyx_PyObject_GetAttrStr(module, name);
if (unlikely(!value) && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Format(PyExc_ImportError,
#if PY_MAJOR_VERSION < 3
"cannot import name %.230s", PyString_AS_STRING(name));
#else
"cannot import name %S", name);
#endif
}
return value;
}
static CYTHON_INLINE void __Pyx_RaiseImportError(PyObject *name) {
}
/////////////// ModuleImport.proto /////////////// /////////////// ModuleImport.proto ///////////////
static PyObject *__Pyx_ImportModule(const char *name); /*proto*/ static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
......
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