diff options
author | Brett Cannon <brett@python.org> | 2014-02-28 15:50:34 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2014-02-28 15:50:34 (GMT) |
commit | 0f3847855dafbfaeed385a676a004f9bb5a9cf83 (patch) | |
tree | 73545353dc566ddbf7d04552d6e4ae6a761b6135 /Lib | |
parent | acc74e6c63d5f4b91b26134b1fc61aa005aa26e3 (diff) | |
parent | 298bb967767d666fca8177f8c2352c2134490565 (diff) | |
download | cpython-0f3847855dafbfaeed385a676a004f9bb5a9cf83.zip cpython-0f3847855dafbfaeed385a676a004f9bb5a9cf83.tar.gz cpython-0f3847855dafbfaeed385a676a004f9bb5a9cf83.tar.bz2 |
merge for issue #20778
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/modulefinder.py | 12 | ||||
-rw-r--r-- | Lib/test/test_modulefinder.py | 20 |
2 files changed, 27 insertions, 5 deletions
diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py index b19941e..cc5b8cc 100644 --- a/Lib/modulefinder.py +++ b/Lib/modulefinder.py @@ -1,6 +1,7 @@ """Find modules used by a script, using introspection.""" import dis +import importlib._bootstrap import importlib.machinery import marshal import os @@ -287,11 +288,12 @@ class ModuleFinder: if type == imp.PY_SOURCE: co = compile(fp.read()+'\n', pathname, 'exec') elif type == imp.PY_COMPILED: - if fp.read(4) != imp.get_magic(): - self.msgout(2, "raise ImportError: Bad magic number", pathname) - raise ImportError("Bad magic number in %s" % pathname) - fp.read(4) - co = marshal.load(fp) + try: + marshal_data = importlib._bootstrap._validate_bytecode_header(fp.read()) + except ImportError as exc: + self.msgout(2, "raise ImportError: " + str(exc), pathname) + raise + co = marshal.loads(marshal_data) else: co = None m = self.add_module(fqname) diff --git a/Lib/test/test_modulefinder.py b/Lib/test/test_modulefinder.py index 53ea232..ed30d6f 100644 --- a/Lib/test/test_modulefinder.py +++ b/Lib/test/test_modulefinder.py @@ -1,5 +1,7 @@ import os import errno +import importlib.machinery +import py_compile import shutil import unittest import tempfile @@ -208,6 +210,14 @@ a/module.py from . import * """] +bytecode_test = [ + "a", + ["a"], + [], + [], + "" +] + def open_file(path): dirname = os.path.dirname(path) @@ -288,6 +298,16 @@ class ModuleFinderTest(unittest.TestCase): def test_relative_imports_4(self): self._do_test(relative_import_test_4) + def test_bytecode(self): + base_path = os.path.join(TEST_DIR, 'a') + source_path = base_path + importlib.machinery.SOURCE_SUFFIXES[0] + bytecode_path = base_path + importlib.machinery.BYTECODE_SUFFIXES[0] + with open_file(source_path) as file: + file.write('testing_modulefinder = True\n') + py_compile.compile(source_path, cfile=bytecode_path) + os.remove(source_path) + self._do_test(bytecode_test) + def test_main(): support.run_unittest(ModuleFinderTest) |