changeset: 104832:e2dd0f48e643 branch: 2.7 parent: 104816:cf91d48aa353 user: Serhiy Storchaka date: Sun Oct 30 20:52:55 2016 +0200 files: Lib/tarfile.py Lib/test/test_tarfile.py Misc/NEWS description: Issue #28449: tarfile.open() with mode "r" or "r:" now tries to open a tar file with compression before trying to open it without compression. Otherwise it had 50% chance failed with ignore_zeros=True. diff -r cf91d48aa353 -r e2dd0f48e643 Lib/tarfile.py --- a/Lib/tarfile.py Sun Oct 30 05:41:04 2016 +0000 +++ b/Lib/tarfile.py Sun Oct 30 20:52:55 2016 +0200 @@ -1665,7 +1665,9 @@ if mode in ("r", "r:*"): # Find out which *open() is appropriate for opening the file. - for comptype in cls.OPEN_METH: + def not_compressed(comptype): + return cls.OPEN_METH[comptype] == 'taropen' + for comptype in sorted(cls.OPEN_METH, key=not_compressed): func = getattr(cls, cls.OPEN_METH[comptype]) if fileobj is not None: saved_pos = fileobj.tell() diff -r cf91d48aa353 -r e2dd0f48e643 Lib/test/test_tarfile.py --- a/Lib/test/test_tarfile.py Sun Oct 30 05:41:04 2016 +0000 +++ b/Lib/test/test_tarfile.py Sun Oct 30 20:52:55 2016 +0200 @@ -2,7 +2,9 @@ import os import shutil import StringIO +from binascii import unhexlify from hashlib import md5 +from random import Random import errno import unittest @@ -276,12 +278,17 @@ else: _open = open + # generate 512 pseudorandom bytes + data = unhexlify('%1024x' % Random(0).getrandbits(512*8)) for char in ('\0', 'a'): # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a') # are ignored correctly. with _open(tmpname, "wb") as fobj: fobj.write(char * 1024) - fobj.write(tarfile.TarInfo("foo").tobuf()) + tarinfo = tarfile.TarInfo("foo") + tarinfo.size = len(data) + fobj.write(tarinfo.tobuf()) + fobj.write(data) tar = tarfile.open(tmpname, mode="r", ignore_zeros=True) try: diff -r cf91d48aa353 -r e2dd0f48e643 Misc/NEWS --- a/Misc/NEWS Sun Oct 30 05:41:04 2016 +0000 +++ b/Misc/NEWS Sun Oct 30 20:52:55 2016 +0200 @@ -60,6 +60,10 @@ Library ------- +- Issue #28449: tarfile.open() with mode "r" or "r:" now tries to open a tar + file with compression before trying to open it without compression. Otherwise + it had 50% chance failed with ignore_zeros=True. + - Issue #25464: Fixed HList.header_exists() in Tix module by adding a workaround to Tix library bug.