summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-10-30 18:52:29 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-10-30 18:52:29 (GMT)
commita89d22aff3fb1e67f8f08eac22b08a58dfa048a8 (patch)
tree2324a9b41897d39e8b10e28f39a60ee5186c27fe
parentd1af5effc214f474bcb1df62eb2089c48f657ee5 (diff)
downloadcpython-a89d22aff3fb1e67f8f08eac22b08a58dfa048a8.zip
cpython-a89d22aff3fb1e67f8f08eac22b08a58dfa048a8.tar.gz
cpython-a89d22aff3fb1e67f8f08eac22b08a58dfa048a8.tar.bz2
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.
-rwxr-xr-xLib/tarfile.py4
-rw-r--r--Lib/test/test_tarfile.py8
-rw-r--r--Misc/NEWS4
3 files changed, 14 insertions, 2 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 721f9d7..999a99b 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1549,7 +1549,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:
+ 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 --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index abfb34d..1efb841 100644
--- 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
+ 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 --git a/Misc/NEWS b/Misc/NEWS
index f7226fd..298d3aa 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -113,6 +113,10 @@ Core and Builtins
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.