merge for issue #20778

This commit is contained in:
Brett Cannon 2014-02-28 10:50:34 -05:00
commit 0f3847855d
3 changed files with 29 additions and 5 deletions

View File

@ -1,6 +1,7 @@
"""Find modules used by a script, using introspection.""" """Find modules used by a script, using introspection."""
import dis import dis
import importlib._bootstrap
import importlib.machinery import importlib.machinery
import marshal import marshal
import os import os
@ -287,11 +288,12 @@ class ModuleFinder:
if type == imp.PY_SOURCE: if type == imp.PY_SOURCE:
co = compile(fp.read()+'\n', pathname, 'exec') co = compile(fp.read()+'\n', pathname, 'exec')
elif type == imp.PY_COMPILED: elif type == imp.PY_COMPILED:
if fp.read(4) != imp.get_magic(): try:
self.msgout(2, "raise ImportError: Bad magic number", pathname) marshal_data = importlib._bootstrap._validate_bytecode_header(fp.read())
raise ImportError("Bad magic number in %s" % pathname) except ImportError as exc:
fp.read(4) self.msgout(2, "raise ImportError: " + str(exc), pathname)
co = marshal.load(fp) raise
co = marshal.loads(marshal_data)
else: else:
co = None co = None
m = self.add_module(fqname) m = self.add_module(fqname)

View File

@ -1,5 +1,7 @@
import os import os
import errno import errno
import importlib.machinery
import py_compile
import shutil import shutil
import unittest import unittest
import tempfile import tempfile
@ -208,6 +210,14 @@ a/module.py
from . import * from . import *
"""] """]
bytecode_test = [
"a",
["a"],
[],
[],
""
]
def open_file(path): def open_file(path):
dirname = os.path.dirname(path) dirname = os.path.dirname(path)
@ -288,6 +298,16 @@ class ModuleFinderTest(unittest.TestCase):
def test_relative_imports_4(self): def test_relative_imports_4(self):
self._do_test(relative_import_test_4) 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(): def test_main():
support.run_unittest(ModuleFinderTest) support.run_unittest(ModuleFinderTest)

View File

@ -17,6 +17,8 @@ Core and Builtins
Library Library
------- -------
- Issue #20778: Fix modulefinder to work with bytecode-only modules.
- Issue #20791: copy.copy() now doesn't make a copy when the input is - Issue #20791: copy.copy() now doesn't make a copy when the input is
a bytes object. Initial patch by Peter Otten. a bytes object. Initial patch by Peter Otten.