Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-99367: Do not mangle sys.path[0] in pdb if safe_path is set #111762

Merged
merged 7 commits into from
Nov 27, 2023

Conversation

gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Nov 5, 2023

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

Co-authored-by: Christian Walther cwalther@users.noreply.github.com

@gaogaotiantian
Copy link
Member Author

cc: @cwalther

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
Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is it better now?

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):
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

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
Copy link
Member

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

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
@gaogaotiantian
Copy link
Member Author

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

@iritkatriel
Copy link
Member

This is probably worth an entry in whatnew.

@gaogaotiantian
Copy link
Member Author

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

gaogaotiantian and others added 2 commits November 27, 2023 14:50
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
@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
@cwalther
Copy link
Contributor

Thanks!

@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
…ython#111762)

Co-authored-by: Christian Walther <cwalther@users.noreply.github.com>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…ython#111762)

Co-authored-by: Christian Walther <cwalther@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants