From 855aac3d289dd096142ed9bd23d00c22ce6e1859 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 8 Dec 2025 02:42:39 -0500 Subject: [PATCH] Extract method for replacing sys_path, and isolate realpath usage there. --- Lib/pdb.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 007541ed0a46..1c2b70f6bdf5 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -183,13 +183,9 @@ class _ExecutableTarget: class _ScriptTarget(_ExecutableTarget): def __init__(self, target): + self._target = target self._check(target) - self._target = os.path.realpath(target) - - # 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) + self._replace_sys_path(target) @staticmethod def _check(target): @@ -203,6 +199,13 @@ class _ScriptTarget(_ExecutableTarget): print(f'Error: {target} is a directory') sys.exit(1) + @staticmethod + def _replace_sys_path(target): + # If PYTHONSAFEPATH (-P) is not set, sys.path[0] is the directory + # of pdb, so replace it with the directory of the script + if not sys.flags.safe_path: + sys.path[0] = os.path.dirname(os.path.realpath(target)) + def __repr__(self): return self._target -- 2.47.3