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.

classification
Title: GzipFile object should raise ValueError on .read() after .close()
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ack, eric.araujo, jfinkels, pitrou
Priority: normal Keywords: easy, patch

Created on 2010-09-03 17:36 by ack, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue9759.patch jfinkels, 2010-09-14 03:34 Patch which changes behavior of GzipFile.read()
issue9759.patch jfinkels, 2010-09-14 03:46 2.7 branch patch
issue9759.patch jfinkels, 2010-09-15 17:34 Patch which changes behavior of GzipFile.read(), .write(), and .flush()
Messages (7)
msg115473 - (view) Author: Alain Kalker (ack) Date: 2010-09-03 17:36
>>> f = open("test2.gz")
>>> d = f.read()
>>> f.close()
>>> e = f.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
import gzip
>>> f = gzip.open("test2.gz")
>>> d = f.read()
>>> f.close()
>>> e = f.read()
>>> e
''

To remain consistent with other file(-like) objects, I think 'GzipFile's should also raise a ValueError in cases like this.
msg116138 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-09-12 00:15
Should this be fixed in 3.1 and 2.7 too?
msg116362 - (view) Author: Jeffrey Finkelstein (jfinkels) * Date: 2010-09-14 03:34
Here is a patch for the py3k branch which adds a check for whether the GzipFile is closed on each call to GzipFile.read(). If the file is closed already, the method raises a ValueError if it is (with the message text copied from the corresponding fileobject's error message). I added an assertion to the test_read() method in Lib/test/test_gzip.py.

The changes will be exactly the same for the 2.7 branch.
msg116363 - (view) Author: Jeffrey Finkelstein (jfinkels) * Date: 2010-09-14 03:46
Here's the 2.7 branch patch.
msg116437 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-15 08:27
You should make sure that all operations (except close() itself) raise ValueError. Currently:

>>> f = gzip.open("test.gz", "rb")
>>> f.close()
>>> f.read()
b''
>>> f.seek(0)
0

Also, you don't have to post a patch for 2.7. We'll do the porting ourselves.
Thanks for contributing!
msg116470 - (view) Author: Jeffrey Finkelstein (jfinkels) * Date: 2010-09-15 17:34
Here's the updated patch, which checks whether the file is closed on each call to read(), write(), and flush(), along with a test.
msg118083 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-06 21:33
Patch committed in r85291 (3.x), and backported to 3.1 (r85293) and 2.7 (r85292). Thank you!
History
Date User Action Args
2022-04-11 14:57:06adminsetgithub: 53968
2010-10-06 21:33:04pitrousetstatus: open -> closed
resolution: fixed
messages: + msg118083

stage: patch review -> resolved
2010-09-15 17:34:58jfinkelssetfiles: + issue9759.patch

messages: + msg116470
2010-09-15 08:27:08pitrousetmessages: + msg116437
stage: needs patch -> patch review
2010-09-14 03:46:18jfinkelssetfiles: + issue9759.patch

messages: + msg116363
2010-09-14 03:34:15jfinkelssetfiles: + issue9759.patch

nosy: + jfinkels
messages: + msg116362

keywords: + patch
2010-09-12 14:59:28pitrousetversions: + Python 3.1, Python 2.7
2010-09-12 00:15:42eric.araujosetnosy: + eric.araujo
messages: + msg116138
2010-09-07 10:16:53r.david.murraysetkeywords: + easy
nosy: + pitrou

versions: + Python 3.2
2010-09-03 19:45:26amaury.forgeotdarcsetstage: needs patch
2010-09-03 17:36:30ackcreate