Skip to content

Commit a936af9

Browse files
pythongh-120144: Make it possible to use sys.monitoring for bdb and make it default for pdb (python#124533)
1 parent f48887f commit a936af9

File tree

7 files changed

+435
-21
lines changed

7 files changed

+435
-21
lines changed

Doc/library/bdb.rst

+50-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ The :mod:`bdb` module also defines two classes:
118118

119119
Count of the number of times a :class:`Breakpoint` has been hit.
120120

121-
.. class:: Bdb(skip=None)
121+
.. class:: Bdb(skip=None, backend='settrace')
122122

123123
The :class:`Bdb` class acts as a generic Python debugger base class.
124124

@@ -132,9 +132,22 @@ The :mod:`bdb` module also defines two classes:
132132
frame is considered to originate in a certain module is determined
133133
by the ``__name__`` in the frame globals.
134134

135+
The *backend* argument specifies the backend to use for :class:`Bdb`. It
136+
can be either ``'settrace'`` or ``'monitoring'``. ``'settrace'`` uses
137+
:func:`sys.settrace` which has the best backward compatibility. The
138+
``'monitoring'`` backend uses the new :mod:`sys.monitoring` that was
139+
introduced in Python 3.12, which can be much more efficient because it
140+
can disable unused events. We are trying to keep the exact interfaces
141+
for both backends, but there are some differences. The debugger developers
142+
are encouraged to use the ``'monitoring'`` backend to achieve better
143+
performance.
144+
135145
.. versionchanged:: 3.1
136146
Added the *skip* parameter.
137147

148+
.. versionchanged:: 3.14
149+
Added the *backend* parameter.
150+
138151
The following methods of :class:`Bdb` normally don't need to be overridden.
139152

140153
.. method:: canonic(filename)
@@ -146,6 +159,20 @@ The :mod:`bdb` module also defines two classes:
146159
<os.path.abspath>`. A *filename* with angle brackets, such as ``"<stdin>"``
147160
generated in interactive mode, is returned unchanged.
148161

162+
.. method:: start_trace(self)
163+
164+
Start tracing. For ``'settrace'`` backend, this method is equivalent to
165+
``sys.settrace(self.trace_dispatch)``
166+
167+
.. versionadded:: 3.14
168+
169+
.. method:: stop_trace(self)
170+
171+
Stop tracing. For ``'settrace'`` backend, this method is equivalent to
172+
``sys.settrace(None)``
173+
174+
.. versionadded:: 3.14
175+
149176
.. method:: reset()
150177

151178
Set the :attr:`!botframe`, :attr:`!stopframe`, :attr:`!returnframe` and
@@ -364,6 +391,28 @@ The :mod:`bdb` module also defines two classes:
364391
Return all breakpoints that are set.
365392

366393

394+
Derived classes and clients can call the following methods to disable and
395+
restart events to achieve better performance. These methods only work
396+
when using the ``'monitoring'`` backend.
397+
398+
.. method:: disable_current_event()
399+
400+
Disable the current event until the next time :func:`restart_events` is
401+
called. This is helpful when the debugger is not interested in the current
402+
line.
403+
404+
.. versionadded:: 3.14
405+
406+
.. method:: restart_events()
407+
408+
Restart all the disabled events. This function is automatically called in
409+
``dispatch_*`` methods after ``user_*`` methods are called. If the
410+
``dispatch_*`` methods are not overridden, the disabled events will be
411+
restarted after each user interaction.
412+
413+
.. versionadded:: 3.14
414+
415+
367416
Derived classes and clients can call the following methods to get a data
368417
structure representing a stack trace.
369418

Doc/library/pdb.rst

+27-1
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,32 @@ slightly different way:
203203
Enter post-mortem debugging of the exception found in
204204
:data:`sys.last_exc`.
205205

206+
.. function:: set_default_backend(backend)
207+
208+
There are two supported backends for pdb: ``'settrace'`` and ``'monitoring'``.
209+
See :class:`bdb.Bdb` for details. The user can set the default backend to
210+
use if none is specified when instantiating :class:`Pdb`. If no backend is
211+
specified, the default is ``'settrace'``.
212+
213+
.. note::
214+
215+
:func:`breakpoint` and :func:`set_trace` will not be affected by this
216+
function. They always use ``'monitoring'`` backend.
217+
218+
.. versionadded:: 3.14
219+
220+
.. function:: get_default_backend()
221+
222+
Returns the default backend for pdb.
223+
224+
.. versionadded:: 3.14
206225

207226
The ``run*`` functions and :func:`set_trace` are aliases for instantiating the
208227
:class:`Pdb` class and calling the method of the same name. If you want to
209228
access further features, you have to do this yourself:
210229

211230
.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \
212-
nosigint=False, readrc=True, mode=None)
231+
nosigint=False, readrc=True, mode=None, backend=None)
213232

214233
:class:`Pdb` is the debugger class.
215234

@@ -235,6 +254,10 @@ access further features, you have to do this yourself:
235254
or ``None`` (for backwards compatible behaviour, as before the *mode*
236255
argument was added).
237256

257+
The *backend* argument specifies the backend to use for the debugger. If ``None``
258+
is passed, the default backend will be used. See :func:`set_default_backend`.
259+
Otherwise the supported backends are ``'settrace'`` and ``'monitoring'``.
260+
238261
Example call to enable tracing with *skip*::
239262

240263
import pdb; pdb.Pdb(skip=['django.*']).set_trace()
@@ -254,6 +277,9 @@ access further features, you have to do this yourself:
254277
.. versionadded:: 3.14
255278
Added the *mode* argument.
256279

280+
.. versionadded:: 3.14
281+
Added the *backend* argument.
282+
257283
.. versionchanged:: 3.14
258284
Inline breakpoints like :func:`breakpoint` or :func:`pdb.set_trace` will
259285
always stop the program at calling frame, ignoring the *skip* pattern (if any).

Doc/whatsnew/3.14.rst

+12
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ ast
423423
that the root node type is appropriate.
424424
(Contributed by Irit Katriel in :gh:`130139`.)
425425

426+
bdb
427+
---
428+
429+
* The :mod:`bdb` module now supports the :mod:`sys.monitoring` backend.
430+
(Contributed by Tian Gao in :gh:`124533`.)
426431

427432
calendar
428433
--------
@@ -843,6 +848,13 @@ pdb
843848
* ``$_asynctask`` is added to access the current asyncio task if applicable.
844849
(Contributed by Tian Gao in :gh:`124367`.)
845850

851+
* :mod:`pdb` now supports two backends: :func:`sys.settrace` and
852+
:mod:`sys.monitoring`. Using :mod:`pdb` CLI or :func:`breakpoint` will
853+
always use the :mod:`sys.monitoring` backend. Explicitly instantiating
854+
:class:`pdb.Pdb` and its derived classes will use the :func:`sys.settrace`
855+
backend by default, which is configurable.
856+
(Contributed by Tian Gao in :gh:`124533`.)
857+
846858

847859
pickle
848860
------

0 commit comments

Comments
 (0)