Commit 9a151cb7 authored by David Gobbi's avatar David Gobbi Committed by Ben Boeckel
Browse files

Allow Python dependencies to give full path

When a module is initialized, the 'depends' list for that module
is now allowed to include the absolute path to each dependency,
e.g. 'vtkmodule.vtkCommonCorePython'.
parent ebf3ea08
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "vtkWeakPointer.h" #include "vtkWeakPointer.h"
#include "vtkWindows.h" #include "vtkWindows.h"
#include <cstring>
#include <sstream> #include <sstream>
#include <map> #include <map>
#include <vector> #include <vector>
...@@ -921,8 +922,19 @@ PyTypeObject *vtkPythonUtil::FindSpecialTypeObject(const char *name) ...@@ -921,8 +922,19 @@ PyTypeObject *vtkPythonUtil::FindSpecialTypeObject(const char *name)
} }
//-------------------------------------------------------------------- //--------------------------------------------------------------------
bool vtkPythonUtil::ImportModule(const char *name, PyObject *globals) bool vtkPythonUtil::ImportModule(const char *fullname, PyObject *globals)
{ {
// strip all but the final part of the path
const char *name = std::strrchr(fullname, '.');
if (name == nullptr)
{
name = fullname;
}
else if (name[0] == '.')
{
name++;
}
// check whether the module is already loaded // check whether the module is already loaded
if (vtkPythonMap) if (vtkPythonMap)
{ {
...@@ -933,15 +945,23 @@ bool vtkPythonUtil::ImportModule(const char *name, PyObject *globals) ...@@ -933,15 +945,23 @@ bool vtkPythonUtil::ImportModule(const char *name, PyObject *globals)
} }
} }
// try relative import (const-cast is needed for Python 2.x only) PyObject *m = nullptr;
PyObject *m = PyImport_ImportModuleLevel(const_cast<char *>(name), globals,
nullptr, nullptr, 1); if (fullname == name || (fullname[0] == '.' && &fullname[1] == name))
{
// try relative import (const-cast is needed for Python 2.x only)
m = PyImport_ImportModuleLevel(const_cast<char *>(name), globals,
nullptr, nullptr, 1);
if (!m)
{
PyErr_Clear();
}
}
if (!m) if (!m)
{ {
// clear error and retry with absolute import // try absolute import
PyErr_Clear(); m = PyImport_ImportModule(fullname);
m = PyImport_ImportModule(name);
} }
if (!m) if (!m)
......
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