Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit05c4d52

Browse files
committed
gh-98108: Sync with zipp 3.9.1 adding pickleability.
1 parent376febe commit05c4d52

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

‎Lib/test/test_zipfile/test_path.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
importpathlib
55
importunittest
66
importstring
7-
importfunctools
7+
importpickle
8+
importitertools
9+
10+
from ._test_paramsimportparameterize,Invoked
11+
from ._functoolsimportcompose
12+
813

914
fromtest.support.os_helperimporttemp_dir
1015

@@ -76,31 +81,19 @@ def build_alpharep_fixture():
7681
returnzf
7782

7883

79-
defpass_alpharep(meth):
80-
"""
81-
Given a method, wrap it in a for loop that invokes method
82-
with each subtest.
83-
"""
84-
85-
@functools.wraps(meth)
86-
defwrapper(self):
87-
foralpharepinself.zipfile_alpharep():
88-
meth(self,alpharep=alpharep)
84+
alpharep_generators= [
85+
Invoked.wrap(build_alpharep_fixture),
86+
Invoked.wrap(compose(add_dirs,build_alpharep_fixture)),
87+
]
8988

90-
returnwrapper
89+
pass_alpharep=parameterize(['alpharep'],alpharep_generators)
9190

9291

9392
classTestPath(unittest.TestCase):
9493
defsetUp(self):
9594
self.fixtures=contextlib.ExitStack()
9695
self.addCleanup(self.fixtures.close)
9796

98-
defzipfile_alpharep(self):
99-
withself.subTest():
100-
yieldbuild_alpharep_fixture()
101-
withself.subTest():
102-
yieldadd_dirs(build_alpharep_fixture())
103-
10497
defzipfile_ondisk(self,alpharep):
10598
tmpdir=pathlib.Path(self.fixtures.enter_context(temp_dir()))
10699
buffer=alpharep.fp
@@ -418,6 +411,21 @@ def test_root_unnamed(self, alpharep):
418411
@pass_alpharep
419412
deftest_inheritance(self,alpharep):
420413
cls=type('PathChild', (zipfile.Path,), {})
421-
foralpharepinself.zipfile_alpharep():
422-
file=cls(alpharep).joinpath('some dir').parent
423-
assertisinstance(file,cls)
414+
file=cls(alpharep).joinpath('some dir').parent
415+
assertisinstance(file,cls)
416+
417+
@parameterize(
418+
['alpharep','path_type','subpath'],
419+
itertools.product(
420+
alpharep_generators,
421+
[str,pathlib.Path],
422+
['','b/'],
423+
),
424+
)
425+
deftest_pickle(self,alpharep,path_type,subpath):
426+
zipfile_ondisk=path_type(self.zipfile_ondisk(alpharep))
427+
428+
saved_1=pickle.dumps(zipfile.Path(zipfile_ondisk,at=subpath))
429+
restored_1=pickle.loads(saved_1)
430+
first,*rest=restored_1.iterdir()
431+
assertfirst.read_text().startswith('content of ')

‎Lib/zipfile/_path.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,25 @@ def _difference(minuend, subtrahend):
6262
returnitertools.filterfalse(set(subtrahend).__contains__,minuend)
6363

6464

65-
classCompleteDirs(zipfile.ZipFile):
65+
classInitializedState:
66+
"""
67+
Mix-in to save the initialization state for pickling.
68+
"""
69+
70+
def__init__(self,*args,**kwargs):
71+
self.__args=args
72+
self.__kwargs=kwargs
73+
super().__init__(*args,**kwargs)
74+
75+
def__getstate__(self):
76+
returnself.__args,self.__kwargs
77+
78+
def__setstate__(self,state):
79+
args,kwargs=state
80+
super().__init__(*args,**kwargs)
81+
82+
83+
classCompleteDirs(InitializedState,zipfile.ZipFile):
6684
"""
6785
A ZipFile subclass that ensures that implied directories
6886
are always included in the namelist.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``zipfile.Path`` is now pickleable if its initialization parameters were
2+
pickleable (e.g. for file system paths).

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp