cpython: 80f4bd647695 (original) (raw)
Mercurial > cpython
changeset 68739:80f4bd647695
Issue #3080: Add PyImport_ImportModuleLevelObject() function Use it for the builtin __import__ function. [#3080]
Victor Stinner victor.stinner@haypocalc.com | |
---|---|
date | Mon, 14 Mar 2011 15:54:52 -0400 |
parents | c4361bab6914 |
children | cc7c0f6f60bf |
files | Doc/c-api/import.rst Include/import.h Python/bltinmodule.c Python/import.c |
diffstat | 4 files changed, 41 insertions(+), 18 deletions(-)[+] [-] Doc/c-api/import.rst 9 Include/import.h 7 Python/bltinmodule.c 11 Python/import.c 32 |
line wrap: on
line diff
--- a/Doc/c-api/import.rst
+++ b/Doc/c-api/import.rst
@@ -57,7 +57,7 @@ Importing Modules
:c:func:PyImport_ImportModule
.
-.. c:function:: PyObject* PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, PyObject fromlist, int level)
+.. c:function:: PyObject PyImport_ImportModuleLevelObject(PyObject *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
@@ -68,6 +68,13 @@ Importing Modules
the return value when a submodule of a package was requested is normally the
top-level package, unless a non-empty fromlist was given.
+ +.. c:function:: PyObject* PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level) +
- Similar to :c:func:
PyImport_ImportModuleLevelObject
, but the name is an - UTF-8 encoded string instead of a Unicode object.
.. c:function:: PyObject* PyImport_Import(PyObject *name)
--- a/Include/import.h +++ b/Include/import.h @@ -50,6 +50,13 @@ PyAPI_FUNC(PyObject *) PyImport_ImportMo PyObject *fromlist, int level ); +PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevelObject(
#define PyImport_ImportModuleEx(n, g, l, f) [](#l2.15) PyImport_ImportModuleLevel(n, g, l, f, -1)
--- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -155,17 +155,14 @@ builtin___import__(PyObject *self, PyObj { static char *kwlist[] = {"name", "globals", "locals", "fromlist", "level", 0};
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:import", kwlist, &name, &globals, &locals, &fromlist, &level)) return NULL;
--- a/Python/import.c +++ b/Python/import.c @@ -2753,25 +2753,37 @@ import_module_level(PyObject *name, PyOb } PyObject * -PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
PyObject *fromlist, int level)[](#l4.8)
+PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
PyObject *locals, PyObject *fromlist,[](#l4.10)
int level)[](#l4.11)
- PyObject *nameobj, *result;
- nameobj = PyUnicode_FromString(name);
- if (nameobj == NULL)
return NULL;[](#l4.16)
- mod = import_module_level(name, globals, locals, fromlist, level); if (_PyImport_ReleaseLock() < 0) {
Py_XDECREF(result);[](#l4.23)
}Py_XDECREF(mod);[](#l4.24) PyErr_SetString(PyExc_RuntimeError,[](#l4.25) "not holding the import lock");[](#l4.26) return NULL;[](#l4.27)
} +PyObject * +PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
PyObject *fromlist, int level)[](#l4.35)
- PyObject *nameobj, *mod;
- nameobj = PyUnicode_FromString(name);
- if (nameobj == NULL)
return NULL;[](#l4.40)
- mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
fromlist, level);[](#l4.42)
- Py_DECREF(nameobj);
- return mod;
+} + + /* Return the package that an import is being performed in. If globals comes from the module foo.bar.bat (not itself a package), this returns the sys.modules entry for foo.bar. If globals is from a package's init.py,