From 3fceadcd21cec96ad41bf4eb984ae1c8e19347d7 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sat, 3 Nov 2012 21:40:01 +0100 Subject: [PATCH] moved helper function __Pyx_Import() into ImportExport.c utility code file --- Cython/Compiler/ExprNodes.py | 82 +------------------------------------------ Cython/Compiler/FusedNode.py | 2 +- Cython/Utility/ImportExport.c | 77 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 82 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 31de95c..16a26a2 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1948,7 +1948,7 @@ class ImportNode(ExprNode): self.name_list.analyse_types(env) self.name_list.coerce_to_pyobject(env) self.is_temp = 1 - env.use_utility_code(import_utility_code) + env.use_utility_code(UtilityCode.load_cached("Import", "ImportExport.c")) gil_message = "Python import" @@ -9901,86 +9901,6 @@ static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) { #------------------------------------------------------------------------------------ -import_utility_code = UtilityCode( -proto = """ -static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level); /*proto*/ -""", -impl = """ -static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level) { - PyObject *py_import = 0; - PyObject *empty_list = 0; - PyObject *module = 0; - PyObject *global_dict = 0; - PyObject *empty_dict = 0; - PyObject *list; - py_import = __Pyx_GetAttrString(%(BUILTINS)s, "__import__"); - if (!py_import) - goto bad; - if (from_list) - list = from_list; - else { - empty_list = PyList_New(0); - if (!empty_list) - goto bad; - list = empty_list; - } - global_dict = PyModule_GetDict(%(GLOBALS)s); - if (!global_dict) - goto bad; - empty_dict = PyDict_New(); - if (!empty_dict) - goto bad; - #if PY_VERSION_HEX >= 0x02050000 - { - #if PY_MAJOR_VERSION >= 3 - if (level == -1) { - if (strchr(__Pyx_MODULE_NAME, '.')) { - /* try package relative import first */ - PyObject *py_level = PyInt_FromLong(1); - if (!py_level) - goto bad; - module = PyObject_CallFunctionObjArgs(py_import, - name, global_dict, empty_dict, list, py_level, NULL); - Py_DECREF(py_level); - if (!module) { - if (!PyErr_ExceptionMatches(PyExc_ImportError)) - goto bad; - PyErr_Clear(); - } - } - level = 0; /* try absolute import on failure */ - } - #endif - if (!module) { - PyObject *py_level = PyInt_FromLong(level); - if (!py_level) - goto bad; - module = PyObject_CallFunctionObjArgs(py_import, - name, global_dict, empty_dict, list, py_level, NULL); - Py_DECREF(py_level); - } - } - #else - if (level>0) { - PyErr_SetString(PyExc_RuntimeError, "Relative import is not supported for Python <=2.4."); - goto bad; - } - module = PyObject_CallFunctionObjArgs(py_import, - name, global_dict, empty_dict, list, NULL); - #endif -bad: - Py_XDECREF(empty_list); - Py_XDECREF(py_import); - Py_XDECREF(empty_dict); - return module; -} -""" % { - "BUILTINS": Naming.builtins_cname, - "GLOBALS": Naming.module_cname, -}) - -#------------------------------------------------------------------------------------ - pyerr_occurred_withgil_utility_code= UtilityCode( proto = """ static CYTHON_INLINE int __Pyx_ErrOccurredWithGIL(void); /* proto */ diff --git a/Cython/Compiler/FusedNode.py b/Cython/Compiler/FusedNode.py index 44dee90..af5913a 100644 --- a/Cython/Compiler/FusedNode.py +++ b/Cython/Compiler/FusedNode.py @@ -580,7 +580,7 @@ class FusedCFuncDefNode(StatListNode): if all_buffer_types: self._buffer_declarations(pyx_code, decl_code, all_buffer_types) - env.use_utility_code(ExprNodes.import_utility_code) + env.use_utility_code(Code.UtilityCode.load_cached("Import", "ImportExport.c")) pyx_code.put_chunk( u""" diff --git a/Cython/Utility/ImportExport.c b/Cython/Utility/ImportExport.c index 304bf75..796c61d 100644 --- a/Cython/Utility/ImportExport.c +++ b/Cython/Utility/ImportExport.c @@ -8,6 +8,83 @@ #endif #endif + +/////////////// Import.proto /////////////// + +static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level); /*proto*/ + +/////////////// Import /////////////// +//@substitute: naming + +static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level) { + PyObject *py_import = 0; + PyObject *empty_list = 0; + PyObject *module = 0; + PyObject *global_dict = 0; + PyObject *empty_dict = 0; + PyObject *list; + py_import = __Pyx_GetAttrString($builtins_cname, "__import__"); + if (!py_import) + goto bad; + if (from_list) + list = from_list; + else { + empty_list = PyList_New(0); + if (!empty_list) + goto bad; + list = empty_list; + } + global_dict = PyModule_GetDict($module_cname); + if (!global_dict) + goto bad; + empty_dict = PyDict_New(); + if (!empty_dict) + goto bad; + #if PY_VERSION_HEX >= 0x02050000 + { + #if PY_MAJOR_VERSION >= 3 + if (level == -1) { + if (strchr(__Pyx_MODULE_NAME, '.')) { + /* try package relative import first */ + PyObject *py_level = PyInt_FromLong(1); + if (!py_level) + goto bad; + module = PyObject_CallFunctionObjArgs(py_import, + name, global_dict, empty_dict, list, py_level, NULL); + Py_DECREF(py_level); + if (!module) { + if (!PyErr_ExceptionMatches(PyExc_ImportError)) + goto bad; + PyErr_Clear(); + } + } + level = 0; /* try absolute import on failure */ + } + #endif + if (!module) { + PyObject *py_level = PyInt_FromLong(level); + if (!py_level) + goto bad; + module = PyObject_CallFunctionObjArgs(py_import, + name, global_dict, empty_dict, list, py_level, NULL); + Py_DECREF(py_level); + } + } + #else + if (level>0) { + PyErr_SetString(PyExc_RuntimeError, "Relative import is not supported for Python <=2.4."); + goto bad; + } + module = PyObject_CallFunctionObjArgs(py_import, + name, global_dict, empty_dict, list, NULL); + #endif +bad: + Py_XDECREF(empty_list); + Py_XDECREF(py_import); + Py_XDECREF(empty_dict); + return module; +} + /////////////// ModuleImport.proto /////////////// static PyObject *__Pyx_ImportModule(const char *name); /*proto*/ -- 2.7.4