@@ -184,24 +184,34 @@ class _ExecutableTarget:
184184class _ScriptTarget (_ExecutableTarget ):
185185 def __init__ (self , target ):
186186 if not os .path .exists (target ):
187- print (f' Error: { target } does not exist' )
187+ print (f" Error: { target } does not exist" )
188188 sys .exit (1 )
189189 if os .path .isdir (target ):
190- print (f' Error: { target } is a directory' )
190+ 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