Skip to content

Commit 7f4b01b

Browse files
Slightly cleaner code
1 parent b7ba389 commit 7f4b01b

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

Lib/importlib/_bootstrap_external.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,12 +1593,7 @@ def __init__(self, path, *loader_details):
15931593
self._path_mtime = -1
15941594
self._path_cache = set()
15951595
self._relaxed_path_cache = set()
1596-
1597-
# Cache in NFKC normalization for imports of non-ASCII
1598-
# names. The regular cache is not kept in NFKC format
1599-
# to avoid circular dependencies during startup.
1600-
self._norm_path_cache = None
1601-
self._norm_relaxed_path_cache = None
1596+
self._cache_is_normalized = False
16021597

16031598
def invalidate_caches(self):
16041599
"""Invalidate the directory mtime."""
@@ -1626,22 +1621,17 @@ def find_spec(self, fullname, target=None):
16261621
# tail_module keeps the original casing, for __file__ and friends
16271622
if _relax_case():
16281623
cache_module = tail_module.lower()
1624+
cache = self._relaxed_path_cache
16291625

1630-
if cache_module.isascii():
1631-
cache = self._relaxed_path_cache
1632-
else:
1633-
if self._norm_relaxed_path_cache is None:
1634-
self._fill_norm_cache()
1635-
cache = self._norm_relaxed_path_cache
1626+
if not cache_module.isascii() and not self._cache_is_normalized:
1627+
self._normalize_cache()
16361628
else:
16371629
cache_module = tail_module
1630+
cache = self._path_cache
1631+
1632+
if cache_module.isascii() and not self._cache_is_normalized:
1633+
self._normalize_cache()
16381634

1639-
if cache_module.isascii():
1640-
cache = self._path_cache
1641-
else:
1642-
if self._norm_path_cache is None:
1643-
self._fill_norm_cache()
1644-
cache = self._norm_path_cache
16451635
# Check if the module is the name of a directory (and thus a package).
16461636
if cache_module in cache:
16471637
base_path = _path_join(self.path, tail_module)
@@ -1703,14 +1693,14 @@ def _fill_cache(self):
17031693
if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
17041694
self._relaxed_path_cache = {fn.lower() for fn in contents}
17051695

1706-
self._norm_path_cache = None
1707-
self._norm_relaxed_path_cache = None
1696+
self._cache_is_normalized = False
17081697

1709-
def _fill_norm_cache(self):
1698+
def _normalize_cache(self):
17101699
from unicodedata import normalize
17111700

1712-
self._norm_path_cache = { normalize('NFKC', p) for p in self._path_cache }
1713-
self._norm_relaxed_path_cache = { normalize('NFKC', p) for p in self._relaxed_path_cache }
1701+
self._path_cache = { normalize('NFKC', p) for p in self._path_cache }
1702+
self._relaxed_path_cache = { normalize('NFKC', p) for p in self._relaxed_path_cache }
1703+
self._cache_is_normalized = True
17141704

17151705

17161706
@classmethod

0 commit comments

Comments
 (0)