Skip to content
Open
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
4 changes: 4 additions & 0 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3634,6 +3634,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))
Expand Down
34 changes: 34 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4173,6 +4173,20 @@ def test_run_script_with_args(self):
stdout, stderr = self.run_pdb_script(script, commands, script_args=["--bar", "foo"])
self.assertIn("['--bar', 'foo']", stdout)

def test_run_script_with_double_dash(self):
script = "import sys; print(sys.argv)"
commands = "continue\nquit"
filename = 'main.py'
with open(filename, 'w') as f:
f.write(script)
self.addCleanup(os_helper.unlink, filename)
stdout, _ = self._run_pdb(["--", filename, "-c", "example"], commands)
self.assertIn(f"['{filename}', '-c', 'example']", stdout)
stdout, _ = self._run_pdb(["-c", "continue", "--", filename, "-c", "example"], "quit")
self.assertIn(f"['{filename}', '-c', 'example']", stdout)
stdout, stderr = self._run_pdb(["--"], "", expected_returncode=2)
self.assertIn("pdb: error: missing script or module to run", stderr)

def test_breakpoint(self):
script = """
if __name__ == '__main__':
Expand Down Expand Up @@ -4752,6 +4766,26 @@ def foo(self):
stdout, stderr = self.run_pdb_script(script, commands)
self.assertIn("The specified object 'C.foo' is not a function", 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)

class ChecklineTests(unittest.TestCase):
def setUp(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :mod:`pdb` to accept standard ``--`` end of options separator,
which was inadvertently rejected since the switch from :mod:`getopt` to
:mod:`argparse` in Python 3.13. Reported by haampie. Patched by Shrey Naithani.
Loading