From 88182cc64720e828fd3a48184cf57b4e429b0246 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 4 Jul 2024 15:33:54 -0400 Subject: [PATCH 1/2] gh-59110: Extract function for _add_implicit_dirs. --- Lib/zipimport.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/Lib/zipimport.py b/Lib/zipimport.py index a79862f1de7564..cf3c8d2e742618 100644 --- a/Lib/zipimport.py +++ b/Lib/zipimport.py @@ -557,20 +557,35 @@ def _read_directory(archive): fp.seek(start_offset) _bootstrap._verbose_message('zipimport: found {} names in {!r}', count, archive) - # Add implicit directories. - for name in list(files): + add_count = _add_implicit_dirs(files) + _bootstrap._verbose_message('zipimport: added {} implicit directories in {!r}', + add_count, archive) + + + return files + + +def _add_implicit_dirs(files): + # dict.from_keys is doing triple-duty here: + # - deduplicating any entries in O(1) time + # - setting values to `None` + # - representing the __len__ of the updates + updates = dict.fromkeys(_implicit_dirs(files)) + files.update(updates) + return len(updates) + + +def _implicit_dirs(names): + for name in names: while True: i = name.rstrip(path_sep).rfind(path_sep) if i < 0: break name = name[:i + 1] - if name in files: + if name in names: break - files[name] = None - count += 1 - _bootstrap._verbose_message('zipimport: added {} implicit directories in {!r}', - count, archive) - return files + yield name + # During bootstrap, we may need to load the encodings # package from a ZIP file. But the cp437 encoding is implemented From 90d3283b813718fe36b0d82616dbb630de6fd7f6 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 4 Jul 2024 15:39:16 -0400 Subject: [PATCH 2/2] Only render the message if files was updated. --- Lib/zipimport.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/zipimport.py b/Lib/zipimport.py index cf3c8d2e742618..224268e469fe1d 100644 --- a/Lib/zipimport.py +++ b/Lib/zipimport.py @@ -558,9 +558,9 @@ def _read_directory(archive): _bootstrap._verbose_message('zipimport: found {} names in {!r}', count, archive) add_count = _add_implicit_dirs(files) - _bootstrap._verbose_message('zipimport: added {} implicit directories in {!r}', - add_count, archive) - + if add_count: + _bootstrap._verbose_message('zipimport: added {} implicit directories in {!r}', + add_count, archive) return files