Commit aac58502 authored by Matti Picus's avatar Matti Picus Committed by Stefan Behnel

Move from deprecated find_loader() to find_spec() for setting up the import...

Move from deprecated find_loader() to find_spec() for setting up the import time package path (GH-4764)

Closes https://github.com/cython/cython/issues/4763
parent 59e0f890
...@@ -2628,18 +2628,12 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -2628,18 +2628,12 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
# packages require __path__, so all we can do is try to figure # packages require __path__, so all we can do is try to figure
# out the module path at runtime by rerunning the import lookup # out the module path at runtime by rerunning the import lookup
code.putln("if (!CYTHON_PEP489_MULTI_PHASE_INIT) {") code.putln("if (!CYTHON_PEP489_MULTI_PHASE_INIT) {")
package_name, _ = self.full_module_name.rsplit('.', 1)
if '.' in package_name:
parent_name = '"%s"' % (package_name.rsplit('.', 1)[0],)
else:
parent_name = 'NULL'
code.globalstate.use_utility_code(UtilityCode.load( code.globalstate.use_utility_code(UtilityCode.load(
"SetPackagePathFromImportLib", "ImportExport.c")) "SetPackagePathFromImportLib", "ImportExport.c"))
code.putln(code.error_goto_if_neg( code.putln(code.error_goto_if_neg(
'__Pyx_SetPackagePathFromImportLib(%s, %s)' % ( '__Pyx_SetPackagePathFromImportLib(%s)' % (
parent_name,
code.globalstate.get_py_string_const( code.globalstate.get_py_string_const(
EncodedString(env.module_name)).cname), self.full_module_name).cname),
self.pos)) self.pos))
code.putln("}") code.putln("}")
......
...@@ -228,9 +228,9 @@ bad: ...@@ -228,9 +228,9 @@ bad:
// PY_VERSION_HEX >= 0x03030000 // PY_VERSION_HEX >= 0x03030000
#if PY_MAJOR_VERSION >= 3 && !CYTHON_PEP489_MULTI_PHASE_INIT #if PY_MAJOR_VERSION >= 3 && !CYTHON_PEP489_MULTI_PHASE_INIT
static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, PyObject *module_name); static int __Pyx_SetPackagePathFromImportLib(PyObject *module_name);
#else #else
#define __Pyx_SetPackagePathFromImportLib(a, b) 0 #define __Pyx_SetPackagePathFromImportLib(a) 0
#endif #endif
/////////////// SetPackagePathFromImportLib /////////////// /////////////// SetPackagePathFromImportLib ///////////////
...@@ -239,32 +239,21 @@ static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, Py ...@@ -239,32 +239,21 @@ static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, Py
// PY_VERSION_HEX >= 0x03030000 // PY_VERSION_HEX >= 0x03030000
#if PY_MAJOR_VERSION >= 3 && !CYTHON_PEP489_MULTI_PHASE_INIT #if PY_MAJOR_VERSION >= 3 && !CYTHON_PEP489_MULTI_PHASE_INIT
static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, PyObject *module_name) { static int __Pyx_SetPackagePathFromImportLib(PyObject *module_name) {
PyObject *importlib, *loader, *osmod, *ossep, *parts, *package_path; PyObject *importlib, *osmod, *ossep, *parts, *package_path;
PyObject *path = NULL, *file_path = NULL; PyObject *file_path = NULL;
int result; int result;
if (parent_package_name) { PyObject *spec;
PyObject *package = PyImport_ImportModule(parent_package_name); // package_path = [importlib.util.find_spec(module_name).origin.rsplit(os.sep, 1)[0]]
if (unlikely(!package)) importlib = PyImport_ImportModule("importlib.util");
goto bad;
path = PyObject_GetAttrString(package, "__path__");
Py_DECREF(package);
if (unlikely(!path) || unlikely(path == Py_None))
goto bad;
} else {
path = Py_None; Py_INCREF(Py_None);
}
// package_path = [importlib.find_loader(module_name, path).path.rsplit(os.sep, 1)[0]]
importlib = PyImport_ImportModule("importlib");
if (unlikely(!importlib)) if (unlikely(!importlib))
goto bad; goto bad;
loader = PyObject_CallMethod(importlib, "find_loader", "(OO)", module_name, path); spec = PyObject_CallMethod(importlib, "find_spec", "(O)", module_name);
Py_DECREF(importlib); Py_DECREF(importlib);
Py_DECREF(path); path = NULL; if (unlikely(!spec))
if (unlikely(!loader))
goto bad; goto bad;
file_path = PyObject_GetAttrString(loader, "path"); file_path = PyObject_GetAttrString(spec, "origin");
Py_DECREF(loader); Py_DECREF(spec);
if (unlikely(!file_path)) if (unlikely(!file_path))
goto bad; goto bad;
...@@ -291,7 +280,6 @@ static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, Py ...@@ -291,7 +280,6 @@ static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, Py
bad: bad:
PyErr_WriteUnraisable(module_name); PyErr_WriteUnraisable(module_name);
Py_XDECREF(path);
Py_XDECREF(file_path); Py_XDECREF(file_path);
// set an empty path list on failure // set an empty path list on failure
......
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