diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2015-03-30 20:23:37 +0200 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2015-03-30 20:23:37 +0200 |
commit | c960580ffecf0280414d252761d740954aee15e0 (patch) | |
tree | 53c590f18c94af52db5e17ec36f7745c017dc8a9 /Cython/Compiler/ModuleNode.py | |
parent | a3447eb8ed9b835efc70892f5daffffbf7948b9a (diff) | |
download | cython-c960580ffecf0280414d252761d740954aee15e0.tar.gz |
move "import *" utility code from ModuleNode.py to ImportExport.c file
Diffstat (limited to 'Cython/Compiler/ModuleNode.py')
-rw-r--r-- | Cython/Compiler/ModuleNode.py | 123 |
1 files changed, 3 insertions, 120 deletions
diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 6eb9e9612..c19ac7d83 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -2024,8 +2024,9 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code.putln("bad:") code.putln("return -1;") code.putln("}") - code.putln(import_star_utility_code) - code.exit_cfunc_scope() # done with labels + code.putln("") + code.putln(UtilityCode.load_cached("ImportStar", "ImportExport.c").impl) + code.exit_cfunc_scope() # done with labels def generate_module_init_func(self, imported_modules, env, code): code.enter_cfunc_scope() @@ -2823,124 +2824,6 @@ static CYTHON_INLINE int __Pyx_StrEq(const char *s1, const char *s2) { #------------------------------------------------------------------------------------ -import_star_utility_code = """ - -/* import_all_from is an unexposed function from ceval.c */ - -static int -__Pyx_import_all_from(PyObject *locals, PyObject *v) -{ - PyObject *all = PyObject_GetAttrString(v, "__all__"); - PyObject *dict, *name, *value; - int skip_leading_underscores = 0; - int pos, err; - - if (all == NULL) { - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) - return -1; /* Unexpected error */ - PyErr_Clear(); - dict = PyObject_GetAttrString(v, "__dict__"); - if (dict == NULL) { - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) - return -1; - PyErr_SetString(PyExc_ImportError, - "from-import-* object has no __dict__ and no __all__"); - return -1; - } -#if PY_MAJOR_VERSION < 3 - all = PyObject_CallMethod(dict, (char *)"keys", NULL); -#else - all = PyMapping_Keys(dict); -#endif - Py_DECREF(dict); - if (all == NULL) - return -1; - skip_leading_underscores = 1; - } - - for (pos = 0, err = 0; ; pos++) { - name = PySequence_GetItem(all, pos); - if (name == NULL) { - if (!PyErr_ExceptionMatches(PyExc_IndexError)) - err = -1; - else - PyErr_Clear(); - break; - } - if (skip_leading_underscores && -#if PY_MAJOR_VERSION < 3 - PyString_Check(name) && - PyString_AS_STRING(name)[0] == '_') -#else - PyUnicode_Check(name) && - PyUnicode_AS_UNICODE(name)[0] == '_') -#endif - { - Py_DECREF(name); - continue; - } - value = PyObject_GetAttr(v, name); - if (value == NULL) - err = -1; - else if (PyDict_CheckExact(locals)) - err = PyDict_SetItem(locals, name, value); - else - err = PyObject_SetItem(locals, name, value); - Py_DECREF(name); - Py_XDECREF(value); - if (err != 0) - break; - } - Py_DECREF(all); - return err; -} - - -static int %(IMPORT_STAR)s(PyObject* m) { - - int i; - int ret = -1; - char* s; - PyObject *locals = 0; - PyObject *list = 0; -#if PY_MAJOR_VERSION >= 3 - PyObject *utf8_name = 0; -#endif - PyObject *name; - PyObject *item; - - locals = PyDict_New(); if (!locals) goto bad; - if (__Pyx_import_all_from(locals, m) < 0) goto bad; - list = PyDict_Items(locals); if (!list) goto bad; - - for(i=0; i<PyList_GET_SIZE(list); i++) { - name = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 0); - item = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 1); -#if PY_MAJOR_VERSION >= 3 - utf8_name = PyUnicode_AsUTF8String(name); - if (!utf8_name) goto bad; - s = PyBytes_AS_STRING(utf8_name); - if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad; - Py_DECREF(utf8_name); utf8_name = 0; -#else - s = PyString_AsString(name); - if (!s) goto bad; - if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad; -#endif - } - ret = 0; - -bad: - Py_XDECREF(locals); - Py_XDECREF(list); -#if PY_MAJOR_VERSION >= 3 - Py_XDECREF(utf8_name); -#endif - return ret; -} -""" % {'IMPORT_STAR': Naming.import_star, - 'IMPORT_STAR_SET': Naming.import_star_set} - refnanny_utility_code = UtilityCode.load("Refnanny", "ModuleSetupCode.c") packed_struct_utility_code = UtilityCode(proto=""" |