GH-109187: Improve symlink loop handling in pathlib.Path.resolve() (GH-109192)

Treat symlink loops like other errors: in strict mode, raise `OSError`, and
in non-strict mode, do not raise any exception.
This commit is contained in:
Barney Gale 2023-09-26 17:57:17 +01:00 committed by GitHub
parent 859618c8cd
commit ecd813f054
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 30 deletions

View file

@ -1230,26 +1230,7 @@ class Path(PurePath):
normalizing it.
"""
def check_eloop(e):
winerror = getattr(e, 'winerror', 0)
if e.errno == ELOOP or winerror == _WINERROR_CANT_RESOLVE_FILENAME:
raise RuntimeError("Symlink loop from %r" % e.filename)
try:
s = os.path.realpath(self, strict=strict)
except OSError as e:
check_eloop(e)
raise
p = self.with_segments(s)
# In non-strict mode, realpath() doesn't raise on symlink loops.
# Ensure we get an exception by calling stat()
if not strict:
try:
p.stat()
except OSError as e:
check_eloop(e)
return p
return self.with_segments(os.path.realpath(self, strict=strict))
def owner(self):
"""