Skip to content

Conversation

@gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Nov 3, 2025

We are at a point that we need to do customize parsing for pdb in order to make the positional argument passing intuitive.

For example

# -m 1 should be passed
python -m pdb -m calendar -m 1
# -p 1 should be passed
python -m pdb -m foo -p 3
# -p 1 should be passed
python -m pdb foo.py -p 1

The order of the arguments now matter (especially when we use a very common option -p).

Now pdb try to

  1. Extract all -c commands (for backward compatible reason it still get commands after the script/module).
  2. Locate the target based on order (the first one except for -c).
  3. Consider the rest of the arguments to be positional arguments to the script/module itself.

@gaogaotiantian gaogaotiantian added the needs backport to 3.14 bugs and security fixes label Nov 3, 2025
@gaogaotiantian
Copy link
Member Author

Hey @iritkatriel , I think it's time to do some customized parsing for pdb, because we want to resolve the ambiguity for the users. LMK if you have any questions. I added backport to 3.14 because this is indeed kind of a bug. Especially considering 3.14 introduced -p which could conflict a lot.

parser.add_argument('-p', '--pid', type=int, help="attach to the specified PID", default=None)
parser.print_help()
sys.exit(2)
elif args[0] == '-p' or args[0] == '--pid':
Copy link
Member

Choose a reason for hiding this comment

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

Is -p always the first arg?

Copy link
Member Author

Choose a reason for hiding this comment

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

So we already skimmed all the -c commands, which are the only "valid options" to all three kinds of invocation. Then we need to determine which path (script, module or pid) we should take. There's no case where -p is not the first argument (after skimming -c) but we should still consider a pid attach.

Copy link
Member

@iritkatriel iritkatriel left a comment

Choose a reason for hiding this comment

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

Probably worth getting a review from @savannahostrowski as the argparse maintainer.

Copy link
Member

@savannahostrowski savannahostrowski left a comment

Choose a reason for hiding this comment

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

I think custom parsing logic is necessary here, given that parse_known_args is too greedy and REMAINDER is plagued with issues (see #58382 as an example). There's no combination of argparse features that will make this work, AFAIK.

On a more meta note, it does make me wonder whether it'd be worth exposing a proper passthrough parameter in argparse itself to simplify things, especially since this scenario is actually not that uncommon.

@gaogaotiantian
Copy link
Member Author

Hi @savannahostrowski and @iritkatriel , any more comments on this PR?

@gaogaotiantian gaogaotiantian merged commit 5348c20 into python:main Nov 16, 2025
50 checks passed
@miss-islington-app
Copy link

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

@gaogaotiantian gaogaotiantian deleted the pdb-parse branch November 16, 2025 18:58
@miss-islington-app
Copy link

Sorry, @gaogaotiantian, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 5348c200f5b26d6dd21d900b2b4cb684150d4b01 3.14

@bedevere-app
Copy link

bedevere-app bot commented Nov 16, 2025

GH-141635 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Nov 16, 2025
gaogaotiantian added a commit that referenced this pull request Nov 16, 2025
…ents can pass through (GH-140933) (#141635)

(cherry-picked from commit 5348c20)
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