Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01a7617

Browse files
committed
Issue #13959: Re-implement imp.load_module() in imp.py.
1 parent 7c3e150 commit 01a7617

2 files changed

Lines changed: 28 additions & 54 deletions

File tree

Lib/imp.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,37 @@
1414
from _imp import (get_magic, get_tag, get_suffixes, cache_from_source,
1515
source_from_cache)
1616
# Should be re-implemented here (and mostly deprecated)
17-
from _imp import (find_module, load_module, load_compiled,
17+
from _imp import (find_module, load_compiled,
1818
load_package, load_source, NullImporter,
1919
SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION,
2020
PY_RESOURCE, PKG_DIRECTORY, C_BUILTIN, PY_FROZEN,
2121
PY_CODERESOURCE, IMP_HOOK)
2222

2323
from importlib._bootstrap import _new_module as new_module
24+
25+
26+
def load_module(name, file, filename, details):
27+
"""Load a module, given information returned by find_module().
28+
29+
The module name must include the full package name, if any.
30+
31+
"""
32+
suffix, mode, type_ = details
33+
if mode and (not mode.startswith(('r', 'U'))) or '+' in mode:
34+
raise ValueError('invalid file open mode {!r}'.format(mode))
35+
elif file is None and type_ in {PY_SOURCE, PY_COMPILED}:
36+
msg = 'file object required for import (type code {})'.format(type_)
37+
raise ValueError(msg)
38+
elif type_ == PY_SOURCE:
39+
return load_source(name, filename, file)
40+
elif type_ == PY_COMPILED:
41+
return load_compiled(name, filename, file)
42+
elif type_ == PKG_DIRECTORY:
43+
return load_package(name, filename)
44+
elif type_ == C_BUILTIN:
45+
return init_builtin(name)
46+
elif type_ == PY_FROZEN:
47+
return init_frozen(name)
48+
else:
49+
msg = "Don't know how to import {} (type code {}".format(name, type_)
50+
raise ImportError(msg, name=name)

Python/import.c

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3600,53 +3600,6 @@ imp_load_source(PyObject *self, PyObject *args)
36003600
return m;
36013601
}
36023602

3603-
static PyObject *
3604-
imp_load_module(PyObject *self, PyObject *args)
3605-
{
3606-
PyObject *name, *fob, *pathname, *pathname_obj, *ret;
3607-
char *suffix; /* Unused */
3608-
char *mode;
3609-
int type;
3610-
FILE *fp;
3611-
3612-
if (!PyArg_ParseTuple(args, "UOO(ssi):load_module",
3613-
&name, &fob, &pathname_obj, &suffix, &mode, &type))
3614-
return NULL;
3615-
if (pathname_obj != Py_None) {
3616-
if (!PyUnicode_FSDecoder(pathname_obj, &pathname))
3617-
return NULL;
3618-
}
3619-
else
3620-
pathname = NULL;
3621-
3622-
if (*mode) {
3623-
/* Mode must start with 'r' or 'U' and must not contain '+'.
3624-
Implicit in this test is the assumption that the mode
3625-
may contain other modifiers like 'b' or 't'. */
3626-
3627-
if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
3628-
PyErr_Format(PyExc_ValueError,
3629-
"invalid file open mode %.200s", mode);
3630-
Py_XDECREF(pathname);
3631-
return NULL;
3632-
}
3633-
}
3634-
if (fob == Py_None)
3635-
fp = NULL;
3636-
else {
3637-
fp = get_file(NULL, fob, mode);
3638-
if (fp == NULL) {
3639-
Py_XDECREF(pathname);
3640-
return NULL;
3641-
}
3642-
}
3643-
ret = load_module(name, fp, pathname, type, NULL);
3644-
Py_XDECREF(pathname);
3645-
if (fp)
3646-
fclose(fp);
3647-
return ret;
3648-
}
3649-
36503603
static PyObject *
36513604
imp_load_package(PyObject *self, PyObject *args)
36523605
{
@@ -3757,11 +3710,6 @@ built-in, frozen or special module and continue search in sys.path.\n\
37573710
The module name cannot contain '.'; to search for a submodule of a\n\
37583711
package, pass the submodule name and the package's __path__.");
37593712

3760-
PyDoc_STRVAR(doc_load_module,
3761-
"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
3762-
Load a module, given information returned by find_module().\n\
3763-
The module name must include the full package name, if any.");
3764-
37653713
PyDoc_STRVAR(doc_get_magic,
37663714
"get_magic() -> string\n\
37673715
Return the magic number for .pyc or .pyo files.");
@@ -3797,7 +3745,6 @@ static PyMethodDef imp_methods[] = {
37973745
{"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
37983746
{"get_tag", imp_get_tag, METH_NOARGS, doc_get_tag},
37993747
{"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3800-
{"load_module", imp_load_module, METH_VARARGS, doc_load_module},
38013748
{"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
38023749
{"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
38033750
{"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},

0 commit comments

Comments
 (0)