cpython: 220c70519958 (original) (raw)
Mercurial > cpython
changeset 104834:220c70519958
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. [#28449]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Sun, 30 Oct 2016 20:58:31 +0200 |
parents | 6e8183abcc35(current diff)de8e83262644(diff) |
children | 11cb97de3edd |
files | Misc/NEWS |
diffstat | 3 files changed, 14 insertions(+), 2 deletions(-)[+] [-] Lib/tarfile.py 4 Lib/test/test_tarfile.py 8 Misc/NEWS 4 |
line wrap: on
line diff
--- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1554,7 +1554,9 @@ class TarFile(object): if mode in ("r", "r:*"): # Find out which *open() is appropriate for opening the file.
for comptype in cls.OPEN_METH:[](#l1.7)
def not_compressed(comptype):[](#l1.8)
return cls.OPEN_METH[comptype] == 'taropen'[](#l1.9)
for comptype in sorted(cls.OPEN_METH, key=not_compressed):[](#l1.10) func = getattr(cls, cls.OPEN_METH[comptype])[](#l1.11) if fileobj is not None:[](#l1.12) saved_pos = fileobj.tell()[](#l1.13)
--- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -3,6 +3,7 @@ import os import io from hashlib import md5 from contextlib import contextmanager +from random import Random import unittest import unittest.mock @@ -349,12 +350,17 @@ class CommonReadTest(ReadTest): def test_ignore_zeros(self): # Test TarFile's ignore_zeros option.
# generate 512 pseudorandom bytes[](#l2.15)
data = Random(0).getrandbits(512*8).to_bytes(512, 'big')[](#l2.16) for char in (b'\0', b'a'):[](#l2.17) # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')[](#l2.18) # are ignored correctly.[](#l2.19) with self.open(tmpname, "w") as fobj:[](#l2.20) fobj.write(char * 1024)[](#l2.21)
fobj.write(tarfile.TarInfo("foo").tobuf())[](#l2.22)
tarinfo = tarfile.TarInfo("foo")[](#l2.23)
tarinfo.size = len(data)[](#l2.24)
fobj.write(tarinfo.tobuf())[](#l2.25)
fobj.write(data)[](#l2.26)
tar = tarfile.open(tmpname, mode="r", ignore_zeros=True) try:
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -99,6 +99,10 @@ Core and Builtins Library ------- +- Issue #28449: tarfile.open() with mode "r" or "r:" now tries to open a tar