]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-81325: Support path-like objects with streaming TarFile (GH-137188) (#137365)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 7 Oct 2025 18:40:42 +0000 (20:40 +0200)
committerGitHub <noreply@github.com>
Tue, 7 Oct 2025 18:40:42 +0000 (20:40 +0200)
gh-81325: Support path-like objects with streaming TarFile (GH-137188)
(cherry picked from commit 3ec3d053452af8a769c18826ea61ba66fc73c8da)

Co-authored-by: Alexander Urieles <aeurielesn@users.noreply.github.com>
Co-authored-by: Emma Smith <emma@emmatyping.dev>
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS.d/next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst [new file with mode: 0644]

index 7dff0122da7775a021894ff87a4618fcb5485cba..c7e9f7d681a8b1c12426183a8309e85513bb2c5b 100644 (file)
@@ -353,7 +353,7 @@ class _Stream:
             fileobj = _StreamProxy(fileobj)
             comptype = fileobj.getcomptype()
 
-        self.name     = name or ""
+        self.name     = os.fspath(name) if name is not None else ""
         self.mode     = mode
         self.comptype = comptype
         self.fileobj  = fileobj
index b23ff34bc327c67c584940a5a037897cffcdb339..860413b88eb6b51960f0a45fae21e6ebf853b1d0 100644 (file)
@@ -1788,6 +1788,16 @@ class StreamWriteTest(WriteTestBase, unittest.TestCase):
         finally:
             os.umask(original_umask)
 
+    def test_pathlike_name(self):
+        expected_name = os.path.abspath(tmpname)
+        tarpath = os_helper.FakePath(tmpname)
+
+        for func in (tarfile.open, tarfile.TarFile.open):
+            with self.subTest():
+                with func(tarpath, self.mode) as tar:
+                    self.assertEqual(tar.name, expected_name)
+                os_helper.unlink(tmpname)
+
 
 class GzipStreamWriteTest(GzipTest, StreamWriteTest):
     def test_source_directory_not_leaked(self):
diff --git a/Misc/NEWS.d/next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst b/Misc/NEWS.d/next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst
new file mode 100644 (file)
index 0000000..3d89b6e
--- /dev/null
@@ -0,0 +1,2 @@
+:class:`tarfile.TarFile` now accepts a :term:`path-like <path-like object>` when working on a tar archive.
+(Contributed by Alexander Enrique Urieles Nieto in :gh:`81325`.)