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

Usage of zipp.Path causes attempt to write after ZipFile is closed #57

Closed
nwh opened this issue Jun 22, 2020 · 1 comment · Fixed by #59
Closed

Usage of zipp.Path causes attempt to write after ZipFile is closed #57

nwh opened this issue Jun 22, 2020 · 1 comment · Fixed by #59

Comments

@nwh
Copy link

nwh commented Jun 22, 2020

Hello! I am attempting to use zipp.Path to construct a zip archive. I encountered the following issue:

import io
from zipfile import ZipFile
import zipp

def make_zip():
    """Make zip file and return bytes."""
    bytes_io = io.BytesIO()
    zip_file = ZipFile(bytes_io, mode="w")
    zipp_path = zipp.Path(zip_file)
    
    # use zipp.Path.open
    with zipp_path.joinpath("file-a").open(mode="wb") as fp:
        fp.write(b"contents of file-a")

    zip_file.close()

    data = bytes_io.getvalue()

    bytes_io.close()
    
    return data

zip_data = make_zip()
# Exception ignored in: <bound method ZipFile.__del__ of <zipp.CompleteDirs [closed]>>
# Traceback (most recent call last):
#   File "/Users/nw_henderson/.pyenv/versions/3.6.10/lib/python3.6/zipfile.py", line 1686, in __del__
#     self.close()
#   File "/Users/nw_henderson/.pyenv/versions/3.6.10/lib/python3.6/zipfile.py", line 1703, in close
#     self.fp.seek(self.start_dir)
# ValueError: I/O operation on closed file.

Following the zipfile docs, I call close() before extracting the data from bytes_io. It looks like zipp.Path keeps some other reference to the file object and attempts to write when make_zip returns and objects are cleaned up. Python 3.8 exhibits the same issue.

@jaraco
Copy link
Owner

jaraco commented Jul 24, 2020

Thanks for the report. I'm following the issue upstream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants