[Python-checkins] cpython: Issue #3080: Add PyImport_ImportModuleLevelObject() function (original) (raw)
victor.stinner python-checkins at python.org
Sun Mar 20 04:14:07 CET 2011
- Previous message: [Python-checkins] cpython: Issue #3080: Use repr() to format the module name on error
- Next message: [Python-checkins] cpython: Issue #3080: skip test_bdist_rpm if sys.executable is not encodable to UTF-8
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
http://hg.python.org/cpython/rev/80f4bd647695 changeset: 68739:80f4bd647695 user: Victor Stinner <victor.stinner at haypocalc.com> date: Mon Mar 14 15:54:52 2011 -0400 summary: Issue #3080: Add PyImport_ImportModuleLevelObject() function
Use it for the builtin import function.
files: Doc/c-api/import.rst Include/import.h Python/bltinmodule.c Python/import.c
diff --git a/Doc/c-api/import.rst b/Doc/c-api/import.rst
--- a/Doc/c-api/import.rst
+++ b/Doc/c-api/import.rst
@@ -57,7 +57,7 @@
: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 @@ 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:: 3.3
- +.. 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)
diff --git a/Include/import.h b/Include/import.h --- a/Include/import.h +++ b/Include/import.h @@ -50,6 +50,13 @@ PyObject *fromlist, int level ); +PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevelObject(
- PyObject *name,
- PyObject *globals,
- PyObject *locals,
- PyObject *fromlist,
- int level
- );
#define PyImport_ImportModuleEx(n, g, l, f)
PyImport_ImportModuleLevel(n, g, l, f, -1)
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -155,17 +155,14 @@
{
static char *kwlist[] = {"name", "globals", "locals", "fromlist",
"level", 0};
- char *name;
- PyObject *globals = NULL;
- PyObject *locals = NULL;
- PyObject *fromlist = NULL;
- PyObject *name, *globals = NULL, *locals = NULL, *fromlist = NULL; int level = -1;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:import",
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:import", kwlist, &name, &globals, &locals, &fromlist, &level)) return NULL;
- return PyImport_ImportModuleLevel(name, globals, locals,
fromlist, level);
- return PyImport_ImportModuleLevelObject(name, globals, locals,
fromlist, level);
}
PyDoc_STRVAR(import_doc, diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -2753,25 +2753,37 @@ }
PyObject * -PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
PyObject *fromlist, int level)
+PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
PyObject *locals, PyObject *fromlist,
int level)
{
- PyObject *nameobj, *result;
- nameobj = PyUnicode_FromString(name);
- if (nameobj == NULL)
return NULL;
- PyObject *mod; _PyImport_AcquireLock();
- result = import_module_level(nameobj, globals, locals, fromlist, level);
- Py_DECREF(nameobj);
- mod = import_module_level(name, globals, locals, fromlist, level); if (_PyImport_ReleaseLock() < 0) {
Py_XDECREF(result);
}Py_XDECREF(mod); PyErr_SetString(PyExc_RuntimeError, "not holding the import lock"); return NULL;
- return result;
- return mod;
}
+PyObject * +PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
PyObject *fromlist, int level)
+{
- PyObject *nameobj, *mod;
- nameobj = PyUnicode_FromString(name);
- if (nameobj == NULL)
return NULL;
- mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
fromlist, level);
- 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,
-- Repository URL: http://hg.python.org/cpython
- Previous message: [Python-checkins] cpython: Issue #3080: Use repr() to format the module name on error
- Next message: [Python-checkins] cpython: Issue #3080: skip test_bdist_rpm if sys.executable is not encodable to UTF-8
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]