gh-74468: [tarfile] Fix incorrect name attribute of ExFileObject (GH-102424)

Co-authored-by: Simeon Visser <svisser@users.noreply.github.com>
This commit is contained in:
Oleg Iarygin 2023-03-28 03:21:07 +04:00 committed by GitHub
parent 89e67ada69
commit 56d055a0d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 3 deletions

View File

@ -601,12 +601,12 @@ class _FileInFile(object):
object. object.
""" """
def __init__(self, fileobj, offset, size, blockinfo=None): def __init__(self, fileobj, offset, size, name, blockinfo=None):
self.fileobj = fileobj self.fileobj = fileobj
self.offset = offset self.offset = offset
self.size = size self.size = size
self.position = 0 self.position = 0
self.name = getattr(fileobj, "name", None) self.name = name
self.closed = False self.closed = False
if blockinfo is None: if blockinfo is None:
@ -703,7 +703,7 @@ class ExFileObject(io.BufferedReader):
def __init__(self, tarfile, tarinfo): def __init__(self, tarfile, tarinfo):
fileobj = _FileInFile(tarfile.fileobj, tarinfo.offset_data, fileobj = _FileInFile(tarfile.fileobj, tarinfo.offset_data,
tarinfo.size, tarinfo.sparse) tarinfo.size, tarinfo.name, tarinfo.sparse)
super().__init__(fileobj) super().__init__(fileobj)
#class ExFileObject #class ExFileObject

View File

@ -479,6 +479,13 @@ class CommonReadTest(ReadTest):
with tarfile.open(support.findfile('recursion.tar')) as tar: with tarfile.open(support.findfile('recursion.tar')) as tar:
pass pass
def test_extractfile_name(self):
# gh-74468: TarFile.name must name a file, not a parent archive.
file = self.tar.getmember('ustar/regtype')
with self.tar.extractfile(file) as fobj:
self.assertEqual(fobj.name, 'ustar/regtype')
class MiscReadTestBase(CommonReadTest): class MiscReadTestBase(CommonReadTest):
def requires_name_attribute(self): def requires_name_attribute(self):
pass pass

View File

@ -0,0 +1,3 @@
Attribute name of the extracted :mod:`tarfile` file object now holds
filename of itself rather than of the archive it is contained in.
Patch by Oleg Iarygin.