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

Skip to content

Commit 39e47f9

Browse files
Issue #17220: Little cleanup of _bootstrap.py.
1 parent bd9b561 commit 39e47f9

2 files changed

Lines changed: 4310 additions & 4344 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,7 @@ def _w_long(x):
4848
XXX Temporary until marshal's long functions are exposed.
4949
5050
"""
51-
x = int(x)
52-
int_bytes = []
53-
int_bytes.append(x & 0xFF)
54-
int_bytes.append((x >> 8) & 0xFF)
55-
int_bytes.append((x >> 16) & 0xFF)
56-
int_bytes.append((x >> 24) & 0xFF)
57-
return bytearray(int_bytes)
51+
return int(x).to_bytes(4, 'little')
5852

5953

6054
# TODO: Expose from marshal
@@ -64,35 +58,25 @@ def _r_long(int_bytes):
6458
XXX Temporary until marshal's long function are exposed.
6559
6660
"""
67-
x = int_bytes[0]
68-
x |= int_bytes[1] << 8
69-
x |= int_bytes[2] << 16
70-
x |= int_bytes[3] << 24
71-
return x
61+
return int.from_bytes(int_bytes, 'little')
7262

7363

7464
def _path_join(*path_parts):
7565
"""Replacement for os.path.join()."""
76-
new_parts = []
77-
for part in path_parts:
78-
if not part:
79-
continue
80-
new_parts.append(part)
81-
if part[-1] not in path_separators:
82-
new_parts.append(path_sep)
83-
return ''.join(new_parts[:-1]) # Drop superfluous path separator.
66+
return path_sep.join([part.rstrip(path_separators)
67+
for part in path_parts if part])
8468

8569

8670
def _path_split(path):
8771
"""Replacement for os.path.split()."""
72+
if len(path_separators) == 1:
73+
front, _, tail = path.rpartition(path_sep)
74+
return front, tail
8875
for x in reversed(path):
8976
if x in path_separators:
90-
sep = x
91-
break
92-
else:
93-
sep = path_sep
94-
front, _, tail = path.rpartition(sep)
95-
return front, tail
77+
front, tail = path.rsplit(x)
78+
return front, tail
79+
return '', path
9680

9781

9882
def _path_is_mode_type(path, mode):
@@ -404,8 +388,8 @@ def _call_with_frames_removed(f, *args, **kwds):
404388
due to the addition of new opcodes).
405389
406390
"""
407-
_RAW_MAGIC_NUMBER = 3250 | ord('\r') << 16 | ord('\n') << 24
408-
_MAGIC_BYTES = bytes(_RAW_MAGIC_NUMBER >> n & 0xff for n in range(0, 25, 8))
391+
_MAGIC_BYTES = (3250).to_bytes(2, 'little') + b'\r\n'
392+
_RAW_MAGIC_NUMBER = int.from_bytes(_MAGIC_BYTES, 'little')
409393

410394
_PYCACHE = '__pycache__'
411395

@@ -1441,7 +1425,7 @@ def _fill_cache(self):
14411425
lower_suffix_contents.add(new_name)
14421426
self._path_cache = lower_suffix_contents
14431427
if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
1444-
self._relaxed_path_cache = set(fn.lower() for fn in contents)
1428+
self._relaxed_path_cache = {fn.lower() for fn in contents}
14451429

14461430
@classmethod
14471431
def path_hook(cls, *loader_details):
@@ -1774,7 +1758,7 @@ def _setup(sys_module, _imp_module):
17741758
setattr(self_module, '_thread', thread_module)
17751759
setattr(self_module, '_weakref', weakref_module)
17761760
setattr(self_module, 'path_sep', path_sep)
1777-
setattr(self_module, 'path_separators', set(path_separators))
1761+
setattr(self_module, 'path_separators', ''.join(path_separators))
17781762
# Constants
17791763
setattr(self_module, '_relax_case', _make_relax_case())
17801764
EXTENSION_SUFFIXES.extend(_imp.extension_suffixes())

0 commit comments

Comments
 (0)