Skip to content

Commit 56d055a

Browse files
arhadthedevsvisser
andauthored
gh-74468: [tarfile] Fix incorrect name attribute of ExFileObject (GH-102424)
Co-authored-by: Simeon Visser <[email protected]>
1 parent 89e67ad commit 56d055a

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

Lib/tarfile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,12 @@ class _FileInFile(object):
601601
object.
602602
"""
603603

604-
def __init__(self, fileobj, offset, size, blockinfo=None):
604+
def __init__(self, fileobj, offset, size, name, blockinfo=None):
605605
self.fileobj = fileobj
606606
self.offset = offset
607607
self.size = size
608608
self.position = 0
609-
self.name = getattr(fileobj, "name", None)
609+
self.name = name
610610
self.closed = False
611611

612612
if blockinfo is None:
@@ -703,7 +703,7 @@ class ExFileObject(io.BufferedReader):
703703

704704
def __init__(self, tarfile, tarinfo):
705705
fileobj = _FileInFile(tarfile.fileobj, tarinfo.offset_data,
706-
tarinfo.size, tarinfo.sparse)
706+
tarinfo.size, tarinfo.name, tarinfo.sparse)
707707
super().__init__(fileobj)
708708
#class ExFileObject
709709

Lib/test/test_tarfile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,13 @@ def test_length_zero_header(self):
479479
with tarfile.open(support.findfile('recursion.tar')) as tar:
480480
pass
481481

482+
def test_extractfile_name(self):
483+
# gh-74468: TarFile.name must name a file, not a parent archive.
484+
file = self.tar.getmember('ustar/regtype')
485+
with self.tar.extractfile(file) as fobj:
486+
self.assertEqual(fobj.name, 'ustar/regtype')
487+
488+
482489
class MiscReadTestBase(CommonReadTest):
483490
def requires_name_attribute(self):
484491
pass
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Attribute name of the extracted :mod:`tarfile` file object now holds
2+
filename of itself rather than of the archive it is contained in.
3+
Patch by Oleg Iarygin.

0 commit comments

Comments
 (0)