Skip to content

Commit 3a61ddf

Browse files
miss-islingtonaeurielesnemmatyping
authored
[3.13] gh-81325: Support path-like objects with streaming TarFile (GH-137188) (#137366)
Co-authored-by: Alexander Urieles <aeurielesn@users.noreply.github.com> Co-authored-by: Emma Smith <emma@emmatyping.dev>
1 parent 29ebe04 commit 3a61ddf

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
@@ -354,7 +354,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
354354
fileobj = _StreamProxy(fileobj)
355355
comptype = fileobj.getcomptype()
356356

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

Lib/test/test_tarfile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,16 @@ def test_file_mode(self):
17171717
finally:
17181718
os.umask(original_umask)
17191719

1720+
def test_pathlike_name(self):
1721+
expected_name = os.path.abspath(tmpname)
1722+
tarpath = os_helper.FakePath(tmpname)
1723+
1724+
for func in (tarfile.open, tarfile.TarFile.open):
1725+
with self.subTest():
1726+
with func(tarpath, self.mode) as tar:
1727+
self.assertEqual(tar.name, expected_name)
1728+
os_helper.unlink(tmpname)
1729+
17201730

17211731
class GzipStreamWriteTest(GzipTest, StreamWriteTest):
17221732
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)