Skip to content

gh-152762: Fix zipfile.Path.is_symlink() raising KeyError for missing entries#152924

Open
UditDewan wants to merge 1 commit into
python:mainfrom
UditDewan:gh-152762-zipfile-path-is-symlink
Open

gh-152762: Fix zipfile.Path.is_symlink() raising KeyError for missing entries#152924
UditDewan wants to merge 1 commit into
python:mainfrom
UditDewan:gh-152762-zipfile-path-is-symlink

Conversation

@UditDewan

@UditDewan UditDewan commented Jul 3, 2026

Copy link
Copy Markdown

zipfile.Path.is_symlink() called ZipFile.getinfo() unguarded, so it raised KeyError for any path not present in the archive:

import io, zipfile

buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
    zf.writestr("present.txt", b"x")

buf.seek(0)
with zipfile.ZipFile(buf) as zf:
    p = zipfile.Path(zf) / "missing.txt"
    p.exists()      # False
    p.is_file()     # False
    p.is_dir()      # False
    p.is_symlink()  # KeyError: "There is no item named 'missing.txt' in the archive"

The same KeyError was raised for the archive root (zipfile.Path(zf)) and for implied directories, since neither has a ZipInfo entry backing it.

This change catches the KeyError from getinfo() and returns False, so is_symlink() behaves like the neighboring predicates (exists(), is_file(), is_dir()) and like pathlib.Path.is_symlink() on a missing filesystem path. Existing symlink entries are unaffected.

Added a test covering the missing-entry, root, and implied-directory cases (all raise KeyError without the fix), and a NEWS entry.

…issing entries

Treat entries not present in the archive (including the archive root
and implied directories) as non-symlinks, matching the boolean-predicate
behavior of exists(), is_file(), and is_dir(), as well as
pathlib.Path.is_symlink() on a missing filesystem path.
@UditDewan UditDewan requested a review from jaraco as a code owner July 3, 2026 03:23
@python-cla-bot

python-cla-bot Bot commented Jul 3, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant