Commit a35d94f8 authored by Stefan Behnel's avatar Stefan Behnel

in Py3.3, use new PyImport_ImportModuleLevelObject() C-API function instead of...

in Py3.3, use new PyImport_ImportModuleLevelObject() C-API function instead of equivalent Python call to __import__()
parent 3fceadcd
...@@ -17,15 +17,17 @@ static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level); ...@@ -17,15 +17,17 @@ static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level);
//@substitute: naming //@substitute: naming
static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level) { static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level) {
PyObject *py_import = 0;
PyObject *empty_list = 0; PyObject *empty_list = 0;
PyObject *module = 0; PyObject *module = 0;
PyObject *global_dict = 0; PyObject *global_dict = 0;
PyObject *empty_dict = 0; PyObject *empty_dict = 0;
PyObject *list; PyObject *list;
#if PY_VERSION_HEX < 0x03030000
PyObject *py_import = 0;
py_import = __Pyx_GetAttrString($builtins_cname, "__import__"); py_import = __Pyx_GetAttrString($builtins_cname, "__import__");
if (!py_import) if (!py_import)
goto bad; goto bad;
#endif
if (from_list) if (from_list)
list = from_list; list = from_list;
else { else {
...@@ -46,12 +48,17 @@ static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level) { ...@@ -46,12 +48,17 @@ static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level) {
if (level == -1) { if (level == -1) {
if (strchr(__Pyx_MODULE_NAME, '.')) { if (strchr(__Pyx_MODULE_NAME, '.')) {
/* try package relative import first */ /* try package relative import first */
#if PY_VERSION_HEX < 0x03030000
PyObject *py_level = PyInt_FromLong(1); PyObject *py_level = PyInt_FromLong(1);
if (!py_level) if (!py_level)
goto bad; goto bad;
module = PyObject_CallFunctionObjArgs(py_import, module = PyObject_CallFunctionObjArgs(py_import,
name, global_dict, empty_dict, list, py_level, NULL); name, global_dict, empty_dict, list, py_level, NULL);
Py_DECREF(py_level); Py_DECREF(py_level);
#else
module = PyImport_ImportModuleLevelObject(
name, global_dict, empty_dict, list, 1);
#endif
if (!module) { if (!module) {
if (!PyErr_ExceptionMatches(PyExc_ImportError)) if (!PyErr_ExceptionMatches(PyExc_ImportError))
goto bad; goto bad;
...@@ -62,12 +69,17 @@ static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level) { ...@@ -62,12 +69,17 @@ static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level) {
} }
#endif #endif
if (!module) { if (!module) {
#if PY_VERSION_HEX < 0x03030000
PyObject *py_level = PyInt_FromLong(level); PyObject *py_level = PyInt_FromLong(level);
if (!py_level) if (!py_level)
goto bad; goto bad;
module = PyObject_CallFunctionObjArgs(py_import, module = PyObject_CallFunctionObjArgs(py_import,
name, global_dict, empty_dict, list, py_level, NULL); name, global_dict, empty_dict, list, py_level, NULL);
Py_DECREF(py_level); Py_DECREF(py_level);
#else
module = PyImport_ImportModuleLevelObject(
name, global_dict, empty_dict, list, level);
#endif
} }
} }
#else #else
...@@ -79,8 +91,10 @@ static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level) { ...@@ -79,8 +91,10 @@ static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level) {
name, global_dict, empty_dict, list, NULL); name, global_dict, empty_dict, list, NULL);
#endif #endif
bad: bad:
Py_XDECREF(empty_list); #if PY_VERSION_HEX < 0x03030000
Py_XDECREF(py_import); Py_XDECREF(py_import);
#endif
Py_XDECREF(empty_list);
Py_XDECREF(empty_dict); Py_XDECREF(empty_dict);
return module; return module;
} }
......
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