]> granicus.if.org Git - python/commitdiff
Closes #15030: Make importlib.abc.PyPycLoader respect the new .pyc
authorBrett Cannon <brett@python.org>
Mon, 2 Jul 2012 18:35:34 +0000 (14:35 -0400)
committerBrett Cannon <brett@python.org>
Mon, 2 Jul 2012 18:35:34 +0000 (14:35 -0400)
file size header field.

Thanks to Marc Abramowitz and Ronan Lamy for helping out with various
parts of the patch.

Doc/library/importlib.rst
Lib/importlib/abc.py
Lib/importlib/test/source/test_abc_loader.py
Misc/ACKS
Misc/NEWS

index 4dc1c78b2e6ff3ea38b59d1edaf40a33c5059347..cea2da048e8b79172bcc85957580d9cde42496b7 100644 (file)
@@ -455,6 +455,10 @@ are also provided to help in implementing the core ABCs.
         :class:`PyLoader`. Do note that this solution will not support
         sourceless/bytecode-only loading; only source *and* bytecode loading.
 
+    .. versionchanged:: 3.3
+       Updated to parse (but not use) the new source size field in bytecode
+       files when reading and to write out the field properly when writing.
+
     .. method:: source_mtime(fullname)
 
         An abstract method which returns the modification time for the source
index c171da37aee307b0d34d52d5e61384079f922f47..8d659076b1ebb3b293ee13c623fa444a40fb7c2e 100644 (file)
@@ -282,7 +282,12 @@ class PyPycLoader(PyLoader):
                 if len(raw_timestamp) < 4:
                     raise EOFError("bad timestamp in {}".format(fullname))
                 pyc_timestamp = _bootstrap._r_long(raw_timestamp)
-                bytecode = data[8:]
+                raw_source_size = data[8:12]
+                if len(raw_source_size) != 4:
+                    raise EOFError("bad file size in {}".format(fullname))
+                # Source size is unused as the ABC does not provide a way to
+                # get the size of the source ahead of reading it.
+                bytecode = data[12:]
                 # Verify that the magic number is valid.
                 if imp.get_magic() != magic:
                     raise ImportError(
@@ -318,6 +323,7 @@ class PyPycLoader(PyLoader):
         if not sys.dont_write_bytecode:
             data = bytearray(imp.get_magic())
             data.extend(_bootstrap._w_long(source_timestamp))
+            data.extend(_bootstrap._w_long(len(source) & 0xFFFFFFFF))
             data.extend(marshal.dumps(code_object))
             self.write_bytecode(fullname, data)
         return code_object
index 27babb984514244bf882577de053ee43e6f69b46..afcaad0d9724ea1d6fcff90ed87526c492477af5 100644 (file)
@@ -148,11 +148,12 @@ class PyPycLoaderMock(abc.PyPycLoader, PyLoaderMock):
             self.bytecode_to_path[name] = data['path']
             magic = data.get('magic', imp.get_magic())
             mtime = importlib._w_long(data.get('mtime', self.default_mtime))
+            source_size = importlib._w_long(len(self.source) & 0xFFFFFFFF)
             if 'bc' in data:
                 bc = data['bc']
             else:
                 bc = self.compile_bc(name)
-            self.module_bytecode[name] = magic + mtime + bc
+            self.module_bytecode[name] = magic + mtime + source_size + bc
 
     def compile_bc(self, name):
         source_path = self.module_paths.get(name, '<test>') or '<test>'
@@ -344,7 +345,10 @@ class PyPycLoaderTests(PyLoaderTests):
         self.assertEqual(magic, imp.get_magic())
         mtime = importlib._r_long(mock.module_bytecode[name][4:8])
         self.assertEqual(mtime, 1)
-        bc = mock.module_bytecode[name][8:]
+        source_size = mock.module_bytecode[name][8:12]
+        self.assertEqual(len(mock.source) & 0xFFFFFFFF,
+                         importlib._r_long(source_size))
+        bc = mock.module_bytecode[name][12:]
         self.assertEqual(bc, mock.compile_bc(name))
 
     def test_module(self):
index 9af9dcd8acc4cefe023c1ae2e2195aa495f842cf..06dd7ebd5cb628352fc2a7585f1cd0fb0d583a50 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -587,6 +587,7 @@ Vladimir Kushnir
 Ross Lagerwall
 Cameron Laird
 Jean-Baptiste "Jiba" Lamy
+Ronan Lamy
 Torsten Landschoff
 Ćukasz Langa
 Tino Lange
index cdd3162afff9ced54b87c7d0ccfdcf2e3dd554f8..b318e503fd46319f6d5f313efbb3e020f32eb985 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #15030: importlib.abc.PyPycLoader now supports the new source size
+  header field in .pyc files.
+
 - Issue #5346: Preserve permissions of mbox, MMDF and Babyl mailbox
   files on flush().