summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-01-21 00:05:57 -0800
committerGitHub <noreply@github.com>2022-01-21 00:05:57 -0800
commit1d11fdd3eeff77ba600278433b7ab0ce4d2a7f3b (patch)
tree1b3c63ed7b07f6c2f9fef644e2269cd1bd00e617
parente5edc8d737a45d9d8b9b93b8be52f85d79d0f417 (diff)
downloadcpython-git-1d11fdd3eeff77ba600278433b7ab0ce4d2a7f3b.tar.gz
bpo-21987: Fix TarFile.getmember getting a dir with a trailing slash (GH-30283)
(cherry picked from commit cfadcc31ea84617b1c73022ce54d4ae831333e8d) Co-authored-by: andrei kulakov <andrei.avk@gmail.com>
-rwxr-xr-xLib/tarfile.py2
-rw-r--r--Lib/test/test_tarfile.py19
-rw-r--r--Misc/NEWS.d/next/Library/2021-12-28-11-55-10.bpo-21987.avBK-p.rst2
3 files changed, 22 insertions, 1 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index c1ee1222e0..e187da2b19 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1789,7 +1789,7 @@ class TarFile(object):
than once in the archive, its last occurrence is assumed to be the
most up-to-date version.
"""
- tarinfo = self._getmember(name)
+ tarinfo = self._getmember(name.rstrip('/'))
if tarinfo is None:
raise KeyError("filename %r not found" % name)
return tarinfo
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index e4b5c52bf1..1357df57eb 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -220,6 +220,25 @@ class UstarReadTest(ReadTest, unittest.TestCase):
def test_issue14160(self):
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):
pass
diff --git a/Misc/NEWS.d/next/Library/2021-12-28-11-55-10.bpo-21987.avBK-p.rst b/Misc/NEWS.d/next/Library/2021-12-28-11-55-10.bpo-21987.avBK-p.rst
new file mode 100644
index 0000000000..305dd16d53
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-12-28-11-55-10.bpo-21987.avBK-p.rst
@@ -0,0 +1,2 @@
+Fix an issue with :meth:`tarfile.TarFile.getmember` getting a directory name
+with a trailing slash.