From 92dc2b3176c56073a0ed367652af31029ca17f71 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 9 Sep 2023 17:13:27 +0100 Subject: [PATCH] GH-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. --- Doc/library/pathlib.rst | 14 ++++++++----- Lib/pathlib.py | 21 +------------------ Lib/test/test_pathlib.py | 8 +++++-- ...-09-09-17-09-54.gh-issue-109187.dIayNW.rst | 3 +++ 4 files changed, 19 insertions(+), 27 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-09-09-17-09-54.gh-issue-109187.dIayNW.rst diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 22360b22fd924b..48d6176d26bb8f 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -1381,15 +1381,19 @@ call fails (for example because the path doesn't exist). >>> p.resolve() PosixPath('/home/antoine/pathlib/setup.py') - If the path doesn't exist and *strict* is ``True``, :exc:`FileNotFoundError` - is raised. If *strict* is ``False``, the path is resolved as far as possible - and any remainder is appended without checking whether it exists. If an - infinite loop is encountered along the resolution path, :exc:`RuntimeError` - is raised. + 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. .. versionchanged:: 3.6 The *strict* parameter was added (pre-3.6 behavior is strict). + .. versionchanged:: 3.13 + Symlink loops are treated like other errors: :exc:`OSError` is raised in + strict mode, and no exception is raised in non-strict mode. In previous + versions, :exc:`RuntimeError` is raised no matter the value of *strict*. + .. method:: Path.rglob(pattern, *, case_sensitive=None, follow_symlinks=None) Glob the given relative *pattern* recursively. This is like calling diff --git a/Lib/pathlib.py b/Lib/pathlib.py index f4ec315da6b4fa..bd5f61b0b7c878 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -1230,26 +1230,7 @@ def resolve(self, strict=False): 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): """ diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 09df3fe471fc3e..b07e7ba04021f4 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -3180,8 +3180,12 @@ def test_absolute(self): def _check_symlink_loop(self, *args, strict=True): path = self.cls(*args) - with self.assertRaises(RuntimeError): - print(path.resolve(strict)) + if strict: + with self.assertRaises(OSError) as cm: + path.resolve(strict=True) + self.assertEqual(cm.exception.errno, errno.ELOOP) + else: + path.resolve(strict=False) @unittest.skipIf( is_emscripten or is_wasi, diff --git a/Misc/NEWS.d/next/Library/2023-09-09-17-09-54.gh-issue-109187.dIayNW.rst b/Misc/NEWS.d/next/Library/2023-09-09-17-09-54.gh-issue-109187.dIayNW.rst new file mode 100644 index 00000000000000..31b3ef77807cde --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-09-09-17-09-54.gh-issue-109187.dIayNW.rst @@ -0,0 +1,3 @@ +:meth:`pathlib.Path.resolve` now treats symlink loops like other errors: in +strict mode, :exc:`OSError` is raised, and in non-strict mode, no exception +is raised.