mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
Fixed #1776. __import__() no longer imports modules by file name
This commit is contained in:
parent
195b883bb4
commit
3403f1589d
3 changed files with 25 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
||||||
from test.test_support import TESTFN, run_unittest, catch_warning
|
from test.test_support import TESTFN, run_unittest, catch_warning
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
import os
|
import os
|
||||||
|
@ -223,6 +223,16 @@ class ImportTest(unittest.TestCase):
|
||||||
warnings.simplefilter('error', ImportWarning)
|
warnings.simplefilter('error', ImportWarning)
|
||||||
self.assertRaises(ImportWarning, __import__, "site-packages")
|
self.assertRaises(ImportWarning, __import__, "site-packages")
|
||||||
|
|
||||||
|
def test_importbyfilename(self):
|
||||||
|
path = os.path.abspath(TESTFN)
|
||||||
|
try:
|
||||||
|
__import__(path)
|
||||||
|
except ImportError, err:
|
||||||
|
self.assertEqual("Import by filename is not supported.",
|
||||||
|
err.args[0])
|
||||||
|
else:
|
||||||
|
self.fail("import by path didn't raise an exception")
|
||||||
|
|
||||||
class PathsTests(unittest.TestCase):
|
class PathsTests(unittest.TestCase):
|
||||||
path = TESTFN
|
path = TESTFN
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,10 @@ What's New in Python 2.6 alpha 1?
|
||||||
Core and builtins
|
Core and builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Bug #1776: __import__ must not accept filenames. Python 2.6 does no longer
|
||||||
|
support module loading by filename. It worked on some system by coincident
|
||||||
|
but it was never intended to work.
|
||||||
|
|
||||||
- Patch #1668: renamed THREADDEBUG envvar to PYTHONTHREADDEBUG.
|
- Patch #1668: renamed THREADDEBUG envvar to PYTHONTHREADDEBUG.
|
||||||
|
|
||||||
- Patch #602345: Add -B command line option, PYTHONDONTWRITEBYTECODE envvar
|
- Patch #602345: Add -B command line option, PYTHONDONTWRITEBYTECODE envvar
|
||||||
|
|
|
@ -2055,6 +2055,16 @@ import_module_level(char *name, PyObject *globals, PyObject *locals,
|
||||||
Py_ssize_t buflen = 0;
|
Py_ssize_t buflen = 0;
|
||||||
PyObject *parent, *head, *next, *tail;
|
PyObject *parent, *head, *next, *tail;
|
||||||
|
|
||||||
|
if (strchr(name, '/') != NULL
|
||||||
|
#ifdef MS_WINDOWS
|
||||||
|
|| strchr(name, '\\') != NULL
|
||||||
|
#endif
|
||||||
|
) {
|
||||||
|
PyErr_SetString(PyExc_ImportError,
|
||||||
|
"Import by filename is not supported.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
parent = get_parent(globals, buf, &buflen, level);
|
parent = get_parent(globals, buf, &buflen, level);
|
||||||
if (parent == NULL)
|
if (parent == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue