Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-59110: Extract function for _add_implicit_dirs. #121378

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions Lib/zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
if add_count:
_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
Expand Down
Loading