Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
This commit is contained in:
parent
92a6be4318
commit
92e212f7d9
|
@ -6,7 +6,7 @@ are always available. They are listed here in alphabetical order.
|
||||||
|
|
||||||
\setindexsubitem{(built-in function)}
|
\setindexsubitem{(built-in function)}
|
||||||
|
|
||||||
\begin{funcdesc}{__import__}{name\optional{, globals\optional{, locals\optional{, fromlist}}}}
|
\begin{funcdesc}{__import__}{name\optional{, globals\optional{, locals\optional{, fromlist\optional{, level}}}}}
|
||||||
This function is invoked by the \keyword{import}\stindex{import}
|
This function is invoked by the \keyword{import}\stindex{import}
|
||||||
statement. It mainly exists so that you can replace it with another
|
statement. It mainly exists so that you can replace it with another
|
||||||
function that has a compatible interface, in order to change the
|
function that has a compatible interface, in order to change the
|
||||||
|
@ -20,9 +20,9 @@ are always available. They are listed here in alphabetical order.
|
||||||
|
|
||||||
For example, the statement \samp{import spam} results in the
|
For example, the statement \samp{import spam} results in the
|
||||||
following call: \code{__import__('spam',} \code{globals(),}
|
following call: \code{__import__('spam',} \code{globals(),}
|
||||||
\code{locals(), [])}; the statement \samp{from spam.ham import eggs}
|
\code{locals(), [], -1)}; the statement \samp{from spam.ham import eggs}
|
||||||
results in \samp{__import__('spam.ham', globals(), locals(),
|
results in \samp{__import__('spam.ham', globals(), locals(),
|
||||||
['eggs'])}. Note that even though \code{locals()} and
|
['eggs'], -1)}. Note that even though \code{locals()} and
|
||||||
\code{['eggs']} are passed in as arguments, the
|
\code{['eggs']} are passed in as arguments, the
|
||||||
\function{__import__()} function does not set the local variable
|
\function{__import__()} function does not set the local variable
|
||||||
named \code{eggs}; this is done by subsequent code that is generated
|
named \code{eggs}; this is done by subsequent code that is generated
|
||||||
|
@ -52,6 +52,15 @@ def my_import(name):
|
||||||
mod = getattr(mod, comp)
|
mod = getattr(mod, comp)
|
||||||
return mod
|
return mod
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
|
\var{level} specifies whether to use absolute or relative imports.
|
||||||
|
The default is \code{-1} which indicates both absolute and relative
|
||||||
|
imports will be attempted. \code{0} means only perform absolute imports.
|
||||||
|
Positive values for \var{level} indicate the number of parent directories
|
||||||
|
to search relative to the directory of the module calling
|
||||||
|
\function{__import__}.
|
||||||
|
\versionchanged[The level parameter was added]{2.5}
|
||||||
|
\versionchanged[Keyword support for parameters was added]{2.5}
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{abs}{x}
|
\begin{funcdesc}{abs}{x}
|
||||||
|
|
|
@ -12,6 +12,8 @@ What's New in Python 2.5 alpha 1?
|
||||||
Core and builtins
|
Core and builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- __import__ accepts keyword arguments.
|
||||||
|
|
||||||
- Patch #1460496: round() now accepts keyword arguments.
|
- Patch #1460496: round() now accepts keyword arguments.
|
||||||
|
|
||||||
- Fixed bug #1459029 - unicode reprs were double-escaped.
|
- Fixed bug #1459029 - unicode reprs were double-escaped.
|
||||||
|
|
|
@ -31,23 +31,25 @@ static PyObject *filterunicode(PyObject *, PyObject *);
|
||||||
static PyObject *filtertuple (PyObject *, PyObject *);
|
static PyObject *filtertuple (PyObject *, PyObject *);
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
builtin___import__(PyObject *self, PyObject *args)
|
builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
|
static char *kwlist[] = {"name", "globals", "locals", "fromlist",
|
||||||
|
"level", 0};
|
||||||
char *name;
|
char *name;
|
||||||
PyObject *globals = NULL;
|
PyObject *globals = NULL;
|
||||||
PyObject *locals = NULL;
|
PyObject *locals = NULL;
|
||||||
PyObject *fromlist = NULL;
|
PyObject *fromlist = NULL;
|
||||||
int level = -1;
|
int level = -1;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "s|OOOi:__import__",
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
|
||||||
&name, &globals, &locals, &fromlist, &level))
|
kwlist, &name, &globals, &locals, &fromlist, &level))
|
||||||
return NULL;
|
return NULL;
|
||||||
return PyImport_ImportModuleLevel(name, globals, locals,
|
return PyImport_ImportModuleLevel(name, globals, locals,
|
||||||
fromlist, level);
|
fromlist, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(import_doc,
|
PyDoc_STRVAR(import_doc,
|
||||||
"__import__(name, globals, locals, fromlist) -> module\n\
|
"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
|
||||||
\n\
|
\n\
|
||||||
Import a module. The globals are only used to determine the context;\n\
|
Import a module. The globals are only used to determine the context;\n\
|
||||||
they are not modified. The locals are currently unused. The fromlist\n\
|
they are not modified. The locals are currently unused. The fromlist\n\
|
||||||
|
@ -55,7 +57,10 @@ should be a list of names to emulate ``from name import ...'', or an\n\
|
||||||
empty list to emulate ``import name''.\n\
|
empty list to emulate ``import name''.\n\
|
||||||
When importing a module from a package, note that __import__('A.B', ...)\n\
|
When importing a module from a package, note that __import__('A.B', ...)\n\
|
||||||
returns package A when fromlist is empty, but its submodule B when\n\
|
returns package A when fromlist is empty, but its submodule B when\n\
|
||||||
fromlist is not empty.");
|
fromlist is not empty. Level is used to determine whether to perform \n\
|
||||||
|
absolute or relative imports. -1 is the original strategy of attempting\n\
|
||||||
|
both absolute and relative imports, 0 is absolute, a positive number\n\
|
||||||
|
is the number of parent directories to search relative to the current module.");
|
||||||
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
@ -2210,7 +2215,7 @@ in length to the length of the shortest argument sequence.");
|
||||||
|
|
||||||
|
|
||||||
static PyMethodDef builtin_methods[] = {
|
static PyMethodDef builtin_methods[] = {
|
||||||
{"__import__", builtin___import__, METH_VARARGS, import_doc},
|
{"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
|
||||||
{"abs", builtin_abs, METH_O, abs_doc},
|
{"abs", builtin_abs, METH_O, abs_doc},
|
||||||
{"all", builtin_all, METH_O, all_doc},
|
{"all", builtin_all, METH_O, all_doc},
|
||||||
{"any", builtin_any, METH_O, any_doc},
|
{"any", builtin_any, METH_O, any_doc},
|
||||||
|
|
Loading…
Reference in New Issue