|
18 | 18 |
|
19 | 19 | # Bootstrap-related code ###################################################### |
20 | 20 |
|
| 21 | +# TODO: Expose from marshal |
| 22 | +def _w_long(x): |
| 23 | + """Convert a 32-bit integer to little-endian. |
| 24 | +
|
| 25 | + XXX Temporary until marshal's long functions are exposed. |
| 26 | +
|
| 27 | + """ |
| 28 | + x = int(x) |
| 29 | + int_bytes = [] |
| 30 | + int_bytes.append(x & 0xFF) |
| 31 | + int_bytes.append((x >> 8) & 0xFF) |
| 32 | + int_bytes.append((x >> 16) & 0xFF) |
| 33 | + int_bytes.append((x >> 24) & 0xFF) |
| 34 | + return bytearray(int_bytes) |
| 35 | + |
| 36 | + |
| 37 | +# TODO: Expose from marshal |
| 38 | +def _r_long(int_bytes): |
| 39 | + """Convert 4 bytes in little-endian to an integer. |
| 40 | +
|
| 41 | + XXX Temporary until marshal's long function are exposed. |
| 42 | +
|
| 43 | + """ |
| 44 | + x = int_bytes[0] |
| 45 | + x |= int_bytes[1] << 8 |
| 46 | + x |= int_bytes[2] << 16 |
| 47 | + x |= int_bytes[3] << 24 |
| 48 | + return x |
| 49 | + |
| 50 | + |
| 51 | + |
21 | 52 | # XXX Could also expose Modules/getpath.c:joinpath() |
22 | 53 | def _path_join(*args): |
23 | 54 | """Replacement for os.path.join.""" |
@@ -353,14 +384,14 @@ def _bytes_from_bytecode(self, fullname, data, source_stats): |
353 | 384 | except KeyError: |
354 | 385 | pass |
355 | 386 | else: |
356 | | - if marshal._r_long(raw_timestamp) != source_mtime: |
| 387 | + if _r_long(raw_timestamp) != source_mtime: |
357 | 388 | raise ImportError("bytecode is stale for {}".format(fullname)) |
358 | 389 | try: |
359 | 390 | source_size = source_stats['size'] & 0xFFFFFFFF |
360 | 391 | except KeyError: |
361 | 392 | pass |
362 | 393 | else: |
363 | | - if marshal._r_long(raw_size) != source_size: |
| 394 | + if _r_long(raw_size) != source_size: |
364 | 395 | raise ImportError("bytecode is stale for {}".format(fullname)) |
365 | 396 | # Can't return the code object as errors from marshal loading need to |
366 | 397 | # propagate even when source is available. |
@@ -472,8 +503,8 @@ def get_code(self, fullname): |
472 | 503 | # their own cached file format, this block of code will most likely |
473 | 504 | # throw an exception. |
474 | 505 | data = bytearray(imp.get_magic()) |
475 | | - data.extend(marshal._w_long(source_mtime)) |
476 | | - data.extend(marshal._w_long(len(source_bytes))) |
| 506 | + data.extend(_w_long(source_mtime)) |
| 507 | + data.extend(_w_long(len(source_bytes))) |
477 | 508 | data.extend(marshal.dumps(code_object)) |
478 | 509 | try: |
479 | 510 | self.set_data(bytecode_path, data) |
|
0 commit comments