>>> f = open("test2.gz") >>> d = f.read() >>> f.close() >>> e = f.read() Traceback (most recent call last): File "", line 1, in 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.
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.
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!