Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c264e3e

Browse files
committed
Move some code from importlib.__init__ to importlib._bootstrap that
does not need to be exposed from C code for bootstrapping reasons.
1 parent b0f30c9 commit c264e3e

4 files changed

Lines changed: 52 additions & 42 deletions

File tree

Lib/importlib/__init__.py

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@
2626
import re
2727
import tokenize
2828

29+
# To simplify imports in test code
30+
_w_long = _bootstrap._w_long
31+
_r_long = _bootstrap._r_long
32+
33+
2934
# Bootstrap help #####################################################
3035

36+
# TODO: Expose from import.c, else handle encode/decode as _os.environ returns
37+
# bytes.
3138
def _case_ok(directory, check):
3239
"""Check if the directory contains something matching 'check'.
3340
@@ -36,37 +43,13 @@ def _case_ok(directory, check):
3643
"""
3744
if 'PYTHONCASEOK' in os.environ:
3845
return True
39-
elif check in os.listdir(directory if directory else os.getcwd()):
46+
if not directory:
47+
directory = os.getcwd()
48+
if check in os.listdir(directory):
4049
return True
4150
return False
4251

43-
44-
def _w_long(x):
45-
"""Convert a 32-bit integer to little-endian.
46-
47-
XXX Temporary until marshal's long functions are exposed.
48-
49-
"""
50-
x = int(x)
51-
int_bytes = []
52-
int_bytes.append(x & 0xFF)
53-
int_bytes.append((x >> 8) & 0xFF)
54-
int_bytes.append((x >> 16) & 0xFF)
55-
int_bytes.append((x >> 24) & 0xFF)
56-
return bytearray(int_bytes)
57-
58-
59-
def _r_long(int_bytes):
60-
"""Convert 4 bytes in little-endian to an integer.
61-
62-
XXX Temporary until marshal's long function are exposed.
63-
64-
"""
65-
x = int_bytes[0]
66-
x |= int_bytes[1] << 8
67-
x |= int_bytes[2] << 16
68-
x |= int_bytes[3] << 24
69-
return x
52+
_bootstrap._case_ok = _case_ok
7053

7154

7255
# Required built-in modules.
@@ -94,10 +77,6 @@ def _r_long(int_bytes):
9477
# For os.path.join replacement; pull from Include/osdefs.h:SEP .
9578
_bootstrap.path_sep = sep
9679

97-
_bootstrap._case_ok = _case_ok
98-
marshal._w_long = _w_long
99-
marshal._r_long = _r_long
100-
10180

10281
# Public API #########################################################
10382

Lib/importlib/_bootstrap.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,37 @@
1818

1919
# Bootstrap-related code ######################################################
2020

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+
2152
# XXX Could also expose Modules/getpath.c:joinpath()
2253
def _path_join(*args):
2354
"""Replacement for os.path.join."""
@@ -353,14 +384,14 @@ def _bytes_from_bytecode(self, fullname, data, source_stats):
353384
except KeyError:
354385
pass
355386
else:
356-
if marshal._r_long(raw_timestamp) != source_mtime:
387+
if _r_long(raw_timestamp) != source_mtime:
357388
raise ImportError("bytecode is stale for {}".format(fullname))
358389
try:
359390
source_size = source_stats['size'] & 0xFFFFFFFF
360391
except KeyError:
361392
pass
362393
else:
363-
if marshal._r_long(raw_size) != source_size:
394+
if _r_long(raw_size) != source_size:
364395
raise ImportError("bytecode is stale for {}".format(fullname))
365396
# Can't return the code object as errors from marshal loading need to
366397
# propagate even when source is available.
@@ -472,8 +503,8 @@ def get_code(self, fullname):
472503
# their own cached file format, this block of code will most likely
473504
# throw an exception.
474505
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)))
477508
data.extend(marshal.dumps(code_object))
478509
try:
479510
self.set_data(bytecode_path, data)

Lib/importlib/abc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def get_code(self, fullname):
260260
raw_timestamp = data[4:8]
261261
if len(raw_timestamp) < 4:
262262
raise EOFError("bad timestamp in {}".format(fullname))
263-
pyc_timestamp = marshal._r_long(raw_timestamp)
263+
pyc_timestamp = _bootstrap._r_long(raw_timestamp)
264264
bytecode = data[8:]
265265
# Verify that the magic number is valid.
266266
if imp.get_magic() != magic:
@@ -292,7 +292,7 @@ def get_code(self, fullname):
292292
# Generate bytecode and write it out.
293293
if not sys.dont_write_bytecode:
294294
data = bytearray(imp.get_magic())
295-
data.extend(marshal._w_long(source_timestamp))
295+
data.extend(_bootstrap._w_long(source_timestamp))
296296
data.extend(marshal.dumps(code_object))
297297
self.write_bytecode(fullname, data)
298298
return code_object

Lib/importlib/test/source/test_abc_loader.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def __init__(self, path, magic=imp.get_magic()):
4242
self.bytecode_path = imp.cache_from_source(self.path)
4343
self.source_size = len(self.source)
4444
data = bytearray(magic)
45-
data.extend(marshal._w_long(self.source_mtime))
46-
data.extend(marshal._w_long(self.source_size))
45+
data.extend(importlib._w_long(self.source_mtime))
46+
data.extend(importlib._w_long(self.source_size))
4747
code_object = compile(self.source, self.path, 'exec',
4848
dont_inherit=True)
4949
data.extend(marshal.dumps(code_object))
@@ -658,8 +658,8 @@ def verify_code(self, code_object, *, bytecode_written=False):
658658
if bytecode_written:
659659
self.assertIn(self.cached, self.loader.written)
660660
data = bytearray(imp.get_magic())
661-
data.extend(marshal._w_long(self.loader.source_mtime))
662-
data.extend(marshal._w_long(self.loader.source_size))
661+
data.extend(importlib._w_long(self.loader.source_mtime))
662+
data.extend(importlib._w_long(self.loader.source_size))
663663
data.extend(marshal.dumps(code_object))
664664
self.assertEqual(self.loader.written[self.cached], bytes(data))
665665

0 commit comments

Comments
 (0)