Skip to content

gh-120144: Make it possible to use sys.monitoring for bdb and make it default for pdb #124533

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 22 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
18e6e9c
Allow using sys.monitoring for bdb
gaogaotiantian Sep 25, 2024
b29aff5
Add basic line ignore
gaogaotiantian Sep 25, 2024
23601f3
Use setttrace as default backend for bdb
gaogaotiantian Sep 25, 2024
c9a92f6
📜🤖 Added by blurb_it.
blurb-it[bot] Sep 25, 2024
8955d78
Use settrace by default for pdb.Pdb, but use monitoring for all
gaogaotiantian Sep 26, 2024
6afc2e7
Only trigger events on thread calling start_trace
gaogaotiantian Oct 16, 2024
b595682
Use local events when possible
gaogaotiantian Oct 16, 2024
addf465
Merge branch 'main' into pdb-use-monitoring
gaogaotiantian Jan 19, 2025
70d5138
Fix ignore and condition of breakpoints
gaogaotiantian Feb 8, 2025
7c83425
Merge branch 'main' into pdb-use-monitoring
gaogaotiantian Feb 8, 2025
fe9971c
Address comments about backend
gaogaotiantian Feb 18, 2025
69a5030
Merge branch 'main' into pdb-use-monitoring
gaogaotiantian Feb 18, 2025
05cc3b0
We need BaseException to handle SystemExit case
gaogaotiantian Feb 18, 2025
e6bc287
Move default_backend to module level and provide utils
gaogaotiantian Feb 19, 2025
f0c1306
Do not need to pass trace_dispatch explicitly
gaogaotiantian Feb 19, 2025
a9b53ed
Add docs
gaogaotiantian Feb 19, 2025
9790085
Add version added
gaogaotiantian Feb 19, 2025
ea811f2
Add new functions to __all__
gaogaotiantian Feb 19, 2025
ad2179e
Merge branch 'main' into pdb-use-monitoring
gaogaotiantian Mar 7, 2025
e4ccd8a
Restart events after user line
gaogaotiantian Mar 7, 2025
d2c1f2e
Merge branch 'main' into pdb-use-monitoring
gaogaotiantian Mar 17, 2025
e9252ec
Remove blank line
gaogaotiantian Mar 17, 2025
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
51 changes: 50 additions & 1 deletion Doc/library/bdb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ The :mod:`bdb` module also defines two classes:

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

.. class:: Bdb(skip=None)
.. class:: Bdb(skip=None, backend='settrace')

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

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

The *backend* argument specifies the backend to use for :class:`Bdb`. It
can be either ``'settrace'`` or ``'monitoring'``. ``'settrace'`` uses
:func:`sys.settrace` which has the best backward compatibility. The
``'monitoring'`` backend uses the new :mod:`sys.monitoring` that was
introduced in Python 3.12, which can be much more efficient because it
can disable unused events. We are trying to keep the exact interfaces
for both backends, but there are some differences. The debugger developers
are encouraged to use the ``'monitoring'`` backend to achieve better
performance.

.. versionchanged:: 3.1
Added the *skip* parameter.

.. versionchanged:: 3.14
Added the *backend* parameter.

The following methods of :class:`Bdb` normally don't need to be overridden.

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

.. method:: start_trace(self)

Start tracing. For ``'settrace'`` backend, this method is equivalent to
``sys.settrace(self.trace_dispatch)``

.. versionadded:: 3.14

.. method:: stop_trace(self)

Stop tracing. For ``'settrace'`` backend, this method is equivalent to
``sys.settrace(None)``

.. versionadded:: 3.14

.. method:: reset()

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


Derived classes and clients can call the following methods to disable and
restart events to achieve better performance. These methods only work
when using the ``'monitoring'`` backend.

.. method:: disable_current_event()

Disable the current event until the next time :func:`restart_events` is
called. This is helpful when the debugger is not interested in the current
line.

.. versionadded:: 3.14

.. method:: restart_events()

Restart all the disabled events. This function is automatically called in
``dispatch_*`` methods after ``user_*`` methods are called. If the
``dispatch_*`` methods are not overridden, the disabled events will be
restarted after each user interaction.

.. versionadded:: 3.14


Derived classes and clients can call the following methods to get a data
structure representing a stack trace.

Expand Down
28 changes: 27 additions & 1 deletion Doc/library/pdb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,32 @@ slightly different way:
Enter post-mortem debugging of the exception found in
:data:`sys.last_exc`.

.. function:: set_default_backend(backend)

There are two supported backends for pdb: ``'settrace'`` and ``'monitoring'``.
See :class:`bdb.Bdb` for details. The user can set the default backend to
use if none is specified when instantiating :class:`Pdb`. If no backend is
specified, the default is ``'settrace'``.

.. note::

:func:`breakpoint` and :func:`set_trace` will not be affected by this
function. They always use ``'monitoring'`` backend.

.. versionadded:: 3.14

.. function:: get_default_backend()

Returns the default backend for pdb.

.. versionadded:: 3.14

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

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

:class:`Pdb` is the debugger class.

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

The *backend* argument specifies the backend to use for the debugger. If ``None``
is passed, the default backend will be used. See :func:`set_default_backend`.
Otherwise the supported backends are ``'settrace'`` and ``'monitoring'``.

Example call to enable tracing with *skip*::

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

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

.. versionchanged:: 3.14
Inline breakpoints like :func:`breakpoint` or :func:`pdb.set_trace` will
always stop the program at calling frame, ignoring the *skip* pattern (if any).
Expand Down
12 changes: 12 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ ast
that the root node type is appropriate.
(Contributed by Irit Katriel in :gh:`130139`.)

bdb
---

* The :mod:`bdb` module now supports the :mod:`sys.monitoring` backend.
(Contributed by Tian Gao in :gh:`124533`.)

calendar
--------
Expand Down Expand Up @@ -833,6 +838,13 @@ pdb
* ``$_asynctask`` is added to access the current asyncio task if applicable.
(Contributed by Tian Gao in :gh:`124367`.)

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


pickle
------
Expand Down
Loading
Loading