From 98fccf531aeb73993a4556607301058a4bfd48f0 Mon Sep 17 00:00:00 2001 From: aeurielesn Date: Mon, 28 Jul 2025 23:06:02 +0200 Subject: [PATCH 1/6] gh-81325: tarfile should accept a path-like object when writing a compressed gzip --- Lib/tarfile.py | 2 +- Lib/test/test_tarfile.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 45f58eb8ac93cfa..4bcca0966515699 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 28914df6b010d02..654feaf1f48c7d1 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -1737,6 +1737,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): From 54d7e1a56712a2d8abb51df999c1f0dbd8cd5e7c Mon Sep 17 00:00:00 2001 From: aeurielesn Date: Mon, 28 Jul 2025 23:13:22 +0200 Subject: [PATCH 2/6] Blurb --- .../next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst 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 000000000000000..8d7e78a758c5691 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst @@ -0,0 +1,2 @@ +:mod:`tarfile` now accepts a path-like object when writing a compressed gzip. +(Contributed by Alexander Enrique Urieles Nieto in :gh:`81325`.) From ffbda417043209c447be70531fd8be95ddd87921 Mon Sep 17 00:00:00 2001 From: aeurielesn Date: Mon, 28 Jul 2025 23:15:56 +0200 Subject: [PATCH 3/6] Tests --- Lib/test/test_tarfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 654feaf1f48c7d1..624468a222a200c 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -1745,7 +1745,7 @@ def test_pathlike_name(self): with self.subTest(): with func(tarpath, self.mode) as tar: self.assertEqual(tar.name, expected_name) - os_helper.unlink(tmpname) + os_helper.unlink(tmpname) class GzipStreamWriteTest(GzipTest, StreamWriteTest): From a08cbdd53757067eaba0d3cab2edfbf26f4863bf Mon Sep 17 00:00:00 2001 From: aeurielesn Date: Mon, 28 Jul 2025 23:17:52 +0200 Subject: [PATCH 4/6] Lint --- .../next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 8d7e78a758c5691..b8095886fe5367a 100644 --- 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 @@ -1,2 +1,2 @@ -:mod:`tarfile` now accepts a path-like object when writing a compressed gzip. +:mod:`tarfile` now accepts a path-like object when writing a compressed gzip. (Contributed by Alexander Enrique Urieles Nieto in :gh:`81325`.) From d17ff1dc52f967f92a8f4425a4717fefdddbce7c Mon Sep 17 00:00:00 2001 From: Alexander Urieles Date: Fri, 1 Aug 2025 21:41:00 +0200 Subject: [PATCH 5/6] Update Misc/NEWS.d/next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst Co-authored-by: Emma Smith --- .../next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index b8095886fe5367a..49666d1d7fff94f 100644 --- 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 @@ -1,2 +1,2 @@ -:mod:`tarfile` now accepts a path-like object when writing a compressed gzip. +:class:`tarfile.Tarfile` now accepts a :term:`path-like ` when working on a tar archive. (Contributed by Alexander Enrique Urieles Nieto in :gh:`81325`.) From 06104ecb81a35f8db0886683b80ada4a91758e75 Mon Sep 17 00:00:00 2001 From: aeurielesn Date: Fri, 1 Aug 2025 22:14:35 +0200 Subject: [PATCH 6/6] NEWS --- .../next/Library/2025-07-28-23-11-29.gh-issue-81325.jMJFBe.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 49666d1d7fff94f..3d89b6eb92a0d66 100644 --- 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 @@ -1,2 +1,2 @@ -:class:`tarfile.Tarfile` now accepts a :term:`path-like ` when working on a tar archive. +:class:`tarfile.TarFile` now accepts a :term:`path-like ` when working on a tar archive. (Contributed by Alexander Enrique Urieles Nieto in :gh:`81325`.)