https://github.com/python/cpython/commit/b414ad10434de87c8b04dc3d819fa01e712f550f
commit: b414ad10434de87c8b04dc3d819fa01e712f550f
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: vstinner <[email protected]>
date: 2025-10-07T20:40:42+02:00
summary:

[3.14] gh-81325: Support path-like objects with streaming TarFile (GH-137188) 
(#137365)

gh-81325: Support path-like objects with streaming TarFile (GH-137188)
(cherry picked from commit 3ec3d053452af8a769c18826ea61ba66fc73c8da)

Co-authored-by: Alexander Urieles <[email protected]>
Co-authored-by: Emma Smith <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst
M Lib/tarfile.py
M Lib/test/test_tarfile.py

diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 7dff0122da7775..c7e9f7d681a8b1 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -353,7 +353,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
             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
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index b23ff34bc327c6..860413b88eb6b5 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -1788,6 +1788,16 @@ def test_file_mode(self):
         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
index 00000000000000..3d89b6eb92a0d6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst
@@ -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`.)

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to