]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #20778: Fix modulefinder to work with bytecode-only modules.
authorBrett Cannon <brett@python.org>
Fri, 28 Feb 2014 15:44:45 +0000 (10:44 -0500)
committerBrett Cannon <brett@python.org>
Fri, 28 Feb 2014 15:44:45 +0000 (10:44 -0500)
Bug filed and initial attempt at a patch by Bohuslav Kabrda.

Lib/modulefinder.py
Lib/test/test_modulefinder.py
Misc/NEWS

index 82c7aed1d746fd80b4d40637961c92d902a0c670..264b0f0bfd1f58e941d3fa2c49235c193de94fe0 100644 (file)
@@ -287,7 +287,7 @@ class ModuleFinder:
             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)
+            fp.read(8)  # Skip mtime and size.
             co = marshal.load(fp)
         else:
             co = None
index 53ea232174912cb0593b29a71f7881aec4293a07..ed30d6fe3a45e554570a84622b4019c957b64156 100644 (file)
@@ -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)
index 3b707839e2ba6f985ab16d684fa4fccf9bd720fd..58b6c36b2ff27b82683311637e3c5dd58fc3632b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,8 @@ Core and Builtins
 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
   a bytes object.  Initial patch by Peter Otten.