[3.9] bpo-21987: Fix TarFile.getmember getting a dir with a trailing … · python/cpython@94d6434 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1785,7 +1785,7 @@ def getmember(self, name):
1785 1785 than once in the archive, its last occurrence is assumed to be the
1786 1786 most up-to-date version.
1787 1787 """
1788 -tarinfo = self._getmember(name)
1788 +tarinfo = self._getmember(name.rstrip('/'))
1789 1789 if tarinfo is None:
1790 1790 raise KeyError("filename %r not found" % name)
1791 1791 return tarinfo
Original file line number Diff line number Diff line change
@@ -219,6 +219,25 @@ def test_fileobj_symlink2(self):
219 219 def test_issue14160(self):
220 220 self._test_fileobj_link("symtype2", "ustar/regtype")
221 221
222 +def test_add_dir_getmember(self):
223 +# bpo-21987
224 +self.add_dir_and_getmember('bar')
225 +self.add_dir_and_getmember('a'*101)
226 +
227 +def add_dir_and_getmember(self, name):
228 +with support.temp_cwd():
229 +with tarfile.open(tmpname, 'w') as tar:
230 +try:
231 +os.mkdir(name)
232 +tar.add(name)
233 +finally:
234 +os.rmdir(name)
235 +with tarfile.open(tmpname) as tar:
236 +self.assertEqual(
237 +tar.getmember(name),
238 +tar.getmember(name + '/')
239 + )
240 +
222 241 class GzipUstarReadTest(GzipTest, UstarReadTest):
223 242 pass
224 243
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 +Fix an issue with :meth:`tarfile.TarFile.getmember` getting a directory name
2 +with a trailing slash.