[3.9] bpo-43757: Make pathlib use os.path.realpath() to resolve symlinks in a path (GH-25264) (GH-135035)

Also adds a new "strict" argument to realpath() to avoid changing the default behaviour of pathlib while sharing the implementation.

(cherry-picked from commit baecfbd849)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
This commit is contained in:
Łukasz Langa 2025-06-02 18:28:09 +02:00 committed by GitHub
parent 03ac445b11
commit 00af9794dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 192 additions and 124 deletions

View file

@ -622,7 +622,7 @@ def _getfinalpathname_nonstrict(path):
tail = join(name, tail) if tail else name
return tail
def realpath(path):
def realpath(path, *, strict=False):
path = normpath(path)
if isinstance(path, bytes):
prefix = b'\\\\?\\'
@ -647,6 +647,8 @@ def realpath(path):
path = _getfinalpathname(path)
initial_winerror = 0
except OSError as ex:
if strict:
raise
initial_winerror = ex.winerror
path = _getfinalpathname_nonstrict(path)
# The path returned by _getfinalpathname will always start with \\?\ -