bpo-21987: Fix TarFile.getmember getting a dir with a trailing slash … · python/cpython@1d11fdd (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1789,7 +1789,7 @@ def getmember(self, name):
1789 1789 than once in the archive, its last occurrence is assumed to be the
1790 1790 most up-to-date version.
1791 1791 """
1792 -tarinfo = self._getmember(name)
1792 +tarinfo = self._getmember(name.rstrip('/'))
1793 1793 if tarinfo is None:
1794 1794 raise KeyError("filename %r not found" % name)
1795 1795 return tarinfo
Original file line number Diff line number Diff line change
@@ -220,6 +220,25 @@ def test_fileobj_symlink2(self):
220 220 def test_issue14160(self):
221 221 self._test_fileobj_link("symtype2", "ustar/regtype")
222 222
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 +
223 242 class GzipUstarReadTest(GzipTest, UstarReadTest):
224 243 pass
225 244
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.