Skip to content

Commit 92dc2b3

Browse files
committed
pythonGH-109187: Improve symlink loop handling in pathlib.Path.resolve()
Treat symlink loops like other errors: in strict mode, raise `OSError`, and in non-strict mode, do not raise any exception.
1 parent 75cd865 commit 92dc2b3

4 files changed

Lines changed: 19 additions & 27 deletions

File tree

‎Doc/library/pathlib.rst‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,15 +1381,19 @@ call fails (for example because the path doesn't exist).
13811381
>>> p.resolve()
13821382
PosixPath('/home/antoine/pathlib/setup.py')
13831383

1384-
If the path doesn't exist and *strict* is ``True``, :exc:`FileNotFoundError`
1385-
is raised. If *strict* is ``False``, the path is resolved as far as possible
1386-
and any remainder is appended without checking whether it exists. If an
1387-
infinite loop is encountered along the resolution path, :exc:`RuntimeError`
1388-
is raised.
1384+
If a path doesn't exist or a symlink loop is encountered, and *strict* is
1385+
``True``, :exc:`OSError` is raised. If *strict* is ``False``, the path is
1386+
resolved as far as possible and any remainder is appended without checking
1387+
whether it exists.
13891388

13901389
.. versionchanged:: 3.6
13911390
The *strict* parameter was added (pre-3.6 behavior is strict).
13921391

1392+
.. versionchanged:: 3.13
1393+
Symlink loops are treated like other errors: :exc:`OSError` is raised in
1394+
strict mode, and no exception is raised in non-strict mode. In previous
1395+
versions, :exc:`RuntimeError` is raised no matter the value of *strict*.
1396+
13931397
.. method:: Path.rglob(pattern, *, case_sensitive=None, follow_symlinks=None)
13941398

13951399
Glob the given relative *pattern* recursively. This is like calling

‎Lib/pathlib.py‎

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,26 +1230,7 @@ def resolve(self, strict=False):
12301230
normalizing it.
12311231
"""
12321232

1233-
def check_eloop(e):
1234-
winerror = getattr(e, 'winerror', 0)
1235-
if e.errno == ELOOP or winerror == _WINERROR_CANT_RESOLVE_FILENAME:
1236-
raise RuntimeError("Symlink loop from %r" % e.filename)
1237-
1238-
try:
1239-
s = os.path.realpath(self, strict=strict)
1240-
except OSError as e:
1241-
check_eloop(e)
1242-
raise
1243-
p = self.with_segments(s)
1244-
1245-
# In non-strict mode, realpath() doesn't raise on symlink loops.
1246-
# Ensure we get an exception by calling stat()
1247-
if not strict:
1248-
try:
1249-
p.stat()
1250-
except OSError as e:
1251-
check_eloop(e)
1252-
return p
1233+
return self.with_segments(os.path.realpath(self, strict=strict))
12531234

12541235
def owner(self):
12551236
"""

‎Lib/test/test_pathlib.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3180,8 +3180,12 @@ def test_absolute(self):
31803180

31813181
def _check_symlink_loop(self, *args, strict=True):
31823182
path = self.cls(*args)
3183-
with self.assertRaises(RuntimeError):
3184-
print(path.resolve(strict))
3183+
if strict:
3184+
with self.assertRaises(OSError) as cm:
3185+
path.resolve(strict=True)
3186+
self.assertEqual(cm.exception.errno, errno.ELOOP)
3187+
else:
3188+
path.resolve(strict=False)
31853189

31863190
@unittest.skipIf(
31873191
is_emscripten or is_wasi,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:meth:`pathlib.Path.resolve` now treats symlink loops like other errors: in
2+
strict mode, :exc:`OSError` is raised, and in non-strict mode, no exception
3+
is raised.

0 commit comments

Comments
 (0)