@@ -306,6 +306,8 @@ class Pdb(bdb.Bdb, cmd.Cmd):
306306
307307 _file_mtime_table = {}
308308
309+ _last_pdb_instance = None
310+
309311 def __init__ (self , completekey = 'tab' , stdin = None , stdout = None , skip = None ,
310312 nosigint = False , readrc = True ):
311313 bdb .Bdb .__init__ (self , skip = skip )
@@ -359,6 +361,12 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
359361 self ._chained_exceptions = tuple ()
360362 self ._chained_exception_index = 0
361363
364+ def set_trace (self , frame = None ):
365+ Pdb ._last_pdb_instance = self
366+ if frame is None :
367+ frame = sys ._getframe ().f_back
368+ super ().set_trace (frame )
369+
362370 def sigint_handler (self , signum , frame ):
363371 if self .allow_kbdint :
364372 raise KeyboardInterrupt
@@ -2350,7 +2358,10 @@ def set_trace(*, header=None):
23502358 an assertion fails). If given, *header* is printed to the console
23512359 just before debugging begins.
23522360 """
2353- pdb = Pdb ()
2361+ if Pdb ._last_pdb_instance is not None :
2362+ pdb = Pdb ._last_pdb_instance
2363+ else :
2364+ pdb = Pdb ()
23542365 if header is not None :
23552366 pdb .message (header )
23562367 pdb .set_trace (sys ._getframe ().f_back )
0 commit comments