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

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 @@
#include "vtkWeakPointer.h"
#include "vtkWindows.h"
#include <cstring>
#include <sstream>
#include <map>
#include <vector>
......@@ -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
if (vtkPythonMap)
{
......@@ -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 = PyImport_ImportModuleLevel(const_cast<char *>(name), globals,
nullptr, nullptr, 1);
PyObject *m = nullptr;
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)
{
// clear error and retry with absolute import
PyErr_Clear();
m = PyImport_ImportModule(name);
// try absolute import
m = PyImport_ImportModule(fullname);
}
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