diff -r 9623c83ba489 Lib/importlib/abc.py --- a/Lib/importlib/abc.py Wed Jun 27 15:26:26 2012 -0400 +++ b/Lib/importlib/abc.py Wed Jun 27 13:57:41 2012 -0700 @@ -282,7 +282,11 @@ if len(raw_timestamp) < 4: raise EOFError("bad timestamp in {}".format(fullname)) pyc_timestamp = _bootstrap._r_long(raw_timestamp) - bytecode = data[8:] + raw_file_size = data[8:12] + if len(raw_file_size) < 4: + raise EOFError("bad file size in {}".format(fullname)) + file_size = _bootstrap._r_long(raw_file_size) + bytecode = data[12:] # Verify that the magic number is valid. if imp.get_magic() != magic: raise ImportError( @@ -318,6 +322,7 @@ if not sys.dont_write_bytecode: data = bytearray(imp.get_magic()) data.extend(_bootstrap._w_long(source_timestamp)) + data.extend(_bootstrap._w_long(source_timestamp)) data.extend(marshal.dumps(code_object)) self.write_bytecode(fullname, data) return code_object diff -r 9623c83ba489 Lib/importlib/test/source/test_abc_loader.py --- a/Lib/importlib/test/source/test_abc_loader.py Wed Jun 27 15:26:26 2012 -0400 +++ b/Lib/importlib/test/source/test_abc_loader.py Wed Jun 27 13:57:41 2012 -0700 @@ -152,7 +152,8 @@ bc = data['bc'] else: bc = self.compile_bc(name) - self.module_bytecode[name] = magic + mtime + bc + size = importlib._w_long(len(bc)) + self.module_bytecode[name] = magic + mtime + size + bc def compile_bc(self, name): source_path = self.module_paths.get(name, '') or '' @@ -344,7 +345,7 @@ 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:] + bc = mock.module_bytecode[name][12:] self.assertEqual(bc, mock.compile_bc(name)) def test_module(self): @@ -456,6 +457,22 @@ mtime = importlib._r_long(mock.module_bytecode[name][4:8]) self.assertEqual(mtime, PyPycLoaderMock.default_mtime) + @source_util.writes_bytecode_files + def test_pyc_compatibility(self): + name = 'foo' + with source_util.create_modules(name) as fpath: + loader = importlib.machinery.SourceFileLoader(name, fpath[name]) + module = loader.load_module(name) + bytecode_path = module.__cached__ + with open(bytecode_path, 'rb') as f: + cached_data = f.read() + mock = PyPycLoaderMock({name: fpath[name]}, + {name: {'path': bytecode_path}}) + mock.module_bytecode[name] = cached_data + del sys.modules[name] + mock.load_module(name) + self.assertEqual(mock.module_bytecode[name], cached_data) + class BadBytecodeFailureTests(unittest.TestCase):