@@ -309,7 +309,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
309309 _last_pdb_instance = None
310310
311311 def __init__ (self , completekey = 'tab' , stdin = None , stdout = None , skip = None ,
312- nosigint = False , readrc = True , commands = None ):
312+ nosigint = False , readrc = True ):
313313 bdb .Bdb .__init__ (self , skip = skip )
314314 cmd .Cmd .__init__ (self , completekey , stdin , stdout )
315315 sys .audit ("pdb.Pdb" )
@@ -348,9 +348,6 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
348348 except OSError :
349349 pass
350350
351- if commands is not None :
352- self .rcLines .extend (commands )
353-
354351 self .commands = {} # associates a command list to breakpoint numbers
355352 self .commands_doprompt = {} # for each bp num, tells if the prompt
356353 # must be disp. after execing the cmd list
@@ -364,10 +361,14 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
364361 self ._chained_exceptions = tuple ()
365362 self ._chained_exception_index = 0
366363
367- def set_trace (self , frame = None ):
364+ def set_trace (self , frame = None , * , commands = None ):
368365 Pdb ._last_pdb_instance = self
369366 if frame is None :
370367 frame = sys ._getframe ().f_back
368+
369+ if commands is not None :
370+ self .rcLines .extend (commands )
371+
371372 super ().set_trace (frame )
372373
373374 def sigint_handler (self , signum , frame ):
@@ -2353,21 +2354,22 @@ def runcall(*args, **kwds):
23532354 """
23542355 return Pdb ().runcall (* args , ** kwds )
23552356
2356- def set_trace (* , header = None ):
2357+ def set_trace (* , header = None , commands = None ):
23572358 """Enter the debugger at the calling stack frame.
23582359
23592360 This is useful to hard-code a breakpoint at a given point in a
23602361 program, even if the code is not otherwise being debugged (e.g. when
23612362 an assertion fails). If given, *header* is printed to the console
2362- just before debugging begins.
2363+ just before debugging begins. *commands* is an optional list of
2364+ pdb commands to run when the debugger starts.
23632365 """
23642366 if Pdb ._last_pdb_instance is not None :
23652367 pdb = Pdb ._last_pdb_instance
23662368 else :
23672369 pdb = Pdb ()
23682370 if header is not None :
23692371 pdb .message (header )
2370- pdb .set_trace (sys ._getframe ().f_back )
2372+ pdb .set_trace (sys ._getframe ().f_back , commands = commands )
23712373
23722374# Post-Mortem interface
23732375
0 commit comments