bdf58d6
bdf58d6
# HG changeset patch
bdf58d6
# User Brett Cannon <brett@python.org>
bdf58d6
# Date 1393602285 18000
bdf58d6
# Node ID 432cb56db05d73f55d211501bf0dfc767768923b
bdf58d6
# Parent  ade5e4922a54cb84c99ec924ab7c700a014893da
bdf58d6
Issue #20778: Fix modulefinder to work with bytecode-only modules.
bdf58d6
bdf58d6
Bug filed and initial attempt at a patch by Bohuslav Kabrda.
bdf58d6
bdf58d6
diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py
bdf58d6
--- a/Lib/modulefinder.py
bdf58d6
+++ b/Lib/modulefinder.py
bdf58d6
@@ -287,7 +287,7 @@ class ModuleFinder:
bdf58d6
             if fp.read(4) != imp.get_magic():
bdf58d6
                 self.msgout(2, "raise ImportError: Bad magic number", pathname)
bdf58d6
                 raise ImportError("Bad magic number in %s" % pathname)
bdf58d6
-            fp.read(4)
bdf58d6
+            fp.read(8)  # Skip mtime and size.
bdf58d6
             co = marshal.load(fp)
bdf58d6
         else:
bdf58d6
             co = None
bdf58d6
diff --git a/Lib/test/test_modulefinder.py b/Lib/test/test_modulefinder.py
bdf58d6
--- a/Lib/test/test_modulefinder.py
bdf58d6
+++ b/Lib/test/test_modulefinder.py
bdf58d6
@@ -1,5 +1,7 @@
bdf58d6
 import os
bdf58d6
 import errno
bdf58d6
+import importlib.machinery
bdf58d6
+import py_compile
bdf58d6
 import shutil
bdf58d6
 import unittest
bdf58d6
 import tempfile
bdf58d6
@@ -208,6 +210,14 @@ a/module.py
bdf58d6
                                 from . import *
bdf58d6
 """]
bdf58d6
 
bdf58d6
+bytecode_test = [
bdf58d6
+    "a",
bdf58d6
+    ["a"],
bdf58d6
+    [],
bdf58d6
+    [],
bdf58d6
+    ""
bdf58d6
+]
bdf58d6
+
bdf58d6
 
bdf58d6
 def open_file(path):
bdf58d6
     dirname = os.path.dirname(path)
bdf58d6
@@ -288,6 +298,16 @@ class ModuleFinderTest(unittest.TestCase
bdf58d6
     def test_relative_imports_4(self):
bdf58d6
         self._do_test(relative_import_test_4)
bdf58d6
 
bdf58d6
+    def test_bytecode(self):
bdf58d6
+        base_path = os.path.join(TEST_DIR, 'a')
bdf58d6
+        source_path = base_path + importlib.machinery.SOURCE_SUFFIXES[0]
bdf58d6
+        bytecode_path = base_path + importlib.machinery.BYTECODE_SUFFIXES[0]
bdf58d6
+        with open_file(source_path) as file:
bdf58d6
+            file.write('testing_modulefinder = True\n')
bdf58d6
+        py_compile.compile(source_path, cfile=bytecode_path)
bdf58d6
+        os.remove(source_path)
bdf58d6
+        self._do_test(bytecode_test)
bdf58d6
+
bdf58d6
 
bdf58d6
 def test_main():
bdf58d6
     support.run_unittest(ModuleFinderTest)