Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3645)

Side by Side Diff: Lib/importlib/abc.py

Issue 15030: PyPycLoader can't read cached .pyc files
Patch Set: Created 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 """Abstract base classes related to import.""" 1 """Abstract base classes related to import."""
2 from . import _bootstrap 2 from . import _bootstrap
3 from . import machinery 3 from . import machinery
4 try: 4 try:
5 import _frozen_importlib 5 import _frozen_importlib
6 except ImportError as exc: 6 except ImportError as exc:
7 if exc.name != '_frozen_importlib': 7 if exc.name != '_frozen_importlib':
8 raise 8 raise
9 _frozen_importlib = None 9 _frozen_importlib = None
10 import abc 10 import abc
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 try: 275 try:
276 magic = data[:4] 276 magic = data[:4]
277 if len(magic) < 4: 277 if len(magic) < 4:
278 raise ImportError( 278 raise ImportError(
279 "bad magic number in {}".format(fullname), 279 "bad magic number in {}".format(fullname),
280 name=fullname, path=bytecode_path) 280 name=fullname, path=bytecode_path)
281 raw_timestamp = data[4:8] 281 raw_timestamp = data[4:8]
282 if len(raw_timestamp) < 4: 282 if len(raw_timestamp) < 4:
283 raise EOFError("bad timestamp in {}".format(fullname)) 283 raise EOFError("bad timestamp in {}".format(fullname))
284 pyc_timestamp = _bootstrap._r_long(raw_timestamp) 284 pyc_timestamp = _bootstrap._r_long(raw_timestamp)
285 bytecode = data[8:] 285 raw_file_size = data[8:12]
286 if len(raw_file_size) < 4:
287 raise EOFError("bad file size in {}".format(fullname))
288 file_size = _bootstrap._r_long(raw_file_size)
289 bytecode = data[12:]
286 # Verify that the magic number is valid. 290 # Verify that the magic number is valid.
287 if imp.get_magic() != magic: 291 if imp.get_magic() != magic:
288 raise ImportError( 292 raise ImportError(
289 "bad magic number in {}".format(fullname), 293 "bad magic number in {}".format(fullname),
290 name=fullname, path=bytecode_path) 294 name=fullname, path=bytecode_path)
291 # Verify that the bytecode is not stale (only matters when 295 # Verify that the bytecode is not stale (only matters when
292 # there is source to fall back on. 296 # there is source to fall back on.
293 if source_timestamp: 297 if source_timestamp:
294 if pyc_timestamp < source_timestamp: 298 if pyc_timestamp < source_timestamp:
295 raise ImportError("bytecode is stale", name=fullname, 299 raise ImportError("bytecode is stale", name=fullname,
(...skipping 13 matching lines...) Expand all
309 name=fullname) 313 name=fullname)
310 # Use the source. 314 # Use the source.
311 source_path = self.source_path(fullname) 315 source_path = self.source_path(fullname)
312 if source_path is None: 316 if source_path is None:
313 message = "a source path must exist to load {0}".format(fullname) 317 message = "a source path must exist to load {0}".format(fullname)
314 raise ImportError(message, name=fullname) 318 raise ImportError(message, name=fullname)
315 source = self.get_data(source_path) 319 source = self.get_data(source_path)
316 code_object = compile(source, source_path, 'exec', dont_inherit=True) 320 code_object = compile(source, source_path, 'exec', dont_inherit=True)
317 # Generate bytecode and write it out. 321 # Generate bytecode and write it out.
318 if not sys.dont_write_bytecode: 322 if not sys.dont_write_bytecode:
323 bytecode = marshal.dumps(code_object)
319 data = bytearray(imp.get_magic()) 324 data = bytearray(imp.get_magic())
320 data.extend(_bootstrap._w_long(source_timestamp)) 325 data.extend(_bootstrap._w_long(source_timestamp))
321 data.extend(marshal.dumps(code_object)) 326 data.extend(_bootstrap._w_long(len(bytecode)))
brett.cannon 2012/06/28 17:08:02 It is the length of the source, not the bytecode;
327 data.extend(bytecode)
322 self.write_bytecode(fullname, data) 328 self.write_bytecode(fullname, data)
323 return code_object 329 return code_object
324 330
325 @abc.abstractmethod 331 @abc.abstractmethod
326 def source_mtime(self, fullname): 332 def source_mtime(self, fullname):
327 """Abstract method. Accepts a str filename and returns an int 333 """Abstract method. Accepts a str filename and returns an int
328 modification time for the source of the module.""" 334 modification time for the source of the module."""
329 raise NotImplementedError 335 raise NotImplementedError
330 336
331 @abc.abstractmethod 337 @abc.abstractmethod
332 def bytecode_path(self, fullname): 338 def bytecode_path(self, fullname):
333 """Abstract method. Accepts a str filename and returns the str pathname 339 """Abstract method. Accepts a str filename and returns the str pathname
334 to the bytecode for the module.""" 340 to the bytecode for the module."""
335 raise NotImplementedError 341 raise NotImplementedError
336 342
337 @abc.abstractmethod 343 @abc.abstractmethod
338 def write_bytecode(self, fullname, bytecode): 344 def write_bytecode(self, fullname, bytecode):
339 """Abstract method. Accepts a str filename and bytes object 345 """Abstract method. Accepts a str filename and bytes object
340 representing the bytecode for the module. Returns a boolean 346 representing the bytecode for the module. Returns a boolean
341 representing whether the bytecode was written or not.""" 347 representing whether the bytecode was written or not."""
342 raise NotImplementedError 348 raise NotImplementedError
OLDNEW
« no previous file with comments | « no previous file | Lib/importlib/test/source/test_abc_loader.py » ('j') | Lib/importlib/test/source/test_abc_loader.py » ('J')

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7