8000 gh-123756: Only allow restart in command line mode by gaogaotiantian · Pull Request #123757 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

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

Merged
merged 19 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
32 changes: 31 additions & 1 deletion Doc/library/pdb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ The ``run*`` functions and :func:`set_trace` are aliases for instantiating the
access further features, you have to do this yourself:

.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \
nosigint=False, readrc=True)
nosigint=False, readrc=True, invoke_origin=PdbInvokeOrigin.Unknown)

:class:`Pdb` is the debugger class.

Expand All @@ -211,6 +211,10 @@ access further features, you have to do this yourself:
The *readrc* argument defaults to true and controls whether Pdb will load
.pdbrc files from the filesystem.

The *invoke_origin* argument is used to determine the origin of the debugger.
It should be a value from the :class:`PdbInvokeOrigin` enumeration. The value
is used to determine whether certain commands are available or not.

Example call to enable tracing with *skip*::

import pdb; pdb.Pdb(skip=['django.*']).set_trace()
Expand All @@ -227,6 +231,9 @@ access further features, you have to do this yourself:
.. versionchanged:: 3.6
The *readrc* argument.

.. versionadded:: 3.14
Added the *invoke_origin* argument.

.. method:: run(statement, globals=None, locals=None)
runeval(expression, globals=None, locals=None)
runcall(function, *args, **kwds)
Expand All @@ -235,6 +242,25 @@ access further features, you have to do this yourself:
See the documentation for the functions explained above.


.. class:: PdbInvokeOrigin

An enumeration of the possible origins of the debugger invocation.

.. attribute:: Unknown

The origin of the debugger invocation is unknown.

.. attribute:: CommandLine

The origin of the debugger invocation is from the command line.
(e.g. ``python -m pdb script.py``)

.. attribute:: InlineBreakpoint

The origin of the debugger invocation is from an inline breakpoint.
(e.g. ``breakpoint()`` or ``import pdb; pdb.set_trace()``)


.. _debugger-commands:

Debugger Commands
Expand Down Expand Up @@ -669,6 +695,10 @@ can be overridden by the local file.
History, breakpoints, actions and debugger options are preserved.
:pdbcmd:`restart` is an alias for :pdbcmd:`run`.

.. versionchanged:: 3.14
:pdbcmd:`run` and :pdbcmd:`restart` commands are not allowed when the
debugger is invoked from an inline breakpoint.

.. pdbcommand:: q(uit)

Quit from the debugger. The program being executed is aborted.
Expand Down
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", "PdbInvokeOrigin"]


class PdbInvokeOrigin(enum.Enum):
"""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_origin=PdbInvokeOrigin.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_origin = invoke_origin
# 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_origin == PdbInvokeOrigin.InlineBreakpoint:
self.error('run/restart command is not allowed with inline breakpoints.\n'
'Use the command line interface if you want to restart your program\n'
'e.g. "python -m pdb myscript.py"')
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_origin=PdbInvokeOrigin.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_origin=PdbInvokeOrigin.CommandLine)
pdb.rcLines.extend(opts.commands)
while True:
try:
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,28 @@ def test_pdb_where_command():
(Pdb) continue
"""

def test_pdb_restart_command():
"""Test restart command

>>> def test_function():
... from pdb import Pdb, PdbInvokeOrigin;
... Pdb(nosigint=True, readrc=False, invoke_origin=PdbInvokeOrigin.InlineBreakpoint).set_trace()
... x = 1

>>> with PdbTestInput([ # doctest: +ELLIPSIS
... 'restart',
... 'continue',
... ]):
... test_function()
> <doctest test.test_pdb.test_pdb_restart_command[0]>(3)test_function()
-> Pdb(nosigint=True, readrc=False, invoke_origin=PdbInvokeOrigin.InlineBreakpoint).set_trace()
(Pdb) restart
*** run/restart command is not allowed with inline breakpoints.
Use the command line interface if you want to restart your program
e.g. "python -m pdb myscript.py"
(Pdb) continue
"""


# skip this test if sys.flags.no_site = True;
# exit() isn't defined unless there's a site module.
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', 'PdbInvokeOrigin'),
)
cm('pydoc', ignore=('input', 'output',)) # properties

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a new argument ``invoke_origin`` to :class:`pdb.Pdb`. Only allow :mod:`pdb` from command line to use ``restart`` command.
Loading
0