Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.)
Loading