]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-148615: Handle `--` separator in pdb argument parsing (#148624)
authorShrey Naithani <shrey.naithani@shelllite.tech>
Wed, 6 May 2026 04:22:58 +0000 (09:52 +0530)
committerGitHub <noreply@github.com>
Wed, 6 May 2026 04:22:58 +0000 (21:22 -0700)
Lib/pdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2026-04-15-16-08-12.gh-issue-148615.Uvx50R.rst [new file with mode: 0644]

index f2a653cf53c74899e2e8092509d7f7c29f15fa10..01451f0229cacb2d941018ee926bbaece7170e2d 100644 (file)
@@ -3809,6 +3809,10 @@ def parse_args():
         opt_module = parser.parse_args(args[:2])
         opts.module = opt_module.module
         args = args[2:]
+    elif args[0] == '--':
+        args.pop(0)
+        if not args:
+            parser.error("missing script or module to run")
     elif args[0].startswith('-'):
         # Invalid argument before the script name.
         invalid_args = list(itertools.takewhile(lambda a: a.startswith('-'), args))
index 8b6ccfbf051e6e5784f73226d7d894add062348a..410f1436ed4d20a1d13d03429d35c871cb466be9 100644 (file)
@@ -4718,6 +4718,27 @@ def bœr():
             ]))
             self.assertIn('break in bar', stdout)
 
+    def test_end_of_options_separator(self):
+        # gh-148615: Test parsing when '--' separator is used
+        script = "import sys; print(f'ARGS: {sys.argv[1:]}')"
+        with open(os_helper.TESTFN, 'w', encoding='utf-8') as f:
+            f.write(script)
+        stdout, _ = self._run_pdb(['--', os_helper.TESTFN, '-foo'], 'c\nq')
+        self.assertIn("ARGS: ['-foo']", stdout)
+        stdout, _ = self._run_pdb(['-c', 'continue', '--', os_helper.TESTFN, '-c', 'foo'], 'q')
+        self.assertIn("ARGS: ['-c', 'foo']", stdout)
+        stdout, stderr = self._run_pdb(['--'], 'q', expected_returncode=2)
+        self.assertIn("missing script or module to run", stderr)
+        stdout, stderr = self._run_pdb(['-x', '--', os_helper.TESTFN], 'q', expected_returncode=2)
+        self.assertIn("unrecognized arguments: -x", stderr)
+        stdout, _ = self._run_pdb([os_helper.TESTFN, '--', 'arg'], 'c\nq')
+        self.assertIn("ARGS: ['--', 'arg']", stdout)
+        with os_helper.temp_cwd():
+            with open('mymod.py', 'w', encoding='utf-8') as f:
+                f.write(script)
+            stdout, _ = self._run_pdb(['-m', 'mymod', '--', 'arg'], 'c\nq')
+            self.assertIn("ARGS: ['--', 'arg']", stdout)
+
     @unittest.skipIf(SKIP_CORO_TESTS, "Coroutine tests are skipped")
     def test_async_break(self):
         script = """
diff --git a/Misc/NEWS.d/next/Library/2026-04-15-16-08-12.gh-issue-148615.Uvx50R.rst b/Misc/NEWS.d/next/Library/2026-04-15-16-08-12.gh-issue-148615.Uvx50R.rst
new file mode 100644 (file)
index 0000000..f023f01
--- /dev/null
@@ -0,0 +1 @@
+Fix :mod:`pdb` to accept standard -- end of options separator. Reported by haampie. Patched by Shrey Naithani.