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.

Title: Incomplete gzip output with, mode="w:gz")
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3, Python 3.4
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: lars.gustaebel, martin.panter, nadeem.vawda, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2014-01-13 10:24 by martin.panter, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
tarfile_fobj_gz_close.patch serhiy.storchaka, 2014-01-13 14:37 review
Messages (3)
msg208017 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-01-13 10:24
I am trying to create a tar file after opening it as a temporary file, and it seems to be writing truncated output when I use mode="w:gz". My workaround looks like it will be to use mode="w|gz" instead. It’s not clear what the difference is: am I losing anything practical by disallowing “random” seeking?

Simplified demonstration:

>>> from io import BytesIO; b = BytesIO()
>>> import tarfile; t =, mode="w:gz")
>>> t._extfileobj
>>> type(t.fileobj)
<class 'gzip.GzipFile'>
>>> t.close()
>>> b.getvalue()
>>> del t
>>> b.getvalue()

Looking at the code, the TarFile.close() method would not be closing the GzipFile object because of this condition:

if not self._extfileobj:

Perhaps it needs to also check if file compression is being used or something; I’m not familiar with the internals.

I did notice that the bug happens with Python 3.3.3 and 3.2.3, but not 2.7.5. Also, I do not see the issue when a file name is passed to rather than a file object, nor do I see it with the bzip or XZ compressors, uncompressed tar creation, or the “special purposes” w|gz mode.
msg208026 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-01-13 14:37
Thank you Martin for you report.

Here is a patch which fixes this issue.
msg208400 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-01-18 13:56
New changeset 5c69332dc3b0 by Serhiy Storchaka in branch '3.3':
Issue #20238: TarFile opened with external fileobj and "w:gz" mode didn't

New changeset e154b93f3857 by Serhiy Storchaka in branch 'default':
Issue #20238: TarFile opened with external fileobj and "w:gz" mode didn't

New changeset f7381f1bf1ec by Serhiy Storchaka in branch '2.7':
Backported test for issue #20238.
Date User Action Args
2022-04-11 14:57:56adminsetgithub: 64437
2014-01-18 15:18:49serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2014-01-18 13:56:27python-devsetnosy: + python-dev
messages: + msg208400
2014-01-13 20:01:41serhiy.storchakalinkissue20243 dependencies
2014-01-13 14:37:55serhiy.storchakasetfiles: + tarfile_fobj_gz_close.patch
versions: + Python 3.4
messages: + msg208026

assignee: serhiy.storchaka
keywords: + patch
stage: patch review
2014-01-13 12:38:25serhiy.storchakasetnosy: + lars.gustaebel, nadeem.vawda, serhiy.storchaka
2014-01-13 10:24:43martin.pantercreate