Skip to content
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

Mutate the passed ZipFile object type instead of making a copy. #59

Merged
merged 4 commits into from
Sep 22, 2020
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
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
v3.2.0
======

#57 and bpo-40564: Mutate the passed ZipFile object
type instead of making a copy. Prevents issues when
both the local copy and the caller's copy attempt to
close the same file handle.

v3.1.0
======

Expand Down
6 changes: 6 additions & 0 deletions test_zipp.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,9 @@ def test_joinpath_constant_time(self):
def test_implied_dirs_performance(self):
data = ['/'.join(string.ascii_lowercase + str(n)) for n in range(10000)]
zipp.CompleteDirs._implied_dirs(data)

def test_read_does_not_close(self):
for alpharep in self.zipfile_ondisk():
with zipfile.ZipFile(alpharep) as file:
for rep in range(2):
zipp.Path(file, 'a.txt').read_text()
14 changes: 11 additions & 3 deletions zipp.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ def make(cls, source):
if 'r' not in source.mode:
cls = CompleteDirs

res = cls.__new__(cls)
vars(res).update(vars(source))
return res
source.__class__ = cls
return source


class FastLookup(CompleteDirs):
Expand Down Expand Up @@ -211,6 +210,15 @@ class Path:
__repr = "{self.__class__.__name__}({self.root.filename!r}, {self.at!r})"

def __init__(self, root, at=""):
"""
Construct a Path from a ZipFile or filename.

Note: When the source is an existing ZipFile object,
its type (__class__) will be mutated to a
specialized type. If the caller wishes to retain the
original type, the caller should either create a
separate ZipFile object or pass a filename.
"""
self.root = FastLookup.make(root)
self.at = at

Expand Down