@@ -332,19 +332,26 @@ def setup(self, f, t):
332332 # locals whenever the .f_locals accessor is called, so we
333333 # cache it here to ensure that modifications are not overwritten.
334334 self .curframe_locals = self .curframe .f_locals
335- self .execRcLines ()
335+ return self .execRcLines ()
336336
337337 # Can be executed earlier than 'setup' if desired
338338 def execRcLines (self ):
339- if self .rcLines :
340- # Make local copy because of recursion
341- rcLines = self .rcLines
342- # executed only once
343- self .rcLines = []
344- for line in rcLines :
345- line = line [:- 1 ]
346- if len (line ) > 0 and line [0 ] != '#' :
347- self .onecmd (line )
339+ if not self .rcLines :
340+ return
341+ # local copy because of recursion
342+ rcLines = self .rcLines
343+ rcLines .reverse ()
344+ # execute every line only once
345+ self .rcLines = []
346+ while rcLines :
347+ line = rcLines .pop ().strip ()
348+ if line and line [0 ] != '#' :
349+ if self .onecmd (line ):
350+ # if onecmd returns True, the command wants to exit
351+ # from the interaction, save leftover rc lines
352+ # to execute before next interaction
353+ self .rcLines += reversed (rcLines )
354+ return True
348355
349356 # Override Bdb methods
350357
@@ -367,7 +374,7 @@ def user_line(self, frame):
367374 if self .bp_commands (frame ):
368375 self .interaction (frame , None )
369376
370- def bp_commands (self ,frame ):
377+ def bp_commands (self , frame ):
371378 """Call every command that was set for the current active breakpoint
372379 (if there is one).
373380
@@ -409,7 +416,11 @@ def user_exception(self, frame, exc_info):
409416 # General interaction function
410417
411418 def interaction (self , frame , traceback ):
412- self .setup (frame , traceback )
419+ if self .setup (frame , traceback ):
420+ # no interaction desired at this time (happens if .pdbrc contains
421+ # a command like "continue")
422+ self .forget ()
423+ return
413424 self .print_stack_entry (self .stack [self .curindex ])
414425 self .cmdloop ()
415426 self .forget ()
@@ -1497,17 +1508,42 @@ def help():
14971508 import pydoc
14981509 pydoc .pager (__doc__ )
14991510
1511+ _usage = """\
1512+ usage: pdb.py [-c command] ... pyfile [arg] ...
1513+
1514+ Debug the Python program given by pyfile.
1515+
1516+ Initial commands are read from .pdbrc files in your home directory
1517+ and in the current directory, if they exist. Commands supplied with
1518+ -c are executed after commands from .pdbrc files.
1519+
1520+ To let the script run until an exception occurs, use "-c continue".
1521+ To let the script run until a given line X in the debugged file, use
1522+ "-c 'break X' -c continue"."""
1523+
15001524def main ():
1501- if not sys .argv [1 :] or sys .argv [1 ] in ("--help" , "-h" ):
1502- print ("usage: pdb.py scriptfile [arg] ..." )
1525+ import getopt
1526+
1527+ opts , args = getopt .getopt (sys .argv [1 :], 'hc:' , ['--help' , '--command=' ])
1528+
1529+ if not args :
1530+ print (_usage )
15031531 sys .exit (2 )
15041532
1505- mainpyfile = sys .argv [1 ] # Get script filename
1533+ commands = []
1534+ for opt , optarg in opts :
1535+ if opt in ['-h' , '--help' ]:
1536+ print (_usage )
1537+ sys .exit ()
1538+ elif opt in ['-c' , '--command' ]:
1539+ commands .append (optarg )
1540+
1541+ mainpyfile = args [0 ] # Get script filename
15061542 if not os .path .exists (mainpyfile ):
15071543 print ('Error:' , mainpyfile , 'does not exist' )
15081544 sys .exit (1 )
15091545
1510- del sys .argv [0 ] # Hide "pdb.py" from argument list
1546+ sys .argv [:] = args # Hide "pdb.py" and pdb options from argument list
15111547
15121548 # Replace pdb's dir with script's dir in front of module search path.
15131549 sys .path [0 ] = os .path .dirname (mainpyfile )
@@ -1517,6 +1553,7 @@ def main():
15171553 # changed by the user from the command line. There is a "restart" command
15181554 # which allows explicit specification of command line arguments.
15191555 pdb = Pdb ()
1556+ pdb .rcLines .extend (commands )
15201557 while True :
15211558 try :
15221559 pdb ._runscript (mainpyfile )
@@ -1525,10 +1562,10 @@ def main():
15251562 print ("The program finished and will be restarted" )
15261563 except Restart :
15271564 print ("Restarting" , mainpyfile , "with arguments:" )
1528- print ("\t " + " " .join (sys . argv [ 1 :] ))
1565+ print ("\t " + " " .join (args ))
15291566 except SystemExit :
15301567 # In most cases SystemExit does not warrant a post-mortem session.
1531- print ("The program exited via sys.exit(). Exit status: " , end = ' ' )
1568+ print ("The program exited via sys.exit(). Exit status:" , end = ' ' )
15321569 print (sys .exc_info ()[1 ])
15331570 except :
15341571 traceback .print_exc ()
0 commit comments