Python debugger gives incorrect stack trace

There’s an annoying bug in pdb that I have observed with Python 3.6 and 3.8.

If I run this:

test.py:

'''Should not see this in stack trace'''

raise Exception()

with python test.py, the stacktrace I see is correct.

However, if I run it with the debugger (python -m pdb test.py, press c), the stacktrace is different, and shows the first line of the file. Also when pressing l to see the lines around where the debugger has stopped on the exception, it shows the top of the file. That’s very annoying when trying to debug a long script. (Note that only the first stack frame (of my code) is affected.)

At least, I’ve found that this bug is fixed of Python 3.10.

It may have been discovered as soon as 2009 ! (See issues referenced here: bpo-24565: f->f_lineno is now -1 when tracing is not set by xdegaye · Pull Request #6233 · python/cpython · GitHub).

There has been a PEP that seems related, which has been partially implemented in 3.10 with this commit: (I had put a link but I had to remove it since new users can only put 2 links per post)

Why am I bringing this here if the bug is fixed? Because versions of Python below 3.10 are still very much in use, and I cannot believe this bug existed for so long. I use pdb regularly because if often run and debug code on remote workstations and clusters. Not being able to rely on the stacktrace given by pdb is very annoying. (I should note that I haven’t stumbled on that before, since don’t write long monolithic scripts where the important stuff happens outside functions (in the first frame); but I’ve been working on someone else’s code. Yep, blame the other guy! :wink: )

Anybody had a similar experience? Any thoughts?