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.

Title: pdb.set_trace() clobbering traceback on error
Type: behavior Stage: resolved
Components: Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: Aaron.Meurer, akaptur, asmeurer, asvetlov, blueyed, inglesp, iritkatriel, ishimoto, jcea, ned.deily, serhiy.storchaka, xdegaye, xtreak
Priority: normal Keywords: patch

Created on 2012-11-15 17:55 by akaptur, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6233 blueyed, 2018-03-27 11:05
Messages (11)
msg175630 - (view) Author: A Kaptur (akaptur) * (Python triager) Date: 2012-11-15 17:55
pdb.set_trace() is overwriting the actual traceback when exiting with an error.  See minimal recreation here:
msg175968 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2012-11-19 21:00
The top level frame line number is not updated because it has a local
trace function while the global trace function is None. This is
related to issue 7238.

The following patch fixes the issue. The patch removes the local trace
at the top level frame and makes sure it is not reinstalled when
returning from the current trace function.

diff --git a/Lib/ b/Lib/
--- a/Lib/
+++ b/Lib/
@@ -64,6 +64,10 @@
         if self.stop_here(frame) or self.break_here(frame):
             if self.quitting: raise BdbQuit
+            # Do not re-install the local trace when we are finished debugging,
+            # see issues 16482 and 7238.
+            if not sys.gettrace():
+                return None
         return self.trace_dispatch
     def dispatch_call(self, frame, arg):
@@ -231,8 +235,10 @@
             # no breakpoints; run without debugger overhead
             frame = sys._getframe().f_back
-            while frame and frame is not self.botframe:
+            while frame:
                 del frame.f_trace
+                if frame is self.botframe:
+                    break
                 frame = frame.f_back
     def set_quit(self):

The following code is a minimum implementation of pdb with the patch
applied and the associated code to test it.

class Bdb:

    def trace_dispatch(self, frame, event, arg):
        if sys.gettrace():
            return self.trace_dispatch

    def set_trace(self, frame):
        self.botframe = frame
        frame.f_trace = self.trace_dispatch

    def set_continue(self):
        del self.botframe.f_trace

frame = sys._getframe()
d = Bdb()

y = "line of code not triggering an error"
x = 1
assert x != 1
msg176271 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2012-11-24 10:40
See also how it is fixed at
msg186583 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2013-04-11 19:39
See also Issue17697.  Xavier, would you be willing to submit a patch with a test?
msg186642 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2013-04-12 15:28
The last patch proposed at issue 17277 and named traced_frame.patch
fixes also issue 17697, and issues 7238, 16482. IMHO one should remove
the assumption in PyFrame_GetLineNumber() that f_lineno is correct
when f_trace is not NULL, as there does not seem to be a way to remove
the local trace function of a generator frame. The traced_frame patch
does that.
msg186643 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2013-04-12 15:30
Forgot to say that traced_frame.patch includes a test.
msg234999 - (view) Author: Peter Inglesby (inglesp) * Date: 2015-01-29 23:00
I've just hit this.  Is there anything I can do to help get this fixed?`
msg314514 - (view) Author: daniel hahler (blueyed) * Date: 2018-03-27 11:05
Just for reference: is about fixing this.
msg331064 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-12-04 17:08
I too just hit this issue with pdb and checked out the PR locally. It works fine though it has merge conflicts with latest master. I am adding 3.8, 3.7 and 3.6 removing the older versions that don't support bug fixes. Since the PR has unknown repository I suppose @xdegaye has deleted their fork and I don't know if the PR can be revived without opening a new one.

I have merged the latest master in my own fork resolving conflicts in case a new PR has to be generated : pr-6233-latest"> Adding Serhiy to the issue. The PR also solves many linked issues in the PR and I thought to add the update here since it seemed appropriate.

Thanks much @xdegaye for the PR
msg331068 - (view) Author: Aaron Meurer (asmeurer) Date: 2018-12-04 17:44
You can download the branch for a pull request even if the repo is deleted using this That will let you keep the original commits intact.
msg410264 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-10 23:29
iritkatriel@Irits-MBP cpython % cat

import pdb

x = 0

while True:
	y = "line of code not triggering an error"
	x += 1
	assert x != 3
iritkatriel@Irits-MBP cpython % cat

import pdb

x = 0

while True:
	y = "line of code not triggering an error"
	x += 1
	assert x != 3
iritkatriel@Irits-MBP cpython % ./python.exe
> /Users/iritkatriel/src/cpython/<module>()
-> y = "line of code not triggering an error"
(Pdb) c
> /Users/iritkatriel/src/cpython/<module>()
-> y = "line of code not triggering an error"
(Pdb) c
> /Users/iritkatriel/src/cpython/<module>()
-> y = "line of code not triggering an error"
(Pdb) c
Traceback (most recent call last):
  File "/Users/iritkatriel/src/cpython/", line 10, in <module>
    assert x != 3
Date User Action Args
2022-04-11 14:57:38adminsetgithub: 60686
2022-01-18 09:39:06iritkatrielsetstatus: pending -> closed
resolution: out of date
stage: patch review -> resolved
2022-01-10 23:29:48iritkatrielsetstatus: open -> pending
nosy: + iritkatriel
messages: + msg410264

2018-12-04 17:44:14asmeurersetnosy: + asmeurer
messages: + msg331068
2018-12-04 17:08:21xtreaksetnosy: + serhiy.storchaka, xtreak

messages: + msg331064
versions: + Python 3.6, Python 3.7, Python 3.8, - Python 3.2, Python 3.3, Python 3.4
2018-12-04 16:32:14xtreaklinkissue35405 superseder
2018-03-27 11:05:02blueyedsetnosy: + blueyed
messages: + msg314514
pull_requests: + pull_request5995

keywords: + patch
stage: needs patch -> patch review
2017-12-20 08:54:11ishimotosetnosy: + ishimoto
2015-05-08 00:36:46Aaron.Meurersetnosy: + Aaron.Meurer
2015-01-29 23:00:58inglespsetnosy: + inglesp
messages: + msg234999
2013-04-12 15:30:08xdegayesetmessages: + msg186643
2013-04-12 15:28:37xdegayesetmessages: + msg186642
2013-04-11 19:39:06ned.deilysetversions: + Python 3.4
nosy: + ned.deily

messages: + msg186583

stage: needs patch
2013-02-25 17:47:00jceasetnosy: + jcea
2012-12-05 11:08:51asvetlovsetnosy: + asvetlov
2012-11-24 10:40:15xdegayesetmessages: + msg176271
2012-11-19 21:00:49xdegayesetnosy: + xdegaye
messages: + msg175968
2012-11-15 17:55:25akapturcreate