vstinner / rpms / python3

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