Skip to content

pdb: -- argument separator is rejected #148615

@haampie

Description

@haampie

Bug report

Bug description:

When using python -m pdb, it is impossible to pass arguments that share names with pdb flags (such as -c) to the target script:

python3 -m pdb -- script.py ...

Standard argparse behavior dictates using -- to separate parser arguments from script arguments, but pdb currently intercepts -- and crashes.

In old versions of Python (3.9) that used getopt for parsing, -- worked fine.

It seems to have regressed in Python 3.13, and #140933 does not fix it.

Reproducer

# Attempt to pass '-c' to the target script using '--'
python3 -m pdb -c continue -- my_script.py -c example

Expected behavior
pdb starts debugging my_script.py with sys.argv = ['my_script.py', '-c', "example"].

Actual behavior

usage: python3 -m pdb [-h] [-c command] (-m module | -p pid | pyfile) [args ...]
python3 -m pdb: error: unrecognized arguments: --

This is triggered by the following branch

    elif args[0].startswith('-'):
        # Invalid argument before the script name.
        invalid_args = list(itertools.takewhile(lambda a: a.startswith('-'), args))
        parser.error(f"unrecognized arguments: {' '.join(invalid_args)}")

that branch could possibly do something like:

    elif args[0].startswith('-'):
        if args[0] == '--':
            args.pop(0)  # Discard the separator
            if not args:
                parser.error("missing script or module to run")
        else:
            # Invalid argument before the script name.
            invalid_args = list(itertools.takewhile(lambda a: a.startswith('-'), args))
            parser.error(f"unrecognized arguments: {' '.join(invalid_args)}")

CPython versions tested on:

3.13, 3.14, 3.15, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions