Skip to content

Commit 00e0422

Browse files
committed
Implement python -m bpdb foo.py for bdpb
We should also install a bpdb script and it to setup.py.
1 parent 0d9d5d2 commit 00e0422

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

bpdb/__init__.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# The MIT License
22
#
33
# Copyright (c) 2008 Bob Farrell
4+
# Copyright (c) 2013 Sebastian Ramacher
45
#
56
# Permission is hereby granted, free of charge, to any person obtaining a copy
67
# of this software and associated documentation files (the "Software"), to deal
@@ -20,11 +21,14 @@
2021
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2122
# THE SOFTWARE.
2223

24+
import os
2325
import sys
26+
import traceback
2427

2528
import bpython
2629
from bpdb.debugger import BPdb
27-
30+
from optparse import OptionParser
31+
from pdb import Restart
2832

2933
__version__ = bpython.__version__
3034

@@ -53,3 +57,52 @@ def post_mortem(t=None):
5357

5458
def pm():
5559
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+

bpdb/__main__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# The MIT License
2+
#
3+
# Copyright (c) 2013 Sebastian Ramacher
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
23+
import sys
24+
25+
if __name__ == '__main__':
26+
from bpdb import main
27+
sys.exit(main())

0 commit comments

Comments
 (0)