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
4 changes: 3 additions & 1 deletion Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2174,7 +2174,9 @@ def getmember(self, name):
than once in the archive, its last occurrence is assumed to be the
most up-to-date version.
"""
tarinfo = self._getmember(name.rstrip('/'))
tarinfo = self._getmember(name)
if tarinfo is None and name.endswith('/'):
tarinfo = self._getmember(name.rstrip('/'))
if tarinfo is None:
raise KeyError("filename %r not found" % name)
return tarinfo
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ def test_add_dir_getmember(self):
self.add_dir_and_getmember('bar')
self.add_dir_and_getmember('a'*101)

def test_extract_name_with_trailing_slash(self):
with tarfile.open(tmpname, 'w') as tar:
tar.addfile(tarfile.TarInfo('./mydir/'))
with tarfile.open(tmpname) as tar:
names = tar.getnames()
self.assertEqual(names, ['./mydir/'])
tar.extract(names[0], TEMPDIR, filter='fully_trusted')

@unittest.skipUnless(hasattr(os, "getuid") and hasattr(os, "getgid"),
"Missing getuid or getgid implementation")
def add_dir_and_getmember(self, name):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :meth:`tarfile.TarFile.extract` to accept archive member names with a
trailing forward slash, including those returned by
:meth:`tarfile.TarFile.getnames`. Contributed by Xiao Yuan.
Loading