This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Nick Henderson
Recipients Nick Henderson
Date 2020-07-20.16:09:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1595261389.35.0.991424986164.issue41350@roundup.psfhosted.org>
In-reply-to
Content
In both Python 3.8.3 and 3.9.0b3, using zipfile.Path to write a file in a context manager results in an attempt to write to the zip file after it is closed.

In Python 3.9.0b3:

import io
from zipfile import ZipFile, Path

def make_zip():
    """Make zip file and return bytes."""
    bytes_io = io.BytesIO()
    zip_file = ZipFile(bytes_io, mode="w")
    zip_path = Path(zip_file, "file-a")
    
    # use zipp.Path.open
    with zip_path.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: <function ZipFile.__del__ at 0x10aceff70>
# Traceback (most recent call last):
#   File "/Users/nick/.pyenv/versions/3.9.0b3/lib/python3.9/zipfile.py", line 1807, in __del__
#     self.close()
#   File "/Users/nick/.pyenv/versions/3.9.0b3/lib/python3.9/zipfile.py", line 1824, in close
#     self.fp.seek(self.start_dir)
# ValueError: I/O operation on closed file.


In Python 3.8.3:

import io
from zipfile import ZipFile, Path

def make_zip():
    """Make zip file and return bytes."""
    bytes_io = io.BytesIO()
    zip_file = ZipFile(bytes_io, mode="w")
    zip_path = Path(zip_file, "file-a")
    
    # use zipp.Path.open
    with zip_path.open(mode="w") 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: <function ZipFile.__del__ at 0x10922e670>
# Traceback (most recent call last):
#   File "/Users/nick/.pyenv/versions/3.8.3/lib/python3.8/zipfile.py", line 1820, in __del__
#     self.close()
#   File "/Users/nick/.pyenv/versions/3.8.3/lib/python3.8/zipfile.py", line 1837, in close
#     self.fp.seek(self.start_dir)
# ValueError: I/O operation on closed file.

In the Python 3.8 example, mode="w" is used in the open method on zip_path.
History
Date User Action Args
2020-07-20 16:09:49Nick Hendersonsetrecipients: + Nick Henderson
2020-07-20 16:09:49Nick Hendersonsetmessageid: <1595261389.35.0.991424986164.issue41350@roundup.psfhosted.org>
2020-07-20 16:09:49Nick Hendersonlinkissue41350 messages
2020-07-20 16:09:49Nick Hendersoncreate