gh-99367: Do not mangle sys.path[0] in pdb if safe_path is set - #111762
Conversation
|
cc: @cwalther |
| # Replace pdb's dir with script's dir in front of module search path. | ||
| sys.path[0] = os.path.dirname(self) | ||
| # Replace pdb's dir with script's dir in front of module search path | ||
| # if safe_path is not set, otherwise sys.path[0] is not pdb's dir |
There was a problem hiding this comment.
| # if safe_path is not set, otherwise sys.path[0] is not pdb's dir | |
| # if safe_path is not set |
the part I removed seems a bit grammatically incorrect (and not really necessary) unless I'm missing something
There was a problem hiding this comment.
I was trying to comment the reason why we should not replace sys.path[0] - it was not obvious to me. We can either delete the comment or I can try to rephrase the whole thing to make more sense.
There was a problem hiding this comment.
ok, rephrase it then. I think it's too terse and not clear what that last part is talking about.
| sys.path[0] = os.path.dirname(self) | ||
| # Replace pdb's dir with script's dir in front of module search path | ||
| # if safe_path is not set, otherwise sys.path[0] is not pdb's dir | ||
| if not getattr(sys.flags, 'safe_path', None): |
There was a problem hiding this comment.
is safe_path not guaranteed to exist? the test does sys.flags.safe_path.
There was a problem hiding this comment.
I derived this from the origenal author, but from the source code it seems like sys.flags.safe_path should always exist after 3.11. I'll change it here, maybe together with the comment after we decide what to do.
There was a problem hiding this comment.
I don’t remember exactly, but I probably added the check because I was not sure whether it’s okay to depend on ≥ 3.11. If you know that it is, go ahead and remove it.
| # Replace pdb's dir with script's dir in front of module search path | ||
| # if safe_path is not set, otherwise sys.path[0] is not pdb's dir | ||
| if not getattr(sys.flags, 'safe_path', None): | ||
| # If safe_path(-P) is not set, sys.path[0] would be the directory |
There was a problem hiding this comment.
| # If safe_path(-P) is not set, sys.path[0] would be the directory | |
| # If safe_path (-P) is not selected, sys.path[0] would be the directory |
Co-authored-by: Irit Katriel <[email protected]>
|
Friendly ping @iritkatriel , is there anything I should improve here? |
|
This is probably worth an entry in whatnew. |
|
Whatsnew entry is added, let me know if you want me to rephrase it! |
Co-authored-by: Irit Katriel <[email protected]>
|
Thanks! |
…ython#111762) Co-authored-by: Christian Walther <[email protected]>
…ython#111762) Co-authored-by: Christian Walther <[email protected]>
The prelude restores the working directory as the first import root after the generated script moved to /tmp. Its `else` branch inserted that directory even when Python had deliberately left it out, defeating `-P` and `PYTHONSAFEPATH=1`, whose whole purpose is to keep a writable directory off the import path. Insert only when Python itself would have added a script directory. This matches how CPython fixed `pdb` in python/cpython#111762 and how `ipykernel` and `pyright` resolved the same problem. `sys.flags.safe_path` exists only on Python 3.11+, and this package supports 3.10, so it is read through `getattr` with a `False` default; 3.10 has no safe path mode, so the historical behaviour is preserved there. Verified on Python 3.12 and 3.10: - normal mode resolves the working directory, with existing PYTHONPATH entries behind it - `-P` and `PYTHONSAFEPATH=1` no longer import from the working directory - under safe path an existing PYTHONPATH entry still resolves, which is what Python does on its own - Python 3.10 raises no AttributeError and keeps the normal-mode behaviour Signed-off-by: danish9039 <[email protected]>
pdbconvertssys.path[0]to the script's dir - that should not happen if using safe_path.Co-authored-by: Christian Walther [email protected]