mirror of
https://github.com/python/cpython.git
synced 2025-11-03 11:23:31 +00:00
bpo-21987: Fix TarFile.getmember getting a dir with a trailing slash (GH-30283)
This commit is contained in:
parent
22f73bd9f1
commit
cfadcc31ea
3 changed files with 22 additions and 1 deletions
|
|
@ -1789,7 +1789,7 @@ class TarFile(object):
|
||||||
than once in the archive, its last occurrence is assumed to be the
|
than once in the archive, its last occurrence is assumed to be the
|
||||||
most up-to-date version.
|
most up-to-date version.
|
||||||
"""
|
"""
|
||||||
tarinfo = self._getmember(name)
|
tarinfo = self._getmember(name.rstrip('/'))
|
||||||
if tarinfo is None:
|
if tarinfo is None:
|
||||||
raise KeyError("filename %r not found" % name)
|
raise KeyError("filename %r not found" % name)
|
||||||
return tarinfo
|
return tarinfo
|
||||||
|
|
|
||||||
|
|
@ -220,6 +220,25 @@ class UstarReadTest(ReadTest, unittest.TestCase):
|
||||||
def test_issue14160(self):
|
def test_issue14160(self):
|
||||||
self._test_fileobj_link("symtype2", "ustar/regtype")
|
self._test_fileobj_link("symtype2", "ustar/regtype")
|
||||||
|
|
||||||
|
def test_add_dir_getmember(self):
|
||||||
|
# bpo-21987
|
||||||
|
self.add_dir_and_getmember('bar')
|
||||||
|
self.add_dir_and_getmember('a'*101)
|
||||||
|
|
||||||
|
def add_dir_and_getmember(self, name):
|
||||||
|
with os_helper.temp_cwd():
|
||||||
|
with tarfile.open(tmpname, 'w') as tar:
|
||||||
|
try:
|
||||||
|
os.mkdir(name)
|
||||||
|
tar.add(name)
|
||||||
|
finally:
|
||||||
|
os.rmdir(name)
|
||||||
|
with tarfile.open(tmpname) as tar:
|
||||||
|
self.assertEqual(
|
||||||
|
tar.getmember(name),
|
||||||
|
tar.getmember(name + '/')
|
||||||
|
)
|
||||||
|
|
||||||
class GzipUstarReadTest(GzipTest, UstarReadTest):
|
class GzipUstarReadTest(GzipTest, UstarReadTest):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix an issue with :meth:`tarfile.TarFile.getmember` getting a directory name
|
||||||
|
with a trailing slash.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue