Issue #13645: pyc files now contain the size of the corresponding source · python/cpython@5136ac0
@@ -70,11 +70,6 @@ def test_module_reuse(self):
7070module_dict_id = id(module.__dict__)
7171with open(mapping['_temp'], 'w') as file:
7272file.write("testing_var = 42\n")
73-# For filesystems where the mtime is only to a second granularity,
74-# everything that has happened above can be too fast;
75-# force an mtime on the source that is guaranteed to be different
76-# than the original mtime.
77-loader.path_mtime = self.fake_mtime(loader.path_mtime)
7873module = loader.load_module('_temp')
7974self.assertTrue('testing_var' in module.__dict__,
8075"'testing_var' not in "
@@ -190,10 +185,17 @@ def _test_partial_timestamp(self, test, *, del_source=False):
190185del_source=del_source)
191186test('_temp', mapping, bc_path)
192187188+def _test_partial_size(self, test, *, del_source=False):
189+with source_util.create_modules('_temp') as mapping:
190+bc_path = self.manipulate_bytecode('_temp', mapping,
191+lambda bc: bc[:11],
192+del_source=del_source)
193+test('_temp', mapping, bc_path)
194+193195def _test_no_marshal(self, *, del_source=False):
194196with source_util.create_modules('_temp') as mapping:
195197bc_path = self.manipulate_bytecode('_temp', mapping,
196-lambda bc: bc[:8],
198+lambda bc: bc[:12],
197199del_source=del_source)
198200file_path = mapping['_temp'] if not del_source else bc_path
199201with self.assertRaises(EOFError):
@@ -202,7 +204,7 @@ def _test_no_marshal(self, *, del_source=False):
202204def _test_non_code_marshal(self, *, del_source=False):
203205with source_util.create_modules('_temp') as mapping:
204206bytecode_path = self.manipulate_bytecode('_temp', mapping,
205-lambda bc: bc[:8] + marshal.dumps(b'abcd'),
207+lambda bc: bc[:12] + marshal.dumps(b'abcd'),
206208del_source=del_source)
207209file_path = mapping['_temp'] if not del_source else bytecode_path
208210with self.assertRaises(ImportError):
@@ -211,7 +213,7 @@ def _test_non_code_marshal(self, *, del_source=False):
211213def _test_bad_marshal(self, *, del_source=False):
212214with source_util.create_modules('_temp') as mapping:
213215bytecode_path = self.manipulate_bytecode('_temp', mapping,
214-lambda bc: bc[:8] + b'<test>',
216+lambda bc: bc[:12] + b'<test>',
215217del_source=del_source)
216218file_path = mapping['_temp'] if not del_source else bytecode_path
217219with self.assertRaises(EOFError):
@@ -235,15 +237,15 @@ def test_empty_file(self):
235237def test(name, mapping, bytecode_path):
236238self.import_(mapping[name], name)
237239with open(bytecode_path, 'rb') as file:
238-self.assertGreater(len(file.read()), 8)
240+self.assertGreater(len(file.read()), 12)
239241240242self._test_empty_file(test)
241243242244def test_partial_magic(self):
243245def test(name, mapping, bytecode_path):
244246self.import_(mapping[name], name)
245247with open(bytecode_path, 'rb') as file:
246-self.assertGreater(len(file.read()), 8)
248+self.assertGreater(len(file.read()), 12)
247249248250self._test_partial_magic(test)
249251@@ -254,7 +256,7 @@ def test_magic_only(self):
254256def test(name, mapping, bytecode_path):
255257self.import_(mapping[name], name)
256258with open(bytecode_path, 'rb') as file:
257-self.assertGreater(len(file.read()), 8)
259+self.assertGreater(len(file.read()), 12)
258260259261self._test_magic_only(test)
260262@@ -276,10 +278,21 @@ def test_partial_timestamp(self):
276278def test(name, mapping, bc_path):
277279self.import_(mapping[name], name)
278280with open(bc_path, 'rb') as file:
279-self.assertGreater(len(file.read()), 8)
281+self.assertGreater(len(file.read()), 12)
280282281283self._test_partial_timestamp(test)
282284285+@source_util.writes_bytecode_files
286+def test_partial_size(self):
287+# When the size is partial, regenerate the .pyc, else
288+# raise EOFError.
289+def test(name, mapping, bc_path):
290+self.import_(mapping[name], name)
291+with open(bc_path, 'rb') as file:
292+self.assertGreater(len(file.read()), 12)
293+294+self._test_partial_size(test)
295+283296@source_util.writes_bytecode_files
284297def test_no_marshal(self):
285298# When there is only the magic number and timestamp, raise EOFError.
@@ -375,6 +388,13 @@ def test(name, mapping, bytecode_path):
375388376389self._test_partial_timestamp(test, del_source=True)
377390391+def test_partial_size(self):
392+def test(name, mapping, bytecode_path):
393+with self.assertRaises(EOFError):
394+self.import_(bytecode_path, name)
395+396+self._test_partial_size(test, del_source=True)
397+378398def test_no_marshal(self):
379399self._test_no_marshal(del_source=True)
380400