Skip to content

Commit fe93faf

Browse files
author
Victor Stinner
committed
Issue python#3080: Add PyImport_ImportModuleLevelObject() function
Use it for the builtin __import__ function.
1 parent 98dbba5 commit fe93faf

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

Doc/c-api/import.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Importing Modules
5757
:c:func:`PyImport_ImportModule`.
5858
5959
60-
.. c:function:: PyObject* PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
60+
.. c:function:: PyObject* PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
6161
6262
Import a module. This is best described by referring to the built-in Python
6363
function :func:`__import__`, as the standard :func:`__import__` function calls
@@ -68,6 +68,13 @@ Importing Modules
6868
the return value when a submodule of a package was requested is normally the
6969
top-level package, unless a non-empty *fromlist* was given.
7070
71+
.. versionadded:: 3.3
72+
73+
74+
.. c:function:: PyObject* PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
75+
76+
Similar to :c:func:`PyImport_ImportModuleLevelObject`, but the name is an
77+
UTF-8 encoded string instead of a Unicode object.
7178
7279
.. c:function:: PyObject* PyImport_Import(PyObject *name)
7380

Include/import.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevel(
5050
PyObject *fromlist,
5151
int level
5252
);
53+
PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevelObject(
54+
PyObject *name,
55+
PyObject *globals,
56+
PyObject *locals,
57+
PyObject *fromlist,
58+
int level
59+
);
5360

5461
#define PyImport_ImportModuleEx(n, g, l, f) \
5562
PyImport_ImportModuleLevel(n, g, l, f, -1)

Python/bltinmodule.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,14 @@ builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
155155
{
156156
static char *kwlist[] = {"name", "globals", "locals", "fromlist",
157157
"level", 0};
158-
char *name;
159-
PyObject *globals = NULL;
160-
PyObject *locals = NULL;
161-
PyObject *fromlist = NULL;
158+
PyObject *name, *globals = NULL, *locals = NULL, *fromlist = NULL;
162159
int level = -1;
163160

164-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
161+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:__import__",
165162
kwlist, &name, &globals, &locals, &fromlist, &level))
166163
return NULL;
167-
return PyImport_ImportModuleLevel(name, globals, locals,
168-
fromlist, level);
164+
return PyImport_ImportModuleLevelObject(name, globals, locals,
165+
fromlist, level);
169166
}
170167

171168
PyDoc_STRVAR(import_doc,

Python/import.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,25 +2753,37 @@ import_module_level(PyObject *name, PyObject *globals, PyObject *locals,
27532753
}
27542754

27552755
PyObject *
2756-
PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2757-
PyObject *fromlist, int level)
2756+
PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
2757+
PyObject *locals, PyObject *fromlist,
2758+
int level)
27582759
{
2759-
PyObject *nameobj, *result;
2760-
nameobj = PyUnicode_FromString(name);
2761-
if (nameobj == NULL)
2762-
return NULL;
2760+
PyObject *mod;
27632761
_PyImport_AcquireLock();
2764-
result = import_module_level(nameobj, globals, locals, fromlist, level);
2765-
Py_DECREF(nameobj);
2762+
mod = import_module_level(name, globals, locals, fromlist, level);
27662763
if (_PyImport_ReleaseLock() < 0) {
2767-
Py_XDECREF(result);
2764+
Py_XDECREF(mod);
27682765
PyErr_SetString(PyExc_RuntimeError,
27692766
"not holding the import lock");
27702767
return NULL;
27712768
}
2772-
return result;
2769+
return mod;
2770+
}
2771+
2772+
PyObject *
2773+
PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
2774+
PyObject *fromlist, int level)
2775+
{
2776+
PyObject *nameobj, *mod;
2777+
nameobj = PyUnicode_FromString(name);
2778+
if (nameobj == NULL)
2779+
return NULL;
2780+
mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
2781+
fromlist, level);
2782+
Py_DECREF(nameobj);
2783+
return mod;
27732784
}
27742785

2786+
27752787
/* Return the package that an import is being performed in. If globals comes
27762788
from the module foo.bar.bat (not itself a package), this returns the
27772789
sys.modules entry for foo.bar. If globals is from a package's __init__.py,

0 commit comments

Comments
 (0)