|
82 | 82 | import inspect
|
83 | 83 | import textwrap
|
84 | 84 | import tokenize
|
| 85 | +import itertools |
85 | 86 | import traceback
|
86 | 87 | import linecache
|
87 | 88 | import _colorize
|
@@ -2433,30 +2434,47 @@ def main():
|
2433 | 2434 | parser.add_argument('-c', '--command', action='append', default=[], metavar='command', dest='commands',
|
2434 | 2435 | help='pdb commands to execute as if given in a .pdbrc file')
|
2435 | 2436 | parser.add_argument('-m', metavar='module', dest='module')
|
2436 |
| - parser.add_argument('args', nargs='*', |
2437 |
| - help="when -m is not specified, the first arg is the script to debug") |
2438 | 2437 |
|
2439 | 2438 | if len(sys.argv) == 1:
|
2440 | 2439 | # If no arguments were given (python -m pdb), print the whole help message.
|
2441 | 2440 | # Without this check, argparse would only complain about missing required arguments.
|
2442 | 2441 | parser.print_help()
|
2443 | 2442 | sys.exit(2)
|
2444 | 2443 |
|
2445 |
| - opts = parser.parse_args() |
| 2444 | + opts, args = parser.parse_known_args() |
| 2445 | + |
| 2446 | + if opts.module: |
| 2447 | + # If a module is being debugged, we consider the arguments after "-m module" to |
| 2448 | + # be potential arguments to the module itself. We need to parse the arguments |
| 2449 | + # before "-m" to check if there is any invalid argument. |
| 2450 | + # e.g. "python -m pdb -m foo --spam" means passing "--spam" to "foo" |
| 2451 | + # "python -m pdb --spam -m foo" means passing "--spam" to "pdb" and is invalid |
| 2452 | + idx = sys.argv.index('-m') |
| 2453 | + args_to_pdb = sys.argv[1:idx] |
| 2454 | + # This will raise an error if there are invalid arguments |
| 2455 | + parser.parse_args(args_to_pdb) |
| 2456 | + else: |
| 2457 | + # If a script is being debugged, then pdb expects the script name as the first argument. |
| 2458 | + # Anything before the script is considered an argument to pdb itself, which would |
| 2459 | + # be invalid because it's not parsed by argparse. |
| 2460 | + invalid_args = list(itertools.takewhile(lambda a: a.startswith('-'), args)) |
| 2461 | + if invalid_args: |
| 2462 | + parser.error(f"unrecognized arguments: {' '.join(invalid_args)}") |
| 2463 | + sys.exit(2) |
2446 | 2464 |
|
2447 | 2465 | if opts.module:
|
2448 | 2466 | file = opts.module
|
2449 | 2467 | target = _ModuleTarget(file)
|
2450 | 2468 | else:
|
2451 |
| - if not opts.args: |
| 2469 | + if not args: |
2452 | 2470 | parser.error("no module or script to run")
|
2453 |
| - file = opts.args.pop(0) |
| 2471 | + file = args.pop(0) |
2454 | 2472 | if file.endswith('.pyz'):
|
2455 | 2473 | target = _ZipTarget(file)
|
2456 | 2474 | else:
|
2457 | 2475 | target = _ScriptTarget(file)
|
2458 | 2476 |
|
2459 |
| - sys.argv[:] = [file] + opts.args # Hide "pdb.py" and pdb options from argument list |
| 2477 | + sys.argv[:] = [file] + args # Hide "pdb.py" and pdb options from argument list |
2460 | 2478 |
|
2461 | 2479 | # Note on saving/restoring sys.argv: it's a good idea when sys.argv was
|
2462 | 2480 | # modified by the script being debugged. It's a bad idea when it was
|
|
0 commit comments