Skip to content

[3.14] gh-135276: Refresh zipfile.Path from zipp 3.23 (GH-135277) #135278

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

Merged
merged 2 commits into from
Jun 8, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Lib/test/test_zipfile/_path/_test_params.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import types
import functools
import types

from ._itertools import always_iterable

Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_zipfile/_path/test_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

from ._functools import compose
from ._itertools import consume

from ._support import import_or_skip


big_o = import_or_skip('big_o')
pytest = import_or_skip('pytest')

Expand Down
22 changes: 16 additions & 6 deletions Lib/test/test_zipfile/_path/test_path.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import contextlib
import io
import itertools
import contextlib
import pathlib
import pickle
import stat
Expand All @@ -9,12 +9,11 @@
import zipfile
import zipfile._path

from test.support.os_helper import temp_dir, FakePath
from test.support.os_helper import FakePath, temp_dir

from ._functools import compose
from ._itertools import Counter

from ._test_params import parameterize, Invoked
from ._test_params import Invoked, parameterize


class jaraco:
Expand Down Expand Up @@ -193,10 +192,10 @@ def test_encoding_warnings(self, alpharep):
"""EncodingWarning must blame the read_text and open calls."""
assert sys.flags.warn_default_encoding
root = zipfile.Path(alpharep)
with self.assertWarns(EncodingWarning) as wc:
with self.assertWarns(EncodingWarning) as wc: # noqa: F821 (astral-sh/ruff#13296)
root.joinpath("a.txt").read_text()
assert __file__ == wc.filename
with self.assertWarns(EncodingWarning) as wc:
with self.assertWarns(EncodingWarning) as wc: # noqa: F821 (astral-sh/ruff#13296)
root.joinpath("a.txt").open("r").close()
assert __file__ == wc.filename

Expand Down Expand Up @@ -364,6 +363,17 @@ def test_root_name(self, alpharep):
root = zipfile.Path(alpharep)
assert root.name == 'alpharep.zip' == root.filename.name

@pass_alpharep
def test_root_on_disk(self, alpharep):
"""
The name/stem of the root should match the zipfile on disk.

This condition must hold across platforms.
"""
root = zipfile.Path(self.zipfile_ondisk(alpharep))
assert root.name == 'alpharep.zip' == root.filename.name
assert root.stem == 'alpharep' == root.filename.stem

@pass_alpharep
def test_suffix(self, alpharep):
"""
Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_zipfile/_path/write-alpharep.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from . import test_path


__name__ == '__main__' and test_path.build_alpharep_fixture().extractall('alpharep')
15 changes: 8 additions & 7 deletions Lib/zipfile/_path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
for more detail.
"""

import contextlib
import io
import posixpath
import zipfile
import itertools
import contextlib
import pathlib
import posixpath
import re
import stat
import sys
import zipfile

from .glob import Translator


__all__ = ['Path']


Expand Down Expand Up @@ -192,11 +191,13 @@ def _name_set(self):
self.__lookup = super()._name_set()
return self.__lookup


def _extract_text_encoding(encoding=None, *args, **kwargs):
# compute stack level so that the caller of the caller sees any warning.
is_pypy = sys.implementation.name == 'pypy'
stack_level = 3 + is_pypy
# PyPy no longer special cased after 7.3.19 (or maybe 7.3.18)
# See jaraco/zipp#143
is_old_pypi = is_pypy and sys.pypy_version_info < (7, 3, 19)
stack_level = 3 + is_old_pypi
return io.text_encoding(encoding, stack_level), args, kwargs


Expand Down Expand Up @@ -351,7 +352,7 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
return io.TextIOWrapper(stream, encoding, *args, **kwargs)

def _base(self):
return pathlib.PurePosixPath(self.at or self.root.filename)
return pathlib.PurePosixPath(self.at) if self.at else self.filename

@property
def name(self):
Expand Down
1 change: 0 additions & 1 deletion Lib/zipfile/_path/glob.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import re


_default_seps = os.sep + str(os.altsep) * bool(os.altsep)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Backported bugfixes in zipfile.Path from zipp 3.23. Fixed
``.name``, ``.stem`` and other basename-based properties on Windows when
working with a zipfile on disk.
Loading