From 5b129045be3506a3b5df8d018a81dee42afe0f55 Mon Sep 17 00:00:00 2001 From: johnslavik Date: Sun, 7 Dec 2025 19:24:42 +0100 Subject: [PATCH] Add more idiomatic safe realpath helper --- Lib/pdb.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 5729624ba139..a2930b5a8542 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -190,11 +190,9 @@ class _ScriptTarget(_ExecutableTarget): print(f'Error: {target} is a directory') sys.exit(1) - # Be careful with realpath to support pseudofilesystems (GH-142315). - realpath = os.path.realpath(target) - self._target = realpath if os.path.exists(realpath) else target + self._target = self._safe_realpath(target) - # If safe_path(-P) is not set, sys.path[0] is the directory + # If PYTHONSAFEPATH (-P) is not set, sys.path[0] is the directory # of pdb, and we should replace it with the directory of the script if not sys.flags.safe_path: sys.path[0] = os.path.dirname(self._target) @@ -202,6 +200,18 @@ class _ScriptTarget(_ExecutableTarget): def __repr__(self): return self._target + @staticmethod + def _safe_realpath(path): + """ + Return the canonical path (realpath) if it is accessible from the userspace. + Otherwise (for example, if the path is a symlink to an anonymous pipe), + return the original path. + + See GH-142315. + """ + realpath = os.path.realpath(path) + return realpath if os.path.exists(realpath) else path + @property def filename(self): return self._target -- 2.47.3