Skip to content

Commit 970b043

Browse files
authored
gh-148615: Handle -- separator in pdb argument parsing (#148624)
1 parent 2140b14 commit 970b043

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

Lib/pdb.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,6 +3809,10 @@ def parse_args():
38093809
opt_module = parser.parse_args(args[:2])
38103810
opts.module = opt_module.module
38113811
args = args[2:]
3812+
elif args[0] == '--':
3813+
args.pop(0)
3814+
if not args:
3815+
parser.error("missing script or module to run")
38123816
elif args[0].startswith('-'):
38133817
# Invalid argument before the script name.
38143818
invalid_args = list(itertools.takewhile(lambda a: a.startswith('-'), args))

Lib/test/test_pdb.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4718,6 +4718,27 @@ def bar():
47184718
]))
47194719
self.assertIn('break in bar', stdout)
47204720

4721+
def test_end_of_options_separator(self):
4722+
# gh-148615: Test parsing when '--' separator is used
4723+
script = "import sys; print(f'ARGS: {sys.argv[1:]}')"
4724+
with open(os_helper.TESTFN, 'w', encoding='utf-8') as f:
4725+
f.write(script)
4726+
stdout, _ = self._run_pdb(['--', os_helper.TESTFN, '-foo'], 'c\nq')
4727+
self.assertIn("ARGS: ['-foo']", stdout)
4728+
stdout, _ = self._run_pdb(['-c', 'continue', '--', os_helper.TESTFN, '-c', 'foo'], 'q')
4729+
self.assertIn("ARGS: ['-c', 'foo']", stdout)
4730+
stdout, stderr = self._run_pdb(['--'], 'q', expected_returncode=2)
4731+
self.assertIn("missing script or module to run", stderr)
4732+
stdout, stderr = self._run_pdb(['-x', '--', os_helper.TESTFN], 'q', expected_returncode=2)
4733+
self.assertIn("unrecognized arguments: -x", stderr)
4734+
stdout, _ = self._run_pdb([os_helper.TESTFN, '--', 'arg'], 'c\nq')
4735+
self.assertIn("ARGS: ['--', 'arg']", stdout)
4736+
with os_helper.temp_cwd():
4737+
with open('mymod.py', 'w', encoding='utf-8') as f:
4738+
f.write(script)
4739+
stdout, _ = self._run_pdb(['-m', 'mymod', '--', 'arg'], 'c\nq')
4740+
self.assertIn("ARGS: ['--', 'arg']", stdout)
4741+
47214742
@unittest.skipIf(SKIP_CORO_TESTS, "Coroutine tests are skipped")
47224743
def test_async_break(self):
47234744
script = """
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`pdb` to accept standard -- end of options separator. Reported by haampie. Patched by Shrey Naithani.

0 commit comments

Comments
 (0)