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: GZip library doesn't properly close files
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 2.7
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: Jake Lever, iritkatriel
Priority: normal Keywords:

Created on 2017-10-03 23:48 by Jake Lever, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
gzipBug.py Jake Lever, 2017-10-03 23:48 Example code
Messages (3)
msg303662 - (view) Author: Jake Lever (Jake Lever) Date: 2017-10-03 23:48
The attached code is designed to output compressed data to a gzip file. It creates two GzipFile objects, but only one is really used. It seems that it doesn't clean up and close the file properly.

In Py2, the attached code will fail. The output file (debug.txt.gz) will be corrupt. However if the self.unused line is commented out, it works and the file is not corrupted.

In Py3, a sys.exit call at the end is required to see the same behaviour.


As example output, when the sys.exit is include in Py3, the output is below and the file is corrupt.
Compressor.__init__
UnusedClass.write
UnusedClass.write
UnusedClass.write
UnusedClass.write
UnusedClass.write
UnusedClass.write
Compressor.compressToFile

And when the sys.exit is commented out, the console output is below and the file is not corrupt.

Compressor.__init__
UnusedClass.write
UnusedClass.write
UnusedClass.write
UnusedClass.write
UnusedClass.write
UnusedClass.write
Compressor.compressToFile
UnusedClass.write
UnusedClass.write
UnusedClass.write
msg376725 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-09-11 11:46
The documentation states that: 

Calling a GzipFile object’s close() method does not close fileobj, since you might wish to append more material after the compressed data. This also allows you to pass an io.BytesIO object opened for writing as fileobj, and retrieve the resulting memory buffer using the io.BytesIO object’s getvalue() method.

(https://docs.python.org/3.8/library/gzip.html)


It's reasonable that, since you opened the file, you would also be responsible for closing it.
msg376728 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-09-11 12:18
As for the bug you report - I was unable to reproduce it on windows (python 3.10) or linux (python 3.7).  Can you check again to see if you still observe it, and give info on the system and python version if so?
History
Date User Action Args
2022-04-11 14:58:53adminsetgithub: 75867
2020-10-20 10:02:11iritkatrielsetstatus: open -> closed
resolution: works for me
stage: resolved
2020-09-11 12:18:31iritkatrielsetmessages: + msg376728
2020-09-11 11:46:45iritkatrielsetnosy: + iritkatriel
messages: + msg376725
components: + Library (Lib)
2017-10-03 23:48:08Jake Levercreate