Skip to content

Commit

Permalink
[3.12] pythongh-119588: Implement zipfile.Path.is_symlink (zipp 3.19.…
Browse files Browse the repository at this point in the history
…0). (pythonGH-119591)

(cherry picked from commit 42a34dd)

Co-authored-by: Jason R. Coombs <[email protected]>
  • Loading branch information
jaraco committed Jun 3, 2024
1 parent 059be67 commit 53b1fd7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
9 changes: 9 additions & 0 deletions Doc/library/zipfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,15 @@ Path objects are traversable using the ``/`` operator or ``joinpath``.

Return ``True`` if the current context references a file.

.. method:: Path.is_symlink()

Return ``True`` if the current context references a symbolic link.

.. versionadded:: 3.12

.. versionchanged:: 3.12.4
Prior to 3.12.4, ``is_symlink`` would unconditionally return ``False``.

.. method:: Path.exists()

Return ``True`` if the current context references a file or
Expand Down
27 changes: 17 additions & 10 deletions Lib/test/test_zipfile/_path/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import contextlib
import pathlib
import pickle
import stat
import sys
import unittest
import zipfile
Expand Down Expand Up @@ -30,12 +31,17 @@ def add_dirs(zf):
return zf


def _make_link(info: zipfile.ZipInfo): # type: ignore[name-defined]
info.external_attr |= stat.S_IFLNK << 16


def build_alpharep_fixture():
"""
Create a zip file with this structure:
.
├── a.txt
├── n.txt (-> a.txt)
├── b
│ ├── c.txt
│ ├── d
Expand All @@ -56,6 +62,7 @@ def build_alpharep_fixture():
- multiple files in a directory (b/c, b/f)
- a directory containing only a directory (g/h)
- a directory with files of different extensions (j/klm)
- a symlink (n) pointing to (a)
"alpha" because it uses alphabet
"rep" because it's a representative example
Expand All @@ -70,6 +77,9 @@ def build_alpharep_fixture():
zf.writestr("j/k.bin", b"content of k")
zf.writestr("j/l.baz", b"content of l")
zf.writestr("j/m.bar", b"content of m")
zf.writestr("n.txt", b"a.txt")
_make_link(zf.infolist()[-1])

zf.filename = "alpharep.zip"
return zf

Expand Down Expand Up @@ -100,7 +110,7 @@ def zipfile_ondisk(self, alpharep):
def test_iterdir_and_types(self, alpharep):
root = zipfile.Path(alpharep)
assert root.is_dir()
a, b, g, j = root.iterdir()
a, k, b, g, j = root.iterdir()
assert a.is_file()
assert b.is_dir()
assert g.is_dir()
Expand All @@ -120,7 +130,7 @@ def test_is_file_missing(self, alpharep):
@pass_alpharep
def test_iterdir_on_file(self, alpharep):
root = zipfile.Path(alpharep)
a, b, g, j = root.iterdir()
a, k, b, g, j = root.iterdir()
with self.assertRaises(ValueError):
a.iterdir()

Expand All @@ -135,7 +145,7 @@ def test_subdir_is_dir(self, alpharep):
@pass_alpharep
def test_open(self, alpharep):
root = zipfile.Path(alpharep)
a, b, g, j = root.iterdir()
a, k, b, g, j = root.iterdir()
with a.open(encoding="utf-8") as strm:
data = strm.read()
self.assertEqual(data, "content of a")
Expand Down Expand Up @@ -237,7 +247,7 @@ def test_open_missing_directory(self):
@pass_alpharep
def test_read(self, alpharep):
root = zipfile.Path(alpharep)
a, b, g, j = root.iterdir()
a, k, b, g, j = root.iterdir()
assert a.read_text(encoding="utf-8") == "content of a"
# Also check positional encoding arg (gh-101144).
assert a.read_text("utf-8") == "content of a"
Expand Down Expand Up @@ -303,7 +313,7 @@ def test_mutability(self, alpharep):
reflect that change.
"""
root = zipfile.Path(alpharep)
a, b, g, j = root.iterdir()
a, k, b, g, j = root.iterdir()
alpharep.writestr('foo.txt', 'foo')
alpharep.writestr('bar/baz.txt', 'baz')
assert any(child.name == 'foo.txt' for child in root.iterdir())
Expand Down Expand Up @@ -520,12 +530,9 @@ def test_eq_hash(self, alpharep):

@pass_alpharep
def test_is_symlink(self, alpharep):
"""
See python/cpython#82102 for symlink support beyond this object.
"""

root = zipfile.Path(alpharep)
assert not root.is_symlink()
assert not root.joinpath('a.txt').is_symlink()
assert root.joinpath('n.txt').is_symlink()

@pass_alpharep
def test_relative_to(self, alpharep):
Expand Down
7 changes: 5 additions & 2 deletions Lib/zipfile/_path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import contextlib
import pathlib
import re
import stat

from .glob import translate

Expand Down Expand Up @@ -355,9 +356,11 @@ def match(self, path_pattern):

def is_symlink(self):
"""
Return whether this path is a symlink. Always false (python/cpython#82102).
Return whether this path is a symlink.
"""
return False
info = self.root.getinfo(self.at)
mode = info.external_attr >> 16
return stat.S_ISLNK(mode)

def glob(self, pattern):
if not pattern:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``zipfile.Path.is_symlink`` now assesses if the given path is a symlink.

0 comments on commit 53b1fd7

Please sign in to comment.