GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-99367: Do not mangle sys.path[0] in pdb if safe_path is set by gaogaotiantian · Pull Request #111762 · python/cpython

Conversation

@gaogaotiantian

@gaogaotiantian gaogaotiantian commented Nov 5, 2023 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

pdb converts sys.path[0] to the script's dir - that should not happen if using safe_path.

Co-authored-by: Christian Walther [email protected]

  • Issue: pdb mangles sys.path when run with -P or ._pth #99367

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gaogaotiantian
Do not mangle sys.path[0] if safe_path is set
3fe225d
@bedevere-app bedevere-app Bot added the awaiting review label Nov 5, 2023
@bedevere-app bedevere-app Bot mentioned this pull request Nov 5, 2023
pdb mangles sys.path when run with -P or ._pth #99367
Closed
@blurb-it
📜🤖 Added by blurb_it.
1ec6724
@gaogaotiantian
gaogaotiantian requested a review from iritkatriel November 5, 2023 20:09
@gaogaotiantian

gaogaotiantian commented Nov 5, 2023

Copy link
Copy Markdown
Member Author

cc: @cwalther

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iritkatriel
iritkatriel reviewed Nov 6, 2023
View reviewed changes
Comment thread Lib/pdb.py Outdated
# 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

@iritkatriel iritkatriel Nov 6, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gaogaotiantian gaogaotiantian Nov 6, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@iritkatriel iritkatriel Nov 6, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, rephrase it then. I think it's too terse and not clear what that last part is talking about.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gaogaotiantian gaogaotiantian Nov 6, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better now?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
iritkatriel
iritkatriel reviewed Nov 6, 2023
View reviewed changes
Comment thread Lib/pdb.py Outdated
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):

@iritkatriel iritkatriel Nov 6, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is safe_path not guaranteed to exist? the test does sys.flags.safe_path.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gaogaotiantian gaogaotiantian Nov 6, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I derived this from the original 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@cwalther cwalther Nov 6, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gaogaotiantian
Rephrase the comments and simplified the logic
9a8919e
iritkatriel
iritkatriel reviewed Nov 6, 2023
View reviewed changes
Comment thread Lib/pdb.py Outdated
# 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

@iritkatriel iritkatriel Nov 6, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
iritkatriel
iritkatriel reviewed Nov 6, 2023
View reviewed changes
Comment thread Lib/pdb.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@gaogaotiantian @iritkatriel
Update Lib/pdb.py
c5fabe6
Co-authored-by: Irit Katriel <[email protected]>
cwalther
cwalther reviewed Nov 6, 2023
View reviewed changes
Comment thread Lib/test/test_pdb.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@gaogaotiantian

gaogaotiantian commented Nov 27, 2023

Copy link
Copy Markdown
Member Author

Friendly ping @iritkatriel , is there anything I should improve here?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iritkatriel
iritkatriel approved these changes Nov 27, 2023
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Nov 27, 2023
@iritkatriel

iritkatriel commented Nov 27, 2023

Copy link
Copy Markdown
Member

This is probably worth an entry in whatnew.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gaogaotiantian
Add whatsnew entry
d73c2da
@gaogaotiantian

gaogaotiantian commented Nov 27, 2023

Copy link
Copy Markdown
Member Author

Whatsnew entry is added, let me know if you want me to rephrase it!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iritkatriel
iritkatriel reviewed Nov 27, 2023
View reviewed changes
Comment thread Doc/whatsnew/3.13.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

gaogaotiantian and others added 2 commits November 27, 2023 14:50
@gaogaotiantian @iritkatriel
Update Doc/whatsnew/3.13.rst
c810afc
Co-authored-by: Irit Katriel <[email protected]>
@gaogaotiantian
Update phrasing
d40d3db
@iritkatriel
iritkatriel enabled auto-merge (squash) November 27, 2023 23:08
@iritkatriel
iritkatriel disabled auto-merge November 27, 2023 23:08
@iritkatriel
iritkatriel enabled auto-merge (squash) November 27, 2023 23:10
@iritkatriel
iritkatriel merged commit b90a5cf into python:main Nov 27, 2023
@bedevere-app bedevere-app Bot removed the awaiting merge label Nov 27, 2023
@cwalther

cwalther commented Nov 28, 2023

Copy link
Copy Markdown
Contributor

Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gaogaotiantian
gaogaotiantian deleted the pdb-safe-path branch December 11, 2023 01:21
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
@gaogaotiantian @cwalther @aisk
pythongh-99367: Do not mangle sys.path[0] in pdb if safe_path is set (p…
6244c0b
…ython#111762)

Co-authored-by: Christian Walther <[email protected]>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@gaogaotiantian @cwalther @Glyphack
pythongh-99367: Do not mangle sys.path[0] in pdb if safe_path is set (p…
41623d1
…ython#111762)

Co-authored-by: Christian Walther <[email protected]>
@algal algal mentioned this pull request Oct 6, 2025
IPython does not respect sys.flags.safe_path (aka, PYTHONSAFEPATH or -P) ipython/ipython#15013
Closed
This was referenced Nov 6, 2025
- LuisMauricioDL/Reto-Master#1
Closed
[hmr] Consider adding -P flag for CLI usage promplate/pyth-on-line#514
Open
danish9039 added a commit to danish9039/sdk that referenced this pull request Aug 5, 2026
@danish9039
fix(trainer): honour Python safe path in the generated script prelude
20501cb
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]>
@danish9039 danish9039 mentioned this pull request Aug 28, 2026
fix(trainer): use writable generated script paths kubeflow/sdk#604
Open
8 of 9 tasks
danish9039 added a commit to danish9039/sdk that referenced this pull request Sep 6, 2026
@danish9039
fix(trainer): honour Python safe path in the generated script prelude
27bca1e
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]>
juliusvonkohout pushed a commit to danish9039/sdk that referenced this pull request Oct 2, 2026
@danish9039 @juliusvonkohout
fix(trainer): honour Python safe path in the generated script prelude
ca63edd
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]>
danish9039 added a commit to danish9039/sdk that referenced this pull request Oct 8, 2026
@danish9039
fix(trainer): honour Python safe path in the generated script prelude
2a1b631
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@iritkatriel iritkatriel iritkatriel approved these changes
+1 more reviewer
@cwalther cwalther cwalther left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@gaogaotiantian @iritkatriel @cwalther
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.