Skip to content

Commit 5b12904

Browse files
committed
Add more idiomatic safe realpath helper
1 parent 2d8c231 commit 5b12904

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Lib/pdb.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,28 @@ def __init__(self, target):
190190
print(f'Error: {target} is a directory')
191191
sys.exit(1)
192192

193-
# Be careful with realpath to support pseudofilesystems (GH-142315).
194-
realpath = os.path.realpath(target)
195-
self._target = realpath if os.path.exists(realpath) else target
193+
self._target = self._safe_realpath(target)
196194

197-
# If safe_path(-P) is not set, sys.path[0] is the directory
195+
# If PYTHONSAFEPATH (-P) is not set, sys.path[0] is the directory
198196
# of pdb, and we should replace it with the directory of the script
199197
if not sys.flags.safe_path:
200198
sys.path[0] = os.path.dirname(self._target)
201199

202200
def __repr__(self):
203201
return self._target
204202

203+
@staticmethod
204+
def _safe_realpath(path):
205+
"""
206+
Return the canonical path (realpath) if it is accessible from the userspace.
207+
Otherwise (for example, if the path is a symlink to an anonymous pipe),
208+
return the original path.
209+
210+
See GH-142315.
211+
"""
212+
realpath = os.path.realpath(path)
213+
return realpath if os.path.exists(realpath) else path
214+
205215
@property
206216
def filename(self):
207217
return self._target

0 commit comments

Comments
 (0)