Skip to content

Commit f0d48b2

Browse files
[3.13] gh-135276: Refresh zipfile.Path from zipp 3.23 (GH-135277) (#135279)
* gh-135276: Refresh `zipfile.Path` from zipp 3.23 (GH-135277) Apply changes from zipp 3.23 (cherry picked from commit 8d6eb0c) Co-authored-by: Jason R. Coombs <jaraco@jaraco.com> * Removed features slated for Python 3.15 only. --------- Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
1 parent b13720e commit f0d48b2

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

Lib/test/test_zipfile/_path/_test_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import types
21
import functools
2+
import types
33

44
from ._itertools import always_iterable
55

Lib/test/test_zipfile/_path/test_complexity.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88

99
from ._functools import compose
1010
from ._itertools import consume
11-
1211
from ._support import import_or_skip
1312

14-
1513
big_o = import_or_skip('big_o')
1614
pytest = import_or_skip('pytest')
1715

Lib/test/test_zipfile/_path/test_path.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import contextlib
12
import io
23
import itertools
3-
import contextlib
44
import pathlib
55
import pickle
66
import stat
@@ -10,12 +10,11 @@
1010
import zipfile
1111
import zipfile._path
1212

13-
from test.support.os_helper import temp_dir, FakePath
13+
from test.support.os_helper import FakePath, temp_dir
1414

1515
from ._functools import compose
1616
from ._itertools import Counter
17-
18-
from ._test_params import parameterize, Invoked
17+
from ._test_params import Invoked, parameterize
1918

2019

2120
class jaraco:
@@ -194,10 +193,10 @@ def test_encoding_warnings(self, alpharep):
194193
"""EncodingWarning must blame the read_text and open calls."""
195194
assert sys.flags.warn_default_encoding
196195
root = zipfile.Path(alpharep)
197-
with self.assertWarns(EncodingWarning) as wc:
196+
with self.assertWarns(EncodingWarning) as wc: # noqa: F821 (astral-sh/ruff#13296)
198197
root.joinpath("a.txt").read_text()
199198
assert __file__ == wc.filename
200-
with self.assertWarns(EncodingWarning) as wc:
199+
with self.assertWarns(EncodingWarning) as wc: # noqa: F821 (astral-sh/ruff#13296)
201200
root.joinpath("a.txt").open("r").close()
202201
assert __file__ == wc.filename
203202

@@ -365,6 +364,17 @@ def test_root_name(self, alpharep):
365364
root = zipfile.Path(alpharep)
366365
assert root.name == 'alpharep.zip' == root.filename.name
367366

367+
@pass_alpharep
368+
def test_root_on_disk(self, alpharep):
369+
"""
370+
The name/stem of the root should match the zipfile on disk.
371+
372+
This condition must hold across platforms.
373+
"""
374+
root = zipfile.Path(self.zipfile_ondisk(alpharep))
375+
assert root.name == 'alpharep.zip' == root.filename.name
376+
assert root.stem == 'alpharep' == root.filename.stem
377+
368378
@pass_alpharep
369379
def test_suffix(self, alpharep):
370380
"""
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
from . import test_path
22

3-
43
__name__ == '__main__' and test_path.build_alpharep_fixture().extractall('alpharep')

Lib/zipfile/_path/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
for more detail.
88
"""
99

10+
import contextlib
1011
import io
11-
import posixpath
12-
import zipfile
1312
import itertools
14-
import contextlib
1513
import pathlib
14+
import posixpath
1615
import re
1716
import stat
1817
import sys
18+
import zipfile
1919

2020
from .glob import Translator
2121

22-
2322
__all__ = ['Path']
2423

2524

@@ -192,11 +191,13 @@ def _name_set(self):
192191
self.__lookup = super()._name_set()
193192
return self.__lookup
194193

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

202203

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

353354
def _base(self):
354-
return pathlib.PurePosixPath(self.at or self.root.filename)
355+
return pathlib.PurePosixPath(self.at) if self.at else self.filename
355356

356357
@property
357358
def name(self):

Lib/zipfile/_path/glob.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import re
33

4-
54
_default_seps = os.sep + str(os.altsep) * bool(os.altsep)
65

76

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Backported bugfixes in zipfile.Path from zipp 3.23. Fixed
2+
``.name``, ``.stem`` and other basename-based properties on Windows when
3+
working with a zipfile on disk.

0 commit comments

Comments
 (0)