(original) (raw)
changeset: 104834:220c70519958 parent: 104830:6e8183abcc35 parent: 104833:de8e83262644 user: Serhiy Storchaka storchaka@gmail.com date: Sun Oct 30 20:58:31 2016 +0200 files: 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 6e8183abcc35 -r 220c70519958 Lib/tarfile.py --- a/Lib/tarfile.py Sun Oct 30 19:38:05 2016 +0200 +++ b/Lib/tarfile.py Sun Oct 30 20:58:31 2016 +0200 @@ -1554,7 +1554,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 6e8183abcc35 -r 220c70519958 Lib/test/test_tarfile.py --- a/Lib/test/test_tarfile.py Sun Oct 30 19:38:05 2016 +0200 +++ b/Lib/test/test_tarfile.py Sun Oct 30 20:58:31 2016 +0200 @@ -3,6 +3,7 @@ import io from hashlib import md5 from contextlib import contextmanager +from random import Random import unittest import unittest.mock @@ -349,12 +350,17 @@ def test_ignore_zeros(self): # Test TarFile's ignore_zeros option. + # generate 512 pseudorandom bytes + data = Random(0).getrandbits(512*8).to_bytes(512, 'big') for char in (b'\0', b'a'): # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a') # are ignored correctly. with self.open(tmpname, "w") 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 6e8183abcc35 -r 220c70519958 Misc/NEWS --- a/Misc/NEWS Sun Oct 30 19:38:05 2016 +0200 +++ b/Misc/NEWS Sun Oct 30 20:58:31 2016 +0200 @@ -99,6 +99,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 #23262: The webbrowser module now supports Firefox 36+ and derived browsers. Based on patch by Oleg Broytman. /storchaka@gmail.com