Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcaf6064

Browse files
GH-118447: Fix handling of unreadable symlinks inos.path.realpath() (#118489)
Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
1 parent30b4e9f commitcaf6064

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

‎Lib/posixpath.py‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -471,26 +471,26 @@ def realpath(filename, *, strict=False):
471471
ifnotstat.S_ISLNK(st.st_mode):
472472
path=newpath
473473
continue
474+
ifnewpathinseen:
475+
# Already seen this path
476+
path=seen[newpath]
477+
ifpathisnotNone:
478+
# use cached value
479+
continue
480+
# The symlink is not resolved, so we must have a symlink loop.
481+
ifstrict:
482+
# Raise OSError(errno.ELOOP)
483+
os.stat(newpath)
484+
path=newpath
485+
continue
486+
target=os.readlink(newpath)
474487
exceptOSError:
475488
ifstrict:
476489
raise
477490
path=newpath
478491
continue
479492
# Resolve the symbolic link
480-
ifnewpathinseen:
481-
# Already seen this path
482-
path=seen[newpath]
483-
ifpathisnotNone:
484-
# use cached value
485-
continue
486-
# The symlink is not resolved, so we must have a symlink loop.
487-
ifstrict:
488-
# Raise OSError(errno.ELOOP)
489-
os.stat(newpath)
490-
path=newpath
491-
continue
492493
seen[newpath]=None# not resolved symlink
493-
target=os.readlink(newpath)
494494
iftarget.startswith(sep):
495495
# Symlink target is absolute; reset resolved path.
496496
path=sep

‎Lib/test/test_posixpath.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,23 @@ def test_realpath_resolve_first(self):
660660
safe_rmdir(ABSTFN+"/k")
661661
safe_rmdir(ABSTFN)
662662

663+
@os_helper.skip_unless_symlink
664+
@skip_if_ABSTFN_contains_backslash
665+
@unittest.skipIf(os.chmodnotinos.supports_follow_symlinks,"Can't set symlink permissions")
666+
deftest_realpath_unreadable_symlink(self):
667+
try:
668+
os.symlink(ABSTFN+"1",ABSTFN)
669+
os.chmod(ABSTFN,0o000,follow_symlinks=False)
670+
self.assertEqual(realpath(ABSTFN),ABSTFN)
671+
self.assertEqual(realpath(ABSTFN+'/foo'),ABSTFN+'/foo')
672+
self.assertEqual(realpath(ABSTFN+'/../foo'),dirname(ABSTFN)+'/foo')
673+
self.assertEqual(realpath(ABSTFN+'/foo/..'),ABSTFN)
674+
withself.assertRaises(PermissionError):
675+
realpath(ABSTFN,strict=True)
676+
finally:
677+
os.chmod(ABSTFN,0o755,follow_symlinks=False)
678+
os.unlink(ABSTFN)
679+
663680
deftest_relpath(self):
664681
(real_getcwd,os.getcwd)= (os.getcwd,lambda:r"/home/user/bar")
665682
try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`os.path.realpath` now suppresses any:exc:`OSError` from
2+
:func:`os.readlink` when *strict* mode is disabled (the default).

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp