Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 5 additions & 24 deletions 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, invoke_origin=PdbInvokeOrigin.Unknown)
nosigint=False, readrc=True, mode=None)

:class:`Pdb` is the debugger class.

Expand All @@ -211,9 +211,9 @@ 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.
The *mode* argument is used to indicate the origin of the debugger.
It should be either ``None``, ``'inline'`` or ``'cli'``. It impacts
the workings of some debugger commands.

Example call to enable tracing with *skip*::

Expand All @@ -232,7 +232,7 @@ access further features, you have to do this yourself:
The *readrc* argument.

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

.. method:: run(statement, globals=None, locals=None)
runeval(expression, globals=None, locals=None)
Expand All @@ -242,25 +242,6 @@ 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
20 changes: 6 additions & 14 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
import bdb
import dis
import code
import enum
import glob
import token
import types
Expand All @@ -97,14 +96,7 @@ class Restart(Exception):
pass

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


class PdbInvokeOrigin(enum.Enum):
"""Enum to specify the type of invocation for the debugger."""
Unknown = "Unknown"
CommandLine = "CommandLine"
InlineBreakpoint = "InlineBreakpoint"
"post_mortem", "help"]


def find_first_executable_line(code):
Expand Down Expand Up @@ -317,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, invoke_origin=PdbInvokeOrigin.Unknown):
nosigint=False, readrc=True, mode=None):
bdb.Bdb.__init__(self, skip=skip)
cmd.Cmd.__init__(self, completekey, stdin, stdout)
sys.audit("pdb.Pdb")
Expand All @@ -329,7 +321,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
self.mode = mode
# Try to load readline if it exists
try:
import readline
Expand Down Expand Up @@ -1616,7 +1608,7 @@ 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:
if self.mode == 'inline':
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"')
Expand Down Expand Up @@ -2375,7 +2367,7 @@ def set_trace(*, header=None):
if Pdb._last_pdb_instance is not None:
pdb = Pdb._last_pdb_instance
else:
pdb = Pdb(invoke_origin=PdbInvokeOrigin.InlineBreakpoint)
pdb = Pdb(mode='inline')
if header is not None:
pdb.message(header)
pdb.set_trace(sys._getframe().f_back)
Expand Down Expand Up @@ -2490,7 +2482,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(invoke_origin=PdbInvokeOrigin.CommandLine)
pdb = Pdb(mode='cli')
pdb.rcLines.extend(opts.commands)
while True:
try:
Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,17 +905,16 @@ 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()
... 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]>(3)test_function()
-> Pdb(nosigint=True, readrc=False, invoke_origin=PdbInvokeOrigin.InlineBreakpoint).set_trace()
> <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 not allowed with inline breakpoints.
Use the command line interface if you want to restart your program
Expand Down
Loading