Skip to content

Commit 3ec3d05

Browse files
gh-81325: Support path-like objects with streaming TarFile (#137188)
Co-authored-by: Emma Smith <emma@emmatyping.dev>
1 parent b266fbc commit 3ec3d05

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Lib/tarfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
353353
fileobj = _StreamProxy(fileobj)
354354
comptype = fileobj.getcomptype()
355355

356-
self.name = name or ""
356+
self.name = os.fspath(name) if name is not None else ""
357357
self.mode = mode
358358
self.comptype = comptype
359359
self.fileobj = fileobj

Lib/test/test_tarfile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,16 @@ def test_file_mode(self):
17371737
finally:
17381738
os.umask(original_umask)
17391739

1740+
def test_pathlike_name(self):
1741+
expected_name = os.path.abspath(tmpname)
1742+
tarpath = os_helper.FakePath(tmpname)
1743+
1744+
for func in (tarfile.open, tarfile.TarFile.open):
1745+
with self.subTest():
1746+
with func(tarpath, self.mode) as tar:
1747+
self.assertEqual(tar.name, expected_name)
1748+
os_helper.unlink(tmpname)
1749+
17401750

17411751
class GzipStreamWriteTest(GzipTest, StreamWriteTest):
17421752
def test_source_directory_not_leaked(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:class:`tarfile.TarFile` now accepts a :term:`path-like <path-like object>` when working on a tar archive.
2+
(Contributed by Alexander Enrique Urieles Nieto in :gh:`81325`.)

0 commit comments

Comments
 (0)