-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbug.py
120 lines (105 loc) · 4.21 KB
/
bug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""PyBug - python debugging utilities/extension of pdb
The intent of this code is that it would be either included directly or used as
an inspiration for features of the pdb module in the Python Standard Library.
Copyright (c) 2012 Daniel Miller
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import os
import sys
try:
from ipdb import sset_trace
from ipdb.__main__ import _init_pdb
have_ipdb = True
except ImportError:
from pdb import Pdb
have_ipdb = False
class Error(Exception): pass
def trace(context=3):
"""like pdb.set_trace() except sets a breakpoint for the next line
works with nose testing framework (uses sys.__stdout__ for output)
"""
frame = sys._getframe().f_back
if have_ipdb:
sset_trace(frame=frame, context=context)
else:
Pdb(stdout=sys.__stdout__).set_trace(frame)
def setbreak(line=None, file=None, cond=None, temp=0, frame=None, throw=False):
"""set a breakpoint or a given line in file with conditional
arguments:
line - line number on which to break
file - module or filename where the breakpoint should be set
cond - string with conditional expression, which (if given) must
evaluate to true to break
temp - if true, create a temporary breakpoint
example usage:
setbreak(42, "/path/to/universe.py", "name == 'hitchhiker'")
setbreak(35, "package.module")
see: http://groups.google.com/group/comp.lang.python/browse_thread/thread/103326200285cb07#
"""
if frame is None:
frame = sys._getframe().f_back
if file is None:
file = frame.f_code.co_filename
elif not file.startswith("file:") and os.path.sep not in file:
try:
mod = __import__(file, globals(), locals(), ["__file__"])
except ImportError as err:
if throw:
raise
sys.__stdout__.write("cannot set breakpoint: %s:%s : %s" %
(file, line, err))
return
file = mod.__file__
sys.__stdout__.write("breaking in: %s" % file)
if file.endswith(".pyc"):
file = file[:-1]
if have_ipdb:
pdb = _init_pdb(context=3)
else:
pdb = Pdb(stdout=sys.__stdout__) # use sys.__stdout__ to work with nose tests
pdb.reset()
pdb.curframe = frame
while frame:
frame.f_trace = pdb.trace_dispatch
pdb.botframe = frame
frame = frame.f_back
templine = line
while templine < line + 10:
error = pdb.set_break(file, templine, cond=cond, temporary=temp)
if error:
templine += 1
else:
break
if error:
error = pdb.set_break(file, line, cond=cond, temporary=temp)
if throw:
raise Error(error)
sys.__stdout__.write("\n%s\n" % error)
return
sys.__stdout__.write("\n")
pdb.do_break("") # print breakpoints
pdb.set_continue()
def trace_dispatch(*args, **kw):
try:
return pdb.trace_dispatch(*args, **kw)
except AttributeError:
# silence error on interpreter shutdown
# AttributeError: 'NoneType' object has no attribute 'abspath'
pass
sys.settrace(trace_dispatch)
if __name__ == "__main__":
trace()
print("testing bug.py")