This commit is contained in:
Nadeshiko Manju 2025-12-23 17:54:23 +09:00 committed by GitHub
commit e2cf1650af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 18 additions and 8 deletions

View file

@ -489,25 +489,27 @@ class _Stream:
# taken from gzip.GzipFile with some alterations
if self.__read(2) != b"\037\213":
raise ReadError("not a gzip file")
if self.__read(1) != b"\010":
method, flag = struct.unpack("<BB6x", self.__read(8))
if method != 8:
raise CompressionError("unsupported compression method")
flag = ord(self.__read(1))
self.__read(6)
# process FEXTRA
if flag & 4:
xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
self.read(xlen)
extra_len, = struct.unpack("<H", self.__read(2))
self.__read(extra_len)
# process FNAME
if flag & 8:
while True:
s = self.__read(1)
if not s or s == NUL:
if not s or s == b'\000':
break
# process FCOMMENT
if flag & 16:
while True:
s = self.__read(1)
if not s or s == NUL:
if not s or s == b'\000':
break
# process FHCRC
if flag & 2:
self.__read(2)

View file

@ -49,6 +49,7 @@ def sha256sum(data):
TEMPDIR = os.path.abspath(os_helper.TESTFN) + "-tardir"
tarextdir = TEMPDIR + '-extract-test'
tarname = support.findfile("testtar.tar", subdir="archivetestdata")
tgzname_with_comment_extra_data_in_header = support.findfile("tgz_with_comment_extra_data_in_header.tgz", subdir="archivetestdata")
gzipname = os.path.join(TEMPDIR, "testtar.tar.gz")
bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2")
xzname = os.path.join(TEMPDIR, "testtar.tar.xz")
@ -928,6 +929,11 @@ class StreamReadTest(CommonReadTest, unittest.TestCase):
if not buf:
break
@unittest.skipIf(zlib is None, "requires zlib")
def test_read_with_extra_header(self):
with tarfile.open(tgzname_with_comment_extra_data_in_header,
mode="r|*") as _:
pass
def test_fileobj_regular_file(self):
tarinfo = self.tar.next() # get "regtype" (can't use getmember)
with self.tar.extractfile(tarinfo) as fobj:

View file

@ -0,0 +1,2 @@
Fix :mod:`tarfile` stream mode exception when process the file with extra header.
data.