Skip to content

Commit cfadcc3

Browse files
authored
bpo-21987: Fix TarFile.getmember getting a dir with a trailing slash (GH-30283)
1 parent 22f73bd commit cfadcc3

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/tarfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1789,7 +1789,7 @@ def getmember(self, name):
17891789
than once in the archive, its last occurrence is assumed to be the
17901790
most up-to-date version.
17911791
"""
1792-
tarinfo = self._getmember(name)
1792+
tarinfo = self._getmember(name.rstrip('/'))
17931793
if tarinfo is None:
17941794
raise KeyError("filename %r not found" % name)
17951795
return tarinfo

Lib/test/test_tarfile.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,25 @@ def test_fileobj_symlink2(self):
220220
def test_issue14160(self):
221221
self._test_fileobj_link("symtype2", "ustar/regtype")
222222

223+
def test_add_dir_getmember(self):
224+
# bpo-21987
225+
self.add_dir_and_getmember('bar')
226+
self.add_dir_and_getmember('a'*101)
227+
228+
def add_dir_and_getmember(self, name):
229+
with os_helper.temp_cwd():
230+
with tarfile.open(tmpname, 'w') as tar:
231+
try:
232+
os.mkdir(name)
233+
tar.add(name)
234+
finally:
235+
os.rmdir(name)
236+
with tarfile.open(tmpname) as tar:
237+
self.assertEqual(
238+
tar.getmember(name),
239+
tar.getmember(name + '/')
240+
)
241+
223242
class GzipUstarReadTest(GzipTest, UstarReadTest):
224243
pass
225244

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an issue with :meth:`tarfile.TarFile.getmember` getting a directory name
2+
with a trailing slash.

0 commit comments

Comments
 (0)