[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

Nick Henderson report at bugs.python.org
Mon Jul 20 12:09:49 EDT 2020


New submission from Nick Henderson <nick.henderson at gmail.com>:

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.

----------
components: Library (Lib)
files: zippath_bug_39.py
messages: 374015
nosy: Nick Henderson
priority: normal
severity: normal
status: open
title: Use of zipfile.Path causes attempt to write after ZipFile is closed
type: behavior
versions: Python 3.8, Python 3.9
Added file: https://bugs.python.org/file49329/zippath_bug_39.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41350>
_______________________________________


More information about the Python-bugs-list mailing list