]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-74468: [tarfile] Fix incorrect name attribute of ExFileObject (GH-102424)
authorOleg Iarygin <oleg@arhadthedev.net>
Mon, 27 Mar 2023 23:21:07 +0000 (03:21 +0400)
committerGitHub <noreply@github.com>
Mon, 27 Mar 2023 23:21:07 +0000 (16:21 -0700)
Co-authored-by: Simeon Visser <svisser@users.noreply.github.com>
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS.d/next/Library/2023-03-04-20-58-29.gh-issue-74468.Ac5Ew_.rst [new file with mode: 0644]

index d686435d90ad1bc7c597b164ac00157ed5ff41dc..b733195c9c56369c7590d4522370d61a3c046fba 100755 (executable)
@@ -601,12 +601,12 @@ class _FileInFile(object):
        object.
     """
 
-    def __init__(self, fileobj, offset, size, blockinfo=None):
+    def __init__(self, fileobj, offset, size, name, blockinfo=None):
         self.fileobj = fileobj
         self.offset = offset
         self.size = size
         self.position = 0
-        self.name = getattr(fileobj, "name", None)
+        self.name = name
         self.closed = False
 
         if blockinfo is None:
@@ -703,7 +703,7 @@ class ExFileObject(io.BufferedReader):
 
     def __init__(self, tarfile, tarinfo):
         fileobj = _FileInFile(tarfile.fileobj, tarinfo.offset_data,
-                tarinfo.size, tarinfo.sparse)
+                tarinfo.size, tarinfo.name, tarinfo.sparse)
         super().__init__(fileobj)
 #class ExFileObject
 
index 75b60e9a50e78afab930a5cfec3b8edd0367298d..39f6f499c818ef59927f24452aeef12b95928ba5 100644 (file)
@@ -479,6 +479,13 @@ class CommonReadTest(ReadTest):
             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
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 (file)
index 0000000..8fad551
--- /dev/null
@@ -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.