-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
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
Changes from 4 commits
f17462c
8e8bbd5
1f40040
160bab6
36dcdd0
aac3cf6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2435,30 +2435,52 @@ def main(): | |||||||||||||||
| parser.add_argument('-c', '--command', action='append', default=[], metavar='command', dest='commands', | ||||||||||||||||
| help='pdb commands to execute as if given in a .pdbrc file') | ||||||||||||||||
| parser.add_argument('-m', metavar='module', dest='module') | ||||||||||||||||
| parser.add_argument('args', nargs='*', | ||||||||||||||||
| help="when -m is not specified, the first arg is the script to debug") | ||||||||||||||||
|
|
||||||||||||||||
| if len(sys.argv) == 1: | ||||||||||||||||
| # If no arguments were given (python -m pdb), print the whole help message. | ||||||||||||||||
| # Without this check, argparse would only complain about missing required arguments. | ||||||||||||||||
| parser.print_help() | ||||||||||||||||
| sys.exit(2) | ||||||||||||||||
|
|
||||||||||||||||
| opts = parser.parse_args() | ||||||||||||||||
| opts, args = parser.parse_known_args() | ||||||||||||||||
|
|
||||||||||||||||
| if opts.module: | ||||||||||||||||
| # If a module is being debugged, we consider the arguments after "-m module" to | ||||||||||||||||
| # be potential arguments to the module itself. We need to parse the arguments | ||||||||||||||||
| # before "-m" to check if there is any invalid argument. | ||||||||||||||||
| # e.g. "python -m pdb -m foo --spam" means passing "--spam" to "foo" | ||||||||||||||||
| # "python -m pdb --spam -m foo" means passing "--spam" to "pdb" and is invalid | ||||||||||||||||
| idx = sys.argv.index('-m') | ||||||||||||||||
| args_to_pdb = sys.argv[1:idx] | ||||||||||||||||
| # This will automatically raise an error if there are invalid arguments | ||||||||||||||||
| parser.parse_args(args_to_pdb) | ||||||||||||||||
| else: | ||||||||||||||||
| # If a script is being debugged, then pdb expects a script as the first argument. | ||||||||||||||||
gaogaotiantian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
| # Anything before the script is considered an argument to pdb itself, which would | ||||||||||||||||
| # be invalid because it's not parsed by argparse. | ||||||||||||||||
| invalid_args = [] | ||||||||||||||||
| for arg in args: | ||||||||||||||||
| if not arg.startswith('-'): | ||||||||||||||||
| break | ||||||||||||||||
| invalid_args.append(arg) | ||||||||||||||||
|
|
||||||||||||||||
| if invalid_args: | ||||||||||||||||
|
||||||||||||||||
| 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)): |
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.
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.
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.
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.
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.
pdb imports traceback which imports itertools.
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 think if the stdlib is broken you probably need to debug it on a previous version of python.
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.
Okay I used takewhile for invalid arguments now, but we do need the full list so I converted it to a list as well.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fixed a bug in :mod:`pdb` where arguments starting with ``-`` can't be passed to the debugged script. |
Uh oh!
There was an error while loading. Please reload this page.