From f3626699063fb27a2151f6cd72386566e61dc7d8 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 28 Dec 2023 14:44:24 -0800 Subject: [PATCH 1/6] Allow CLI arguments to pdb -m --- Lib/pdb.py | 14 ++++++++++---- Lib/test/test_pdb.py | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 83b7fefec63636..f61d55d44ca215 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -2186,14 +2186,16 @@ 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) + # 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') - group = parser.add_mutually_exclusive_group(required=True) - group.add_argument('-m', metavar='module') - group.add_argument('pyfile', nargs='?') + parser.add_argument('-m', metavar='module') parser.add_argument('args', nargs="*") if len(sys.argv) == 1: @@ -2208,7 +2210,11 @@ def main(): file = opts.m target = _ModuleTarget(file) else: - file = opts.pyfile + # No module is given, use the first positional argument as the script to debug + if not opts.args: + parser.error("one of the arguments -m pyfile is required") + file = opts.args[0] + opts.args = opts.args[1:] target = _ScriptTarget(file) target.check() diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index d53fe3c611bc35..5c060fcec11432 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -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__': From 3d01c2f82c67d23ba68e1f3fad0c84802fe733c5 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 28 Dec 2023 22:52:47 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-12-28-22-52-45.gh-issue-113548.j6TJ7O.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-12-28-22-52-45.gh-issue-113548.j6TJ7O.rst diff --git a/Misc/NEWS.d/next/Library/2023-12-28-22-52-45.gh-issue-113548.j6TJ7O.rst b/Misc/NEWS.d/next/Library/2023-12-28-22-52-45.gh-issue-113548.j6TJ7O.rst new file mode 100644 index 00000000000000..5edc3c91566e2e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-12-28-22-52-45.gh-issue-113548.j6TJ7O.rst @@ -0,0 +1 @@ +Allow CLI arguments to pdb -m From e833ed4abc3126c9b3dbac3c1137e42665d6d5b5 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 28 Dec 2023 15:04:47 -0800 Subject: [PATCH 3/6] Add some changes requested earlier --- Lib/pdb.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index f61d55d44ca215..f2a35d07e05da4 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -2194,9 +2194,11 @@ def main(): # 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') - parser.add_argument('-m', metavar='module') - parser.add_argument('args', nargs="*") + 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('pyfile', nargs='?') + parser.add_argument('args', nargs='*') if len(sys.argv) == 1: # If no arguments were given (python -m pdb), print the whole help message. @@ -2206,15 +2208,16 @@ def main(): opts = parser.parse_args() - if opts.m: - file = opts.m + if opts.module: + file = opts.module target = _ModuleTarget(file) + # If the module is given, the first positional argument is not the script + if opts.pyfile: + opts.args = [opts.pyfile] + opts.args else: - # No module is given, use the first positional argument as the script to debug - if not opts.args: + if not opts.pyfile: parser.error("one of the arguments -m pyfile is required") - file = opts.args[0] - opts.args = opts.args[1:] + file = opts.pyfile target = _ScriptTarget(file) target.check() @@ -2226,7 +2229,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) From cb8dae1b841d5ed8dcbcef693fdbe420047b7df5 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 26 Mar 2024 10:52:03 -0700 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Lib/pdb.py | 2 +- .../next/Library/2023-12-28-22-52-45.gh-issue-113548.j6TJ7O.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index f2a35d07e05da4..6386fcdb0aaa09 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -2216,7 +2216,7 @@ def main(): opts.args = [opts.pyfile] + opts.args else: if not opts.pyfile: - parser.error("one of the arguments -m pyfile is required") + parser.error("no module or script to run") file = opts.pyfile target = _ScriptTarget(file) diff --git a/Misc/NEWS.d/next/Library/2023-12-28-22-52-45.gh-issue-113548.j6TJ7O.rst b/Misc/NEWS.d/next/Library/2023-12-28-22-52-45.gh-issue-113548.j6TJ7O.rst index 5edc3c91566e2e..972ddeb54822e2 100644 --- a/Misc/NEWS.d/next/Library/2023-12-28-22-52-45.gh-issue-113548.j6TJ7O.rst +++ b/Misc/NEWS.d/next/Library/2023-12-28-22-52-45.gh-issue-113548.j6TJ7O.rst @@ -1 +1 @@ -Allow CLI arguments to pdb -m +:mod:`pdb` now allows CLI arguments to ``pdb -m``. From ce8fec1f4051ffe3719898108df57d4d3ada6b61 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 26 Mar 2024 11:34:32 -0700 Subject: [PATCH 5/6] Clean up the args part code --- Lib/pdb.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 6386fcdb0aaa09..9cb23f1872025a 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -2197,8 +2197,8 @@ 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('pyfile', nargs='?') - parser.add_argument('args', nargs='*') + parser.add_argument('args', nargs='*', + help="the first arg is the script to debug if -m is not specified") if len(sys.argv) == 1: # If no arguments were given (python -m pdb), print the whole help message. @@ -2211,13 +2211,10 @@ def main(): if opts.module: file = opts.module target = _ModuleTarget(file) - # If the module is given, the first positional argument is not the script - if opts.pyfile: - opts.args = [opts.pyfile] + opts.args else: - if not opts.pyfile: + if not opts.args: parser.error("no module or script to run") - file = opts.pyfile + file = opts.args.pop(0) target = _ScriptTarget(file) target.check() From f28e6e63e44942a986a33739112d016aac18b896 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 26 Mar 2024 17:44:07 -0700 Subject: [PATCH 6/6] Update Lib/pdb.py Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Lib/pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 9cb23f1872025a..f02bb8f304f91d 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -2198,7 +2198,7 @@ def main(): 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="the first arg is the script to debug if -m is not specified") + 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.