Skip to content

gh-74468: Fix name attribute of extracted object #1483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,16 +601,17 @@ class _FileInFile(object):
object.
"""

def __init__(self, fileobj, offset, size, blockinfo=None):
def __init__(self, fileobj, tarinfo):
self.fileobj = fileobj
self.offset = offset
self.size = size
self.offset = tarinfo.offset_data
self.size = tarinfo.size
self.position = 0
self.name = getattr(fileobj, "name", None)
self.name = tarinfo.name
self.closed = False
blockinfo = tarinfo.sparse

if blockinfo is None:
blockinfo = [(0, size)]
blockinfo = [(0, self.size)]

# Construct a map with data and zero blocks.
self.map_index = 0
Expand Down Expand Up @@ -702,8 +703,7 @@ def close(self):
class ExFileObject(io.BufferedReader):

def __init__(self, tarfile, tarinfo):
fileobj = _FileInFile(tarfile.fileobj, tarinfo.offset_data,
tarinfo.size, tarinfo.sparse)
fileobj = _FileInFile(tarfile.fileobj, tarinfo)
super().__init__(fileobj)
#class ExFileObject

Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,13 @@ def test_length_zero_header(self):
with tarfile.open(support.findfile('recursion.tar')) as tar:
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):
def requires_name_attribute(self):
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Attribute name of the extracted :mod:`tarfile` file object now holds
filename of itself rather than then of the archive it is contained in. Patch
by Ondřej Kubečka.