Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0

Added a new API function PyImport_ImportModuleNoBlock. It solves the problem with dead locks when mixing threads and imports
This commit is contained in:
Christian Heimes 2008-01-03 22:16:32 +00:00
parent 0a83d79268
commit 000a074c95
19 changed files with 135 additions and 63 deletions

View file

@ -183,7 +183,8 @@ Importing Modules
single: __all__ (package variable)
This is a simplified interface to :cfunc:`PyImport_ImportModuleEx` below,
leaving the *globals* and *locals* arguments set to *NULL*. When the *name*
leaving the *globals* and *locals* arguments set to *NULL* and *level* set
to 0. When the *name*
argument contains a dot (when it specifies a submodule of a package), the
*fromlist* argument is set to the list ``['*']`` so that the return value is the
named module rather than the top-level package containing it as would otherwise
@ -198,9 +199,28 @@ Importing Modules
.. versionchanged:: 2.4
failing imports remove incomplete module objects.
.. versionchanged:: 2.6
always use absolute imports
.. index:: single: modules (in module sys)
.. cfunction:: PyObject* PyImport_ImportModuleNoBlock(const char *name)
.. index::
single: `cfunc:PyImport_ImportModule`
This version of `cfunc:PyImport_ImportModule` does not block. It's intended
to be used in C function which import other modules to execute a function.
The import may block if another thread holds the import lock. The function
`cfunc:PyImport_ImportModuleNoBlock` doesn't block. It first tries to fetch
the module from sys.modules and falls back to `cfunc:PyImport_ImportModule`
unless the the lock is hold. In the latter case the function raises an
ImportError.
.. versionadded:: 2.6
.. cfunction:: PyObject* PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist)
.. index:: builtin: __import__
@ -218,6 +238,24 @@ Importing Modules
.. versionchanged:: 2.4
failing imports remove incomplete module objects.
.. versionchanged:: 2.6
The function is an alias for `cfunc:PyImport_ImportModuleLevel` with
-1 as level, meaning relative import.
.. cfunction:: PyObject* PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
Import a module. This is best described by referring to the built-in Python
function :func:`__import__`, as the standard :func:`__import__` function calls
this function directly.
The return value is a new reference to the imported module or top-level package,
or *NULL* with an exception set on failure. Like for :func:`__import__`,
the return value when a submodule of a package was requested is normally the
top-level package, unless a non-empty *fromlist* was given.
..versionadded:: 2.5
.. cfunction:: PyObject* PyImport_Import(PyObject *name)
@ -230,6 +268,9 @@ Importing Modules
current globals. This means that the import is done using whatever import hooks
are installed in the current environment, e.g. by :mod:`rexec` or :mod:`ihooks`.
.. versionchanged:: 2.6
always use absolute imports
.. cfunction:: PyObject* PyImport_ReloadModule(PyObject *m)