Mailing List Archive

GH-117546: Fix symlink resolution in `os.path.realpath('loop/../link')` (#117568)
https://github.com/python/cpython/commit/630df37116b1c5b381984c547ef9d23792ceb464
commit: 630df37116b1c5b381984c547ef9d23792ceb464
branch: main
author: Barney Gale <barney.gale@gmail.com>
committer: barneygale <barney.gale@gmail.com>
date: 2024-04-10T18:17:18+01:00
summary:

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.

files:
A Misc/NEWS.d/next/Library/2024-04-05-13-38-53.gh-issue-117546.lWjhHE.rst
M Doc/library/os.path.rst
M Lib/posixpath.py
M Lib/test/test_posixpath.py

diff --git a/Doc/library/os.path.rst b/Doc/library/os.path.rst
index fcf4b6d68e018c..ebeb3bb50b8b1f 100644
--- a/Doc/library/os.path.rst
+++ b/Doc/library/os.path.rst
@@ -409,9 +409,8 @@ the :mod:`glob` module.)
style names such as ``C:\\PROGRA~1`` to ``C:\\Program Files``.

If a path doesn't exist or a symlink loop is encountered, and *strict* is
- ``True``, :exc:`OSError` is raised. If *strict* is ``False``, the path is
- resolved as far as possible and any remainder is appended without checking
- whether it exists.
+ ``True``, :exc:`OSError` is raised. If *strict* is ``False`` these errors
+ are ignored, and so the result might be missing or otherwise inaccessible.

.. note::
This function emulates the operating system's procedure for making a path
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index 79e65587e66282..8fd49cdc358908 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -431,11 +431,6 @@ def realpath(filename, *, strict=False):
# 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 @@ def realpath(filename, *, strict=False):
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 @@ def realpath(filename, *, strict=False):
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):
diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py
index ff78410738022d..248fe2cc5d5ca8 100644
--- a/Lib/test/test_posixpath.py
+++ b/Lib/test/test_posixpath.py
@@ -484,7 +484,7 @@ def test_realpath_symlink_loops(self):
self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x")
os.symlink(ABSTFN+"x", ABSTFN+"y")
self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"),
- ABSTFN + "y")
+ ABSTFN + "x")
self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"),
ABSTFN + "1")

diff --git a/Misc/NEWS.d/next/Library/2024-04-05-13-38-53.gh-issue-117546.lWjhHE.rst b/Misc/NEWS.d/next/Library/2024-04-05-13-38-53.gh-issue-117546.lWjhHE.rst
new file mode 100644
index 00000000000000..9762991e47a6a4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-04-05-13-38-53.gh-issue-117546.lWjhHE.rst
@@ -0,0 +1,2 @@
+Fix issue where :func:`os.path.realpath` stopped resolving symlinks after
+encountering a symlink loop on POSIX.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-leave@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: list-python-checkins@lists.gossamer-threads.com