Skip to content

Commit 66c5fa9

Browse files
committed
gh-152762: Fix zipfile.Path.is_symlink() raising KeyError for missing 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.
1 parent 0a13efc commit 66c5fa9

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

Lib/test/test_zipfile/_path/test_path.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,18 @@ def test_is_symlink(self, alpharep):
551551
assert not root.joinpath('a.txt').is_symlink()
552552
assert root.joinpath('n.txt').is_symlink()
553553

554+
@pass_alpharep
555+
def test_is_symlink_missing(self, alpharep):
556+
"""
557+
is_symlink returns False for entries missing from the archive
558+
(including the root and implied directories) rather than
559+
raising KeyError.
560+
"""
561+
root = zipfile.Path(alpharep)
562+
assert not root.is_symlink()
563+
assert not root.joinpath('b').is_symlink()
564+
assert not root.joinpath('missing.txt').is_symlink()
565+
554566
@pass_alpharep
555567
def test_relative_to(self, alpharep):
556568
root = zipfile.Path(alpharep)

Lib/zipfile/_path/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,10 @@ def is_symlink(self):
414414
"""
415415
Return whether this path is a symlink.
416416
"""
417-
info = self.root.getinfo(self.at)
417+
try:
418+
info = self.root.getinfo(self.at)
419+
except KeyError:
420+
return False
418421
mode = info.external_attr >> 16
419422
return stat.S_ISLNK(mode)
420423

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :meth:`zipfile.Path.is_symlink` raising :exc:`KeyError` for paths not
2+
present in the archive. It now returns ``False``, consistent with the other
3+
:class:`zipfile.Path` predicates and with :meth:`pathlib.Path.is_symlink`.

0 commit comments

Comments
 (0)