From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Tue, 5 Aug 2025 21:17:12 +0000 (+0200) Subject: [3.13] gh-81325: Support path-like objects with streaming TarFile (GH-137188) (#137366) X-Git-Tag: v3.13.6~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3a61ddf2d3725932777444e844ad281088589ee0;p=thirdparty%2FPython%2Fcpython.git [3.13] gh-81325: Support path-like objects with streaming TarFile (GH-137188) (#137366) Co-authored-by: Alexander Urieles Co-authored-by: Emma Smith --- diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 9ff9df696de6..4867735dd95e 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -354,7 +354,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 diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 7024be46de59..c31842cf5237 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -1717,6 +1717,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 index 000000000000..3d89b6eb92a0 --- /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 ` when working on a tar archive. +(Contributed by Alexander Enrique Urieles Nieto in :gh:`81325`.)