GH-117546: Fix symlink resolution in os.path.realpath('loop/../link') (#117568)

Continue resolving symlink targets after encountering a symlink loop, which
matches coreutils `realpath` behaviour.
This commit is contained in:
Barney Gale 2024-04-10 18:17:18 +01:00 committed by GitHub
parent 6bc0b33a91
commit 630df37116
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 7 additions and 17 deletions

View file

@ -431,11 +431,6 @@ symbolic links encountered in the path."""
# the same links.
seen = {}
# Whether we're calling lstat() and readlink() to resolve symlinks. If we
# encounter an OSError for a symlink loop in non-strict mode, this is
# switched off.
querying = True
while rest:
name = rest.pop()
if name is None:
@ -453,9 +448,6 @@ symbolic links encountered in the path."""
newpath = path + name
else:
newpath = path + sep + name
if not querying:
path = newpath
continue
try:
st = os.lstat(newpath)
if not stat.S_ISLNK(st.st_mode):
@ -477,11 +469,8 @@ symbolic links encountered in the path."""
if strict:
# Raise OSError(errno.ELOOP)
os.stat(newpath)
else:
# Return already resolved part + rest of the path unchanged.
path = newpath
querying = False
continue
path = newpath
continue
seen[newpath] = None # not resolved symlink
target = os.readlink(newpath)
if target.startswith(sep):