Commit 6ae1e7f0 authored by Victor Stinner's avatar Victor Stinner

Issue #3080: imp.load_module() accepts None for the module path

imp.find_module() returns None as module path for builtin and frozen builtins.
parent 6a1454f3
...@@ -229,7 +229,9 @@ class ImportHooksTestCase(ImportHooksBaseTestCase): ...@@ -229,7 +229,9 @@ class ImportHooksTestCase(ImportHooksBaseTestCase):
i = ImpWrapper() i = ImpWrapper()
sys.meta_path.append(i) sys.meta_path.append(i)
sys.path_hooks.append(ImpWrapper) sys.path_hooks.append(ImpWrapper)
mnames = ("colorsys", "urllib.parse", "distutils.core") mnames = (
"colorsys", "urllib.parse", "distutils.core", "sys",
)
for mname in mnames: for mname in mnames:
parent = mname.split(".")[0] parent = mname.split(".")[0]
for n in list(sys.modules): for n in list(sys.modules):
...@@ -237,7 +239,8 @@ class ImportHooksTestCase(ImportHooksBaseTestCase): ...@@ -237,7 +239,8 @@ class ImportHooksTestCase(ImportHooksBaseTestCase):
del sys.modules[n] del sys.modules[n]
for mname in mnames: for mname in mnames:
m = __import__(mname, globals(), locals(), ["__dummy__"]) m = __import__(mname, globals(), locals(), ["__dummy__"])
m.__loader__ # to make sure we actually handled the import # to make sure we actually handled the import
self.assertTrue(hasattr(m, "__loader__"))
def test_main(): def test_main():
......
...@@ -3744,17 +3744,22 @@ imp_load_source(PyObject *self, PyObject *args) ...@@ -3744,17 +3744,22 @@ imp_load_source(PyObject *self, PyObject *args)
static PyObject * static PyObject *
imp_load_module(PyObject *self, PyObject *args) imp_load_module(PyObject *self, PyObject *args)
{ {
PyObject *name, *fob, *pathname, *ret; PyObject *name, *fob, *pathname, *pathname_obj, *ret;
char *suffix; /* Unused */ char *suffix; /* Unused */
char *mode; char *mode;
int type; int type;
FILE *fp; FILE *fp;
if (!PyArg_ParseTuple(args, "UOO&(ssi):load_module", if (!PyArg_ParseTuple(args, "UOO(ssi):load_module",
&name, &fob, &name, &fob, &pathname_obj, &suffix, &mode, &type))
PyUnicode_FSDecoder, &pathname, return NULL;
&suffix, &mode, &type)) if (pathname_obj != Py_None) {
if (!PyUnicode_FSDecoder(pathname_obj, &pathname))
return NULL; return NULL;
}
else
pathname = NULL;
if (*mode) { if (*mode) {
/* Mode must start with 'r' or 'U' and must not contain '+'. /* Mode must start with 'r' or 'U' and must not contain '+'.
Implicit in this test is the assumption that the mode Implicit in this test is the assumption that the mode
...@@ -3763,7 +3768,7 @@ imp_load_module(PyObject *self, PyObject *args) ...@@ -3763,7 +3768,7 @@ imp_load_module(PyObject *self, PyObject *args)
if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) { if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"invalid file open mode %.200s", mode); "invalid file open mode %.200s", mode);
Py_DECREF(pathname); Py_XDECREF(pathname);
return NULL; return NULL;
} }
} }
...@@ -3772,12 +3777,12 @@ imp_load_module(PyObject *self, PyObject *args) ...@@ -3772,12 +3777,12 @@ imp_load_module(PyObject *self, PyObject *args)
else { else {
fp = get_file(NULL, fob, mode); fp = get_file(NULL, fob, mode);
if (fp == NULL) { if (fp == NULL) {
Py_DECREF(pathname); Py_XDECREF(pathname);
return NULL; return NULL;
} }
} }
ret = load_module(name, fp, pathname, type, NULL); ret = load_module(name, fp, pathname, type, NULL);
Py_DECREF(pathname); Py_XDECREF(pathname);
if (fp) if (fp)
fclose(fp); fclose(fp);
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment