Skip to content

gh-113548: Allow CLI arguments to pdb -m #113557

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 7 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2186,15 +2186,19 @@ def main():
import argparse

parser = argparse.ArgumentParser(prog="pdb",
usage="%(prog)s [-h] [-c command] (-m module | pyfile) [args ...]",
description=_usage,
formatter_class=argparse.RawDescriptionHelpFormatter,
allow_abbrev=False)

parser.add_argument('-c', '--command', action='append', default=[], metavar='command')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-m', metavar='module')
group.add_argument('pyfile', nargs='?')
parser.add_argument('args', nargs="*")
# We need to maunally get the script from args, because the first positional
# arguments could be either the script we need to debug, or the argument
# to the -m module
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.
Expand All @@ -2204,11 +2208,13 @@ def main():

opts = parser.parse_args()

if opts.m:
file = opts.m
if opts.module:
file = opts.module
target = _ModuleTarget(file)
else:
file = opts.pyfile
if not opts.args:
parser.error("no module or script to run")
file = opts.args.pop(0)
target = _ScriptTarget(file)

target.check()
Expand All @@ -2220,7 +2226,7 @@ def main():
# changed by the user from the command line. There is a "restart" command
# which allows explicit specification of command line arguments.
pdb = Pdb()
pdb.rcLines.extend(opts.command)
pdb.rcLines.extend(opts.commands)
while True:
try:
pdb._run(target)
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2959,6 +2959,15 @@ def test_module_is_run_as_main(self):
stdout, stderr = self.run_pdb_module(script, commands)
self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout)

def test_run_module_with_args(self):
commands = """
continue
"""
self._run_pdb(["calendar", "-m"], commands, expected_returncode=2)

stdout, _ = self._run_pdb(["-m", "calendar", "1"], commands)
self.assertIn("December", stdout)

def test_breakpoint(self):
script = """
if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:mod:`pdb` now allows CLI arguments to ``pdb -m``.