[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 3ec3d05345)

Co-authored-by: Alexander Urieles <aeurielesn@users.noreply.github.com>
Co-authored-by: Emma Smith <emma@emmatyping.dev>
This commit is contained in:
Miss Islington (bot) 2025-10-07 20:40:42 +02:00 committed by GitHub
parent a868c6a70d
commit b414ad1043
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 1 deletions

View file

@ -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

View file

@ -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):

View file

@ -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`.)