@@ -332,19 +332,26 @@ def setup(self, f, t):
332
332
# locals whenever the .f_locals accessor is called, so we
333
333
# cache it here to ensure that modifications are not overwritten.
334
334
self .curframe_locals = self .curframe .f_locals
335
- self .execRcLines ()
335
+ return self .execRcLines ()
336
336
337
337
# Can be executed earlier than 'setup' if desired
338
338
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
348
355
349
356
# Override Bdb methods
350
357
@@ -367,7 +374,7 @@ def user_line(self, frame):
367
374
if self .bp_commands (frame ):
368
375
self .interaction (frame , None )
369
376
370
- def bp_commands (self ,frame ):
377
+ def bp_commands (self , frame ):
371
378
"""Call every command that was set for the current active breakpoint
372
379
(if there is one).
373
380
@@ -409,7 +416,11 @@ def user_exception(self, frame, exc_info):
409
416
# General interaction function
410
417
411
418
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
413
424
self .print_stack_entry (self .stack [self .curindex ])
414
425
self .cmdloop ()
415
426
self .forget ()
@@ -1497,17 +1508,42 @@ def help():
1497
1508
import pydoc
1498
1509
pydoc .pager (__doc__ )
1499
1510
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
+
1500
1524
def 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 )
1503
1531
sys .exit (2 )
1504
1532
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
1506
1542
if not os .path .exists (mainpyfile ):
1507
1543
print ('Error:' , mainpyfile , 'does not exist' )
1508
1544
sys .exit (1 )
1509
1545
1510
- del sys .argv [0 ] # Hide "pdb.py" from argument list
1546
+ sys .argv [:] = args # Hide "pdb.py" and pdb options from argument list
1511
1547
1512
1548
# Replace pdb's dir with script's dir in front of module search path.
1513
1549
sys .path [0 ] = os .path .dirname (mainpyfile )
@@ -1517,6 +1553,7 @@ def main():
1517
1553
# changed by the user from the command line. There is a "restart" command
1518
1554
# which allows explicit specification of command line arguments.
1519
1555
pdb = Pdb ()
1556
+ pdb .rcLines .extend (commands )
1520
1557
while True :
1521
1558
try :
1522
1559
pdb ._runscript (mainpyfile )
@@ -1525,10 +1562,10 @@ def main():
1525
1562
print ("The program finished and will be restarted" )
1526
1563
except Restart :
1527
1564
print ("Restarting" , mainpyfile , "with arguments:" )
1528
- print ("\t " + " " .join (sys . argv [ 1 :] ))
1565
+ print ("\t " + " " .join (args ))
1529
1566
except SystemExit :
1530
1567
# 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 = ' ' )
1532
1569
print (sys .exc_info ()[1 ])
1533
1570
except :
1534
1571
traceback .print_exc ()
0 commit comments