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
zipfile.ZipFile is closed when zipfile.Path is closed #88804
Comments
When executing the code below with the attached zip file (or any other that has one or more files directly at root level), I get a "ValueError: seek of closed file". It seems, the zipfile handle being part of the
|
This seems similar to https://bugs.python.org/issue40564 |
I'm not able to reproduce this on my machine; the script runs without any issue.
What do you mean by this statement? You aren't doing anything to TestClass or its instance ("test") in this script. They remain in scope, so they will always be referenced. |
I work on macOS 11.4 (20F71) (Kernel Version: Darwin 20.5.0). Let me try to clarify what I mean. test = TestClass(root) # this creates a zipfile handle (an instance of zipfile.ZipFile) at test.zip_file
files = test.iter_dir() # this creates multiple instances of zipfile.Path() as part of the list comprehension and these are deferenced afterwards. I found that test.zip_file.fp is closed after this line executes, which to me suggests that the closing of the zipfile.Path also closes the zipfile.ZipFile that was used to create the zipfile.Path.
test.read(files[0]) # this should in theory try to read from the test.zip_file for the first time, but fails because it is closed as per the above. Here is the full stack trace:
Traceback (most recent call last):
File "test.py", line 20, in <module>
test.read(files[0])
File "test.py", line 12, in read
with self.zip_file.open(filename) as file:
File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1530, in open
fheader = zef_file.read(sizeFileHeader)
File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 763, in read
self._file.seek(self._pos)
ValueError: seek of closed file |
I was able to replicate the error using the script as posted:
|
Here's a much simpler repro that avoids the class construction but triggers the same error:
|
Even simpler:
|
This also reproduces the failure:
Removing |
Even simpler:
|
Changing the repro to:
I'm able now to test against zipfile or zipp. And I notice that the issue occurs only on zipp<3.2 or Python<3.10.
Looking at the changelog (https://zipp.readthedocs.io/en/latest/history.html#v3-2-0), it's clear now that this issue is a duplicate of bpo-40564 and the problem goes away using the original repro and Python 3.10:
The solution is to use zipp>=3.2 or Python 3.10. |
It's not enough to have |
Thank you for the in depth look Jason! |
If a backport isn't desired (as mentioned in #84744 (comment)), would it at least be possible to put a note about this bug into the |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: