[Python-checkins] cpython (merge 3.3 -> default): merge for issue #20778

brett.cannon python-checkins at python.org
Fri Feb 28 16:50:46 CET 2014


http://hg.python.org/cpython/rev/b6e999c8907c
changeset:   89429:b6e999c8907c
parent:      89427:2dd7b9618596
parent:      89428:432cb56db05d
user:        Brett Cannon <brett at python.org>
date:        Fri Feb 28 10:50:34 2014 -0500
summary:
  merge for issue #20778

files:
  Lib/modulefinder.py           |  12 +++++++-----
  Lib/test/test_modulefinder.py |  20 ++++++++++++++++++++
  Misc/NEWS                     |   2 ++
  3 files changed, 29 insertions(+), 5 deletions(-)


diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py
--- 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 @@
         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
--- 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 @@
                                 from . import *
 """]
 
+bytecode_test = [
+    "a",
+    ["a"],
+    [],
+    [],
+    ""
+]
+
 
 def open_file(path):
     dirname = os.path.dirname(path)
@@ -288,6 +298,16 @@
     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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,8 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list