-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
gzip fails to read a gzipped file (ValueError: readline of closed file) #89638
Comments
Attempting to iterate over an opened gzip file raises a ValueError: readline of closed file Behavior in Python 3.9.7:
Python 3.9.7 (default, Oct 13 2021, 09:08:19)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gzip
>>> ll = [l for l in gzip.GzipFile(filename='data/UTF-8-test_for_gzip.txt.gz')]
>>> len(ll)
300
Behavior in Python 3.10.0 (and 3.11.0a1 is the same):
Python 3.10.0 (default, Oct 13 2021, 08:53:15) [GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gzip
>>> ll = [l for l in gzip.GzipFile(filename='data/UTF-8-test_for_gzip.txt.gz')]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
ValueError: readline of closed file
This only happens when iterating directly over the GzipFile object. Using a with: statement has the correct behaviour in both 3.10 and 3.11:
>>> with gzip.GzipFile(filename='UTF-8-test_for_gzip.txt.gz') as input_file:
... len(list(input_file))
...
300 |
This might be related to below commit : commit d2a8e69
python -m gzip README.rst
(myenv) ➜ cpython git:(main) ✗ git checkout d2a8e69c2c605fbaa3656a5f99aa8d295f74c80e~1 Lib/gzip.py
Updated 1 path from 2ea7c00ab4
(myenv) ➜ cpython git:(main) ✗ ./python
Python 3.11.0a1+ (heads/main:160c38df7f, Oct 15 2021, 11:25:16) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gzip
>>> len([None for _ in gzip.GzipFile("README.rst.gz")])
267
>>>
(myenv) ➜ cpython git:(main) ✗ git checkout d2a8e69c2c605fbaa3656a5f99aa8d295f74c80e Lib/gzip.py
Updated 1 path from 1f9874eec6
(myenv) ➜ cpython git:(main) ✗ ./python
Python 3.11.0a1+ (heads/main:160c38df7f, Oct 15 2021, 11:25:16) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gzip
>>> len([None for _ in gzip.GzipFile("README.rst.gz")])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
ValueError: readline of closed file |
This is bad code pattern because you don't close the file explicitly. Although this is caused by bad code pattern, I must admit this is a regression. |
__iter__
optimization for GzipFile, BZ2File, and LZMAFile. #29016__iter__
optimization for GzipFile, BZ2File, and LZMAFile. (GH-29016) #29050Note: 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: