Skip to content

gh-125115: Pass unknown pdb command line args to script instead of fail #125424

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

Merged
merged 6 commits into from
Oct 15, 2024

Conversation

gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Oct 14, 2024

Moving to argparse created a bug where an argument starting with - to the script being debugged will be parsed, which frustrated argparse. We should just parse known args and pass the rest to the script.

@gaogaotiantian gaogaotiantian added the needs backport to 3.13 bugs and security fixes label Oct 14, 2024
Lib/pdb.py Outdated
invalid_args = []
for arg in args:
if not arg.startswith('-'):
break
Copy link
Member

Choose a reason for hiding this comment

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

What's the logic here? Why are we breaking?

Copy link
Member Author

Choose a reason for hiding this comment

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

A pdb CLI usage could be python -m pdb script.py --bar or python -m pdb --bar script.py.

The unknown args would be ['script.py', '--bar'] and [--bar', 'script.py'] respectively. Notice that in the first case, the user is trying to pass --bar to script.py, but in the latter, they try to pass --bar to pdb. This logic is to differentiate this. As long as we got something that does not start with -, we consider that as the script.

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'm missing something here, for the module case.

Copy link
Member

Choose a reason for hiding this comment

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

I'm missing something here, for the module case.

And a test for this case..

Copy link
Member Author

Choose a reason for hiding this comment

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

Turns out dealing with modules is not super trivial. I added some comments in the parsing code and hopefully that's helpful. We can "change the rule" and require users to use -- to resolve all ambiguities, but I think it would be a better experience if we can be a bit smarter.

An existing test already covers the case where --spam -m module should fail, I added a new test for -m calendar --type text which works after the change.

Lib/pdb.py Outdated
Comment on lines 2462 to 2467
for arg in args:
if not arg.startswith('-'):
break
invalid_args.append(arg)

if invalid_args:
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
for arg in args:
if not arg.startswith('-'):
break
invalid_args.append(arg)
if invalid_args:
if any(itertools.takewhile(lambda a: a.startswith('-'), args)):

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a better expression. Just one question - is it okay to import itertools in pdb? We do already use plenty of internal libraries, but there were concerns about using pdb to debug stdlib when they are not working.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, we need to full list of invalid arguments to report to keep the old behavior I think. So we need to turn this takewhiel to a list and check that.

Copy link
Member

Choose a reason for hiding this comment

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

pdb imports traceback which imports itertools.

Copy link
Member

Choose a reason for hiding this comment

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

I think if the stdlib is broken you probably need to debug it on a previous version of python.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay I used takewhile for invalid arguments now, but we do need the full list so I converted it to a list as well.

@gaogaotiantian gaogaotiantian merged commit 9c2bb7d into python:main Oct 15, 2024
36 checks passed
@miss-islington-app
Copy link

Thanks @gaogaotiantian for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@gaogaotiantian gaogaotiantian deleted the pdb-fix-argument branch October 15, 2024 19:30
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 15, 2024
… of fail (pythonGH-125424)

(cherry picked from commit 9c2bb7d)

Co-authored-by: Tian Gao <[email protected]>
Co-authored-by: Irit Katriel <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Oct 15, 2024

GH-125547 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Oct 15, 2024
gaogaotiantian added a commit that referenced this pull request Oct 15, 2024
…d of fail (GH-125424) (#125547)

gh-125115: Pass unknown pdb command line args to script instead of fail (GH-125424)
(cherry picked from commit 9c2bb7d)

Co-authored-by: Tian Gao <[email protected]>
Co-authored-by: Irit Katriel <[email protected]>
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
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.

2 participants