[3.13] GH-118447: Fix handling of unreadable symlinks in os.path.realpath() (GH-118489) (#119163)

(cherry picked from commit caf6064a1b)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-05-19 01:12:19 +02:00 committed by GitHub
parent 91296146d5
commit 7407267ce4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 13 deletions

View file

@ -660,6 +660,23 @@ class PosixPathTest(unittest.TestCase):
safe_rmdir(ABSTFN + "/k")
safe_rmdir(ABSTFN)
@os_helper.skip_unless_symlink
@skip_if_ABSTFN_contains_backslash
@unittest.skipIf(os.chmod not in os.supports_follow_symlinks, "Can't set symlink permissions")
def test_realpath_unreadable_symlink(self):
try:
os.symlink(ABSTFN+"1", ABSTFN)
os.chmod(ABSTFN, 0o000, follow_symlinks=False)
self.assertEqual(realpath(ABSTFN), ABSTFN)
self.assertEqual(realpath(ABSTFN + '/foo'), ABSTFN + '/foo')
self.assertEqual(realpath(ABSTFN + '/../foo'), dirname(ABSTFN) + '/foo')
self.assertEqual(realpath(ABSTFN + '/foo/..'), ABSTFN)
with self.assertRaises(PermissionError):
realpath(ABSTFN, strict=True)
finally:
os.chmod(ABSTFN, 0o755, follow_symlinks=False)
os.unlink(ABSTFN)
def test_relpath(self):
(real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
try: