This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pdb: restore original tracing function instead of sys.settrace(None)
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: blueyed, remi.lapeyre, xdegaye
Priority: normal Keywords: patch

Created on 2019-05-22 12:44 by blueyed, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 13497 closed remi.lapeyre, 2019-05-22 14:46
Messages (12)
msg343188 - (view) Author: daniel hahler (blueyed) * Date: 2019-05-22 12:44
bdb/pdb currently uses `sys.settrace(None)` when uninstalling its trace function (trace_dispatch), but should rather store the original trace function in the beginning and use this instead of `None`.

While typically pdb is not used in tests, it is just good practice, given that there can only be a single trace function.

I've done this via monkeypatching for pdbpp's tests, which resulted in an easy 2% coverage gain (https://github.com/antocuni/pdb/pull/253).
msg343197 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-05-22 14:47
Hi @blueyed, can you confirm PR 13497 solve your issue ?
msg343220 - (view) Author: daniel hahler (blueyed) * Date: 2019-05-22 16:51
Looks great, thanks!
msg343463 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2019-05-25 09:35
> While typically pdb is not used in tests, it is just good practice, given that there can only be a single trace function.

IMO invoking "good practice" is not sufficient to motivate such a change.

The proposed change is not backward compatible. A common use case for pdb is to insert a so-called hard breakpoint (import pdb; pdb.set_trace()) line in existing code. In that case the proposed change builds an implicit stack of trace functions that is increased each time the interpreter executes this hard breakpoint (when set in a loop or in a function invoked multiple times for example). When the user hits the 'continue' pdb command, then pdb stops at the next function call if the hard breakpoint has been hit more than once instead of leaving the pdb session for good as it does currently.
msg343467 - (view) Author: daniel hahler (blueyed) * Date: 2019-05-25 10:33
> In that case the proposed change builds an implicit stack of trace functions that is increased each time the interpreter executes this hard breakpoint

Very valid and good point.

Would it work to store it on the class then (once)?

FWIW: pdbpp uses a global Pdb instance - mainly to support the use case of hard breakpoints like this (to remember/re-use its configuration then).
msg343485 - (view) Author: daniel hahler (blueyed) * Date: 2019-05-25 14:53
I've just found that I've created an issue with regard to `do_debug` for this already (https://bugs.python.org/issue36388), and a PR: https://github.com/python/cpython/pull/12479.
msg343488 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2019-05-25 15:42
> Would it work to store it on the class then (once)?

A motivation for this change should be provided first: the incorrect behavior it is trying to fix or the enhancement it is providing.
msg343558 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-05-26 16:32
> The proposed change is not backward compatible.

Indeed, I missed that thanks. I think I could fix that.

> A motivation for this change should be provided first

Is getting accurate coverage reports not enough?
msg343577 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2019-05-26 18:53
> Is getting accurate coverage reports not enough?

But it does not achieve that goal, does it ?
msg343587 - (view) Author: daniel hahler (blueyed) * Date: 2019-05-26 23:42
From my initial comment:

> I've done this via monkeypatching for pdbpp's tests, which resulted in an easy 2% coverage gain (https://github.com/antocuni/pdb/pull/253).

It will also affect running tests with coverage (in general, or using tools like pytest-testmon), and using pdb.set_trace then (either for debugging, or via post-mortem, e.g. pytest's `--pdb`).
msg343611 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2019-05-27 08:57
The main goal of test coverage is to verify that the tests cover the code that is being tested, it is not to get '100 %' printed. Measuring the coverage of the tests themselves is entirely another matter and not very useful. Since it is not possible to measure the coverage of code executed by a trace function, attempting to increase the measure of the coverage of the tests themselves that test pdb, just for the sake of it, does not justify the added complexity and possible source of confusion.

-1 for this change.
msg344688 - (view) Author: daniel hahler (blueyed) * Date: 2019-06-05 09:31
Just for reference: I've just spent quite some time debugging missing coverage with pytest, and it turned out that part of it is because of this issue.  Most of the pdb related tests run in a subprocess (via pexpect), but not all of them.

I am using a wrapper now to restore this manually (https://github.com/pytest-dev/pytest/pull/5406).

Even though I was aware of this issue, it took me quite a while, mainly because this started out with slash related issues for Windows only, and I was debugging this on/via CI only at first.
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81192
2019-06-05 09:31:05blueyedsetmessages: + msg344688
2019-05-27 08:57:19xdegayesetmessages: + msg343611
2019-05-26 23:42:34blueyedsetmessages: + msg343587
2019-05-26 18:53:21xdegayesetmessages: + msg343577
2019-05-26 16:32:12remi.lapeyresetmessages: + msg343558
2019-05-25 15:42:49xdegayesetmessages: + msg343488
2019-05-25 14:53:00blueyedsetmessages: + msg343485
2019-05-25 10:33:32blueyedsetmessages: + msg343467
2019-05-25 09:35:57xdegayesetnosy: + xdegaye
messages: + msg343463
2019-05-22 16:51:23blueyedsetmessages: + msg343220
2019-05-22 14:47:16remi.lapeyresetnosy: + remi.lapeyre
messages: + msg343197
2019-05-22 14:46:23remi.lapeyresetkeywords: + patch
stage: patch review
pull_requests: + pull_request13413
2019-05-22 12:45:16blueyedsetversions: + Python 3.8
2019-05-22 12:44:44blueyedcreate