From 1a75b64f9fe6bbec83adf15e31d4fde459e3ba24 Mon Sep 17 00:00:00 2001 From: Ondrej Kubecka Date: Fri, 5 May 2017 20:52:13 +0200 Subject: [PATCH 1/3] bpo-30282: Fix name attribute of extracted object Attribute name of the extracted file object should hold filename of itself rather than then of the archive it is contained in. --- Lib/tarfile.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib/tarfile.py b/Lib/tarfile.py index efc1f3b9f7fb84..41f351c7efb369 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -610,16 +610,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 @@ -711,8 +712,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 From 7fdf779973b3da204fbecf7e03412ab3691e3d64 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Sat, 4 Mar 2023 20:58:44 +0400 Subject: [PATCH 2/3] Add a news entry --- .../next/Library/2023-03-04-20-58-29.gh-issue-74468.Ac5Ew_.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-03-04-20-58-29.gh-issue-74468.Ac5Ew_.rst diff --git a/Misc/NEWS.d/next/Library/2023-03-04-20-58-29.gh-issue-74468.Ac5Ew_.rst b/Misc/NEWS.d/next/Library/2023-03-04-20-58-29.gh-issue-74468.Ac5Ew_.rst new file mode 100644 index 00000000000000..8cc7ae30ed726f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-03-04-20-58-29.gh-issue-74468.Ac5Ew_.rst @@ -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. From e0e603b9641e33eca24607bf620f2b1af90c91ea Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Sat, 4 Mar 2023 21:41:12 +0400 Subject: [PATCH 3/3] Add a test --- Lib/test/test_tarfile.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 75b60e9a50e78a..39f6f499c818ef 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -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