diff options
author | Tian Gao <gaogaotiantian@hotmail.com> | 2024-09-25 18:18:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-25 18:18:01 (GMT) |
commit | 28efeefab7d577ea4fb6e3f6e82f903f2aee271d (patch) | |
tree | b053d588877512cbab2eb4782fa941cc33661cea /Lib | |
parent | da5855e99a8c2d6ef2bb20124d2ebb862dbb971f (diff) | |
download | cpython-28efeefab7d577ea4fb6e3f6e82f903f2aee271d.zip cpython-28efeefab7d577ea4fb6e3f6e82f903f2aee271d.tar.gz cpython-28efeefab7d577ea4fb6e3f6e82f903f2aee271d.tar.bz2 |
gh-123756: Disable restart command if pdb is in inline mode (#123757)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/pdb.py | 12 | ||||
-rw-r--r-- | Lib/test/test_pdb.py | 21 |
2 files changed, 30 insertions, 3 deletions
@@ -309,7 +309,7 @@ class Pdb(bdb.Bdb, cmd.Cmd): _last_pdb_instance = None def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, - nosigint=False, readrc=True): + nosigint=False, readrc=True, mode=None): bdb.Bdb.__init__(self, skip=skip) cmd.Cmd.__init__(self, completekey, stdin, stdout) sys.audit("pdb.Pdb") @@ -321,6 +321,7 @@ class Pdb(bdb.Bdb, cmd.Cmd): self.mainpyfile = '' self._wait_for_mainpyfile = False self.tb_lineno = {} + self.mode = mode # Try to load readline if it exists try: import readline @@ -1611,6 +1612,11 @@ class Pdb(bdb.Bdb, cmd.Cmd): sys.argv. History, breakpoints, actions and debugger options are preserved. "restart" is an alias for "run". """ + if self.mode == 'inline': + self.error('run/restart command is disabled when pdb is running in inline mode.\n' + 'Use the command line interface to enable restarting your program\n' + 'e.g. "python -m pdb myscript.py"') + return if arg: import shlex argv0 = sys.argv[0:1] @@ -2366,7 +2372,7 @@ def set_trace(*, header=None, commands=None): if Pdb._last_pdb_instance is not None: pdb = Pdb._last_pdb_instance else: - pdb = Pdb() + pdb = Pdb(mode='inline') if header is not None: pdb.message(header) pdb.set_trace(sys._getframe().f_back, commands=commands) @@ -2481,7 +2487,7 @@ def main(): # modified by the script being debugged. It's a bad idea when it was # changed by the user from the command line. There is a "restart" command # which allows explicit specification of command line arguments. - pdb = Pdb() + pdb = Pdb(mode='cli') pdb.rcLines.extend(opts.commands) while True: try: diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 3173b05..84c0e10 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -901,6 +901,27 @@ def test_pdb_where_command(): (Pdb) continue """ +def test_pdb_restart_command(): + """Test restart command + + >>> def test_function(): + ... import pdb; pdb.Pdb(nosigint=True, readrc=False, mode='inline').set_trace() + ... x = 1 + + >>> with PdbTestInput([ # doctest: +ELLIPSIS + ... 'restart', + ... 'continue', + ... ]): + ... test_function() + > <doctest test.test_pdb.test_pdb_restart_command[0]>(2)test_function() + -> import pdb; pdb.Pdb(nosigint=True, readrc=False, mode='inline').set_trace() + (Pdb) restart + *** run/restart command is disabled when pdb is running in inline mode. + Use the command line interface to enable restarting your program + e.g. "python -m pdb myscript.py" + (Pdb) continue + """ + def test_pdb_commands_with_set_trace(): """Test that commands can be passed to Pdb.set_trace() |