Skip to content

Commit a9b53ed

Browse files
Add docs
1 parent f0c1306 commit a9b53ed

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

Doc/library/bdb.rst

Lines changed: 42 additions & 1 deletion
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,16 @@ 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+
.. method:: stop_trace(self)
168+
169+
Stop tracing. For ``'settrace'`` backend, this method is equivalent to
170+
``sys.settrace(None)``
171+
149172
.. method:: reset()
150173

151174
Set the :attr:`!botframe`, :attr:`!stopframe`, :attr:`!returnframe` and
@@ -364,6 +387,24 @@ The :mod:`bdb` module also defines two classes:
364387
Return all breakpoints that are set.
365388

366389

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

Doc/library/pdb.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,32 @@ slightly different way:
194194
Enter post-mortem debugging of the exception found in
195195
:data:`sys.last_exc`.
196196

197+
.. function:: set_default_backend(backend)
198+
199+
There are two supported backends for pdb: ``'settrace'`` and ``'monitoring'``.
200+
See :class:`bdb.Bdb` for details. The user can set the default backend to
201+
use if none is specified when instantiating :class:`Pdb`. If no backend is
202+
specified, the default is ``'settrace'``.
203+
204+
.. note::
205+
206+
:func:`breakpoint` and :func:`set_trace` will not be affected by this
207+
function. They always use ``'monitoring'`` backend.
208+
209+
.. versionadded:: 3.14
210+
211+
.. function:: get_default_backend()
212+
213+
Returns the default backend for pdb.
214+
215+
.. versionadded:: 3.14
197216

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

202221
.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \
203-
nosigint=False, readrc=True, mode=None)
222+
nosigint=False, readrc=True, mode=None, backend=None)
204223

205224
:class:`Pdb` is the debugger class.
206225

@@ -226,6 +245,10 @@ access further features, you have to do this yourself:
226245
or ``None`` (for backwards compatible behaviour, as before the *mode*
227246
argument was added).
228247

248+
The *backend* argument specifies the backend to use for the debugger. If ``None``
249+
is passed, the default backend will be used. See :func:`set_default_backend`.
250+
Otherwise the supported backends are ``'settrace'`` and ``'monitoring'``.
251+
229252
Example call to enable tracing with *skip*::
230253

231254
import pdb; pdb.Pdb(skip=['django.*']).set_trace()
@@ -245,6 +268,9 @@ access further features, you have to do this yourself:
245268
.. versionadded:: 3.14
246269
Added the *mode* argument.
247270

271+
.. versionadded:: 3.14
272+
Added the *backend* argument.
273+
248274
.. method:: run(statement, globals=None, locals=None)
249275
runeval(expression, globals=None, locals=None)
250276
runcall(function, *args, **kwds)

Doc/whatsnew/3.14.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ ast
350350
that the root node type is appropriate.
351351
(Contributed by Irit Katriel in :gh:`130139`.)
352352

353+
bdb
354+
---
355+
356+
* The :mod:`bdb` module now supports the :mod:`sys.monitoring` backend.
357+
(Contributed by Tian Gao in :gh:`124533`.)
353358

354359
calendar
355360
--------
@@ -684,6 +689,12 @@ pdb
684689
the quit and call :func:`sys.exit`, instead of raising :exc:`bdb.BdbQuit`.
685690
(Contributed by Tian Gao in :gh:`124704`.)
686691

692+
* :mod:`pdb` now supports two backends: :func:`sys.settrace` and
693+
:mod:`sys.monitoring`. Using :mod:`pdb` CLI or :func:`breakpoint` will
694+
always use the :mod:`sys.monitoring` backend. Explicitly instantiating
695+
:class:`pdb.Pdb` and its derived classes will use the :func:`sys.settrace`
696+
backend by default, which is configurable.
697+
(Contributed by Tian Gao in :gh:`124533`.)
687698

688699
pickle
689700
------

0 commit comments

Comments
 (0)