Skip to content

Commit c79a9ea

Browse files
usiemsjcfr
authored andcommitted
[Backport] Update __spec__ too (if existing) when setting the import path
(cherry picked from commit MeVisLab/pythonqt@8e69c65)
1 parent c1d49dc commit c79a9ea

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/PythonQt.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,20 @@ void PythonQt::overwriteSysPath(const QStringList& paths)
16901690
void PythonQt::setModuleImportPath(PyObject* module, const QStringList& paths)
16911691
{
16921692
PyModule_AddObject(module, "__path__", PythonQtConv::QStringListToPyList(paths));
1693+
#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4)
1694+
// Since Python 3.4 a module has a __spec__ member of type ModuleSpec which
1695+
// mirrors some module-specific attributes, see
1696+
// https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec .
1697+
//
1698+
// Python 3.9 prints an import warning "__package__ != __spec__.parent" on relative imports
1699+
// if we don't set the submodule_search_locations on __spec__ (this indirectly changes
1700+
// the value of parent)
1701+
PyObject* spec = PyObject_GetAttrString(module, "__spec__");
1702+
if (spec) {
1703+
PythonQt::self()->addVariable(spec, "submodule_search_locations", paths);
1704+
Py_DECREF(spec);
1705+
}
1706+
#endif
16931707
}
16941708

16951709
void PythonQt::stdOutRedirectCB(const QString& str)

0 commit comments

Comments
 (0)