|
1 | 1 | # The MIT License
|
2 | 2 | #
|
3 | 3 | # Copyright (c) 2008 Bob Farrell
|
| 4 | +# Copyright (c) 2013 Sebastian Ramacher |
4 | 5 | #
|
5 | 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 | 7 | # of this software and associated documentation files (the "Software"), to deal
|
|
20 | 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21 | 22 | # THE SOFTWARE.
|
22 | 23 |
|
| 24 | +import os |
23 | 25 | import sys
|
| 26 | +import traceback |
24 | 27 |
|
25 | 28 | import bpython
|
26 | 29 | from bpdb.debugger import BPdb
|
27 |
| - |
| 30 | +from optparse import OptionParser |
| 31 | +from pdb import Restart |
28 | 32 |
|
29 | 33 | __version__ = bpython.__version__
|
30 | 34 |
|
@@ -53,3 +57,52 @@ def post_mortem(t=None):
|
53 | 57 |
|
54 | 58 | def pm():
|
55 | 59 | post_mortem(getattr(sys, "last_traceback", None))
|
| 60 | + |
| 61 | +def main(): |
| 62 | + parser = OptionParser( |
| 63 | + usage='Usage: %prog [options] [file [args]]') |
| 64 | + parser.add_option('--version', '-V', action='store_true', |
| 65 | + help='Print version and exit.') |
| 66 | + options, args = parser.parse_args(sys.argv) |
| 67 | + if options.version: |
| 68 | + print 'bpdb on top of bpython version', __version__, |
| 69 | + print 'on top of Python', sys.version.split()[0] |
| 70 | + print ('(C) 2008-2013 Bob Farrell, Andreas Stuehrk et al. ' |
| 71 | + 'See AUTHORS for detail.') |
| 72 | + return 0 |
| 73 | + |
| 74 | + # The following code is baed on Python's pdb.py. |
| 75 | + mainpyfile = args[1] |
| 76 | + if not os.path.exists(mainpyfile): |
| 77 | + print 'Error:', mainpyfile, 'does not exist' |
| 78 | + return 1 |
| 79 | + |
| 80 | + # Hide bpdb from argument list. |
| 81 | + del sys.argv[0] |
| 82 | + |
| 83 | + # Replace bpdb's dir with script's dir in front of module search path. |
| 84 | + sys.path[0] = os.path.dirname(mainpyfile) |
| 85 | + |
| 86 | + pdb = BPdb() |
| 87 | + while True: |
| 88 | + try: |
| 89 | + pdb._runscript(mainpyfile) |
| 90 | + if pdb._user_requested_quit: |
| 91 | + break |
| 92 | + print "The program finished and will be restarted" |
| 93 | + except Restart: |
| 94 | + print "Restarting", mainpyfile, "with arguments:" |
| 95 | + print "\t" + " ".join(sys.argv[1:]) |
| 96 | + except SystemExit: |
| 97 | + # In most cases SystemExit does not warrant a post-mortem session. |
| 98 | + print "The program exited via sys.exit(). Exit status: ", |
| 99 | + print sys.exc_info()[1] |
| 100 | + except: |
| 101 | + traceback.print_exc() |
| 102 | + print "Uncaught exception. Entering post mortem debugging" |
| 103 | + print "Running 'cont' or 'step' will restart the program" |
| 104 | + t = sys.exc_info()[2] |
| 105 | + pdb.interaction(None, t) |
| 106 | + print "Post mortem debugger finished. The " + mainpyfile + \ |
| 107 | + " will be restarted" |
| 108 | + |
0 commit comments