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):
# packages require __path__, so all we can do is try to figure
# out the module path at runtime by rerunning the import lookup
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(
"SetPackagePathFromImportLib", "ImportExport.c"))
code.putln(code.error_goto_if_neg(
'__Pyx_SetPackagePathFromImportLib(%s, %s)' % (
parent_name,
'__Pyx_SetPackagePathFromImportLib(%s)' % (
code.globalstate.get_py_string_const(
EncodedString(env.module_name)).cname),
self.full_module_name).cname),
self.pos))
code.putln("}")
......
......@@ -228,9 +228,9 @@ bad:
// PY_VERSION_HEX >= 0x03030000
#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
#define __Pyx_SetPackagePathFromImportLib(a, b) 0
#define __Pyx_SetPackagePathFromImportLib(a) 0
#endif
/////////////// SetPackagePathFromImportLib ///////////////
......@@ -239,32 +239,21 @@ static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, Py
// PY_VERSION_HEX >= 0x03030000
#if PY_MAJOR_VERSION >= 3 && !CYTHON_PEP489_MULTI_PHASE_INIT
static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, PyObject *module_name) {
PyObject *importlib, *loader, *osmod, *ossep, *parts, *package_path;
PyObject *path = NULL, *file_path = NULL;
static int __Pyx_SetPackagePathFromImportLib(PyObject *module_name) {
PyObject *importlib, *osmod, *ossep, *parts, *package_path;
PyObject *file_path = NULL;
int result;
if (parent_package_name) {
PyObject *package = PyImport_ImportModule(parent_package_name);
if (unlikely(!package))
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");
PyObject *spec;
// package_path = [importlib.util.find_spec(module_name).origin.rsplit(os.sep, 1)[0]]
importlib = PyImport_ImportModule("importlib.util");
if (unlikely(!importlib))
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(path); path = NULL;
if (unlikely(!loader))
if (unlikely(!spec))
goto bad;
file_path = PyObject_GetAttrString(loader, "path");
Py_DECREF(loader);
file_path = PyObject_GetAttrString(spec, "origin");
Py_DECREF(spec);
if (unlikely(!file_path))
goto bad;
......@@ -291,7 +280,6 @@ static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, Py
bad:
PyErr_WriteUnraisable(module_name);
Py_XDECREF(path);
Py_XDECREF(file_path);
// 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