]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-113548: Allow CLI arguments to `pdb -m` (#113557)
authorTian Gao <gaogaotiantian@hotmail.com>
Wed, 27 Mar 2024 01:20:12 +0000 (18:20 -0700)
committerGitHub <noreply@github.com>
Wed, 27 Mar 2024 01:20:12 +0000 (01:20 +0000)
Lib/pdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2023-12-28-22-52-45.gh-issue-113548.j6TJ7O.rst [new file with mode: 0644]

index f8f42ddcdb2b203aae5d3834bf37b0207935d583..f80171d172b23e2b74163ba98a3005cddde24d6b 100755 (executable)
@@ -2250,15 +2250,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.
@@ -2268,11 +2272,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()
@@ -2284,7 +2290,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)
index 9ee994e3fe309d2b220752a8875b66fe9a55a9f1..eedbcec1e66dcb7f60f89176f9e5f9f7cb1b01f9 100644 (file)
@@ -3057,6 +3057,15 @@ def bœr():
         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__':
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 (file)
index 0000000..972ddeb
--- /dev/null
@@ -0,0 +1 @@
+:mod:`pdb` now allows CLI arguments to ``pdb -m``.