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

Skip to content

Commit 6ae1e7f

Browse files
author
Victor Stinner
committed
Issue #3080: imp.load_module() accepts None for the module path
imp.find_module() returns None as module path for builtin and frozen builtins.
1 parent 6a1454f commit 6ae1e7f

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

Lib/test/test_importhooks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,18 @@ def testImpWrapper(self):
229229
i = ImpWrapper()
230230
sys.meta_path.append(i)
231231
sys.path_hooks.append(ImpWrapper)
232-
mnames = ("colorsys", "urllib.parse", "distutils.core")
232+
mnames = (
233+
"colorsys", "urllib.parse", "distutils.core", "sys",
234+
)
233235
for mname in mnames:
234236
parent = mname.split(".")[0]
235237
for n in list(sys.modules):
236238
if n.startswith(parent):
237239
del sys.modules[n]
238240
for mname in mnames:
239241
m = __import__(mname, globals(), locals(), ["__dummy__"])
240-
m.__loader__ # to make sure we actually handled the import
242+
# to make sure we actually handled the import
243+
self.assertTrue(hasattr(m, "__loader__"))
241244

242245

243246
def test_main():

Python/import.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,17 +3744,22 @@ imp_load_source(PyObject *self, PyObject *args)
37443744
static PyObject *
37453745
imp_load_module(PyObject *self, PyObject *args)
37463746
{
3747-
PyObject *name, *fob, *pathname, *ret;
3747+
PyObject *name, *fob, *pathname, *pathname_obj, *ret;
37483748
char *suffix; /* Unused */
37493749
char *mode;
37503750
int type;
37513751
FILE *fp;
37523752

3753-
if (!PyArg_ParseTuple(args, "UOO&(ssi):load_module",
3754-
&name, &fob,
3755-
PyUnicode_FSDecoder, &pathname,
3756-
&suffix, &mode, &type))
3753+
if (!PyArg_ParseTuple(args, "UOO(ssi):load_module",
3754+
&name, &fob, &pathname_obj, &suffix, &mode, &type))
37573755
return NULL;
3756+
if (pathname_obj != Py_None) {
3757+
if (!PyUnicode_FSDecoder(pathname_obj, &pathname))
3758+
return NULL;
3759+
}
3760+
else
3761+
pathname = NULL;
3762+
37583763
if (*mode) {
37593764
/* Mode must start with 'r' or 'U' and must not contain '+'.
37603765
Implicit in this test is the assumption that the mode
@@ -3763,7 +3768,7 @@ imp_load_module(PyObject *self, PyObject *args)
37633768
if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
37643769
PyErr_Format(PyExc_ValueError,
37653770
"invalid file open mode %.200s", mode);
3766-
Py_DECREF(pathname);
3771+
Py_XDECREF(pathname);
37673772
return NULL;
37683773
}
37693774
}
@@ -3772,12 +3777,12 @@ imp_load_module(PyObject *self, PyObject *args)
37723777
else {
37733778
fp = get_file(NULL, fob, mode);
37743779
if (fp == NULL) {
3775-
Py_DECREF(pathname);
3780+
Py_XDECREF(pathname);
37763781
return NULL;
37773782
}
37783783
}
37793784
ret = load_module(name, fp, pathname, type, NULL);
3780-
Py_DECREF(pathname);
3785+
Py_XDECREF(pathname);
37813786
if (fp)
37823787
fclose(fp);
37833788
return ret;

0 commit comments

Comments
 (0)