Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-123756: Only allow restart in command line mode #123757

Merged
merged 19 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
22 changes: 18 additions & 4 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import bdb
import dis
import code
import enum
import glob
import token
import types
Expand All @@ -96,7 +97,14 @@ class Restart(Exception):
pass

__all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace",
"post_mortem", "help"]
"post_mortem", "help", "PdbInvokeType"]


class PdbInvokeType(enum.Enum):
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
"""Enum to specify the type of invocation for the debugger."""
Unknown = "Unknown"
CommandLine = "CommandLine"
InlineBreakpoint = "InlineBreakpoint"


def find_first_executable_line(code):
Expand Down Expand Up @@ -309,7 +317,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, invoke_type=PdbInvokeType.Unknown):
bdb.Bdb.__init__(self, skip=skip)
cmd.Cmd.__init__(self, completekey, stdin, stdout)
sys.audit("pdb.Pdb")
Expand All @@ -321,6 +329,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
self.mainpyfile = ''
self._wait_for_mainpyfile = False
self.tb_lineno = {}
self.invoke_type = invoke_type
# Try to load readline if it exists
try:
import readline
Expand Down Expand Up @@ -1607,6 +1616,11 @@ def do_run(self, arg):
sys.argv. History, breakpoints, actions and debugger options
are preserved. "restart" is an alias for "run".
"""
if self.invoke_type == PdbInvokeType.InlineBreakpoint:
self.error('run/restart command is not allowed with inline breakpoints.\n'
gaogaotiantian marked this conversation as resolved.
Show resolved Hide resolved
'Use the command line interface if you want to restart your program\n'
'e.g. "python -m pdb myscript.py"')
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
gaogaotiantian marked this conversation as resolved.
Show resolved Hide resolved
return
if arg:
import shlex
argv0 = sys.argv[0:1]
Expand Down Expand Up @@ -2361,7 +2375,7 @@ def set_trace(*, header=None):
if Pdb._last_pdb_instance is not None:
pdb = Pdb._last_pdb_instance
else:
pdb = Pdb()
pdb = Pdb(invoke_type=PdbInvokeType.InlineBreakpoint)
if header is not None:
pdb.message(header)
pdb.set_trace(sys._getframe().f_back)
Expand Down Expand Up @@ -2476,7 +2490,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(invoke_type=PdbInvokeType.CommandLine)
pdb.rcLines.extend(opts.commands)
while True:
try:
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_pyclbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ def test_others(self):
cm('sre_parse', ignore=('dump', 'groups', 'pos')) # from sre_constants import *; property
cm(
'pdb',
# pyclbr does not handle elegantly `typing` or properties
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget'),
# pyclbr does not handle elegantly `typing` or properties or enum
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'PdbInvokeType'),
)
cm('pydoc', ignore=('input', 'output',)) # properties

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Only allow command line usage of :mod:`pdb` to use ``restart`` command.
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
Loading