forked from ionelmc/python-hunter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cookbook.py
More file actions
219 lines (174 loc) · 6.34 KB
/
test_cookbook.py
File metadata and controls
219 lines (174 loc) · 6.34 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import collections
import contextlib
import functools
import opcode
import os
import sys
from logging import getLogger
import aspectlib
import pytest
import hunter
from hunter import CallPrinter
from hunter import CodePrinter
from hunter import VarsSnooper
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
logger = getLogger(__name__)
pytest_plugins = 'pytester',
def nothin(x):
return x
def bar():
baz()
@nothin
@nothin
@nothin
@nothin
def baz():
for i in range(10):
os.path.join('a', str(i))
foo = 1
def brief_probe(qualname, *actions, **kwargs):
return aspectlib.weave(qualname, functools.partial(hunter.wrap, actions=actions, **kwargs))
def fast_probe(qualname, *actions, **filters):
def tracing_decorator(func):
@functools.wraps(func)
def tracing_wrapper(*args, **kwargs):
# create the Tracer manually to avoid spending time in likely useless things like:
# - loading PYTHONHUNTERCONFIG
# - setting up the clear_env_var or thread_support options
# - atexit cleanup registration
with hunter.Tracer().trace(hunter.When(hunter.Query(**filters), *actions)):
return func(*args, **kwargs)
return tracing_wrapper
return aspectlib.weave(qualname, tracing_decorator) # this does the monkeypatch
@contextlib.contextmanager
def no_probe(*args, **kwargs):
yield
@pytest.mark.parametrize('impl', [fast_probe, brief_probe, no_probe])
def test_probe(impl, benchmark):
with impl('%s.baz' % __name__, hunter.VarsPrinter('foo', stream=open(os.devnull, 'w')), kind="return", depth=0):
benchmark(bar)
def error():
raise RuntimeError()
def silenced1():
try:
error()
except Exception:
pass
def silenced2():
try:
error()
except Exception as exc:
print(exc)
for i in range(25):
print(i)
return 'x'
def silenced3():
try:
error()
finally:
return "mwhahaha"
def silenced4():
try:
error()
except Exception as exc:
logger.info(repr(exc))
baz()
def notsilenced():
try:
error()
except Exception as exc:
raise ValueError(exc)
RETURN_VALUE = opcode.opmap['RETURN_VALUE']
class DumpExceptions(hunter.CodePrinter):
events = ()
depth = 0
count = 0
exc = None
def __init__(self, max_count=10, **kwargs):
self.max_count = max_count
self.backlog = collections.deque(maxlen=5)
super(DumpExceptions, self).__init__(**kwargs)
def __call__(self, event):
self.count += 1
if event.kind == 'exception': # something interesting happened ;)
self.events = list(self.backlog)
self.events.append(event.detach(self.try_repr))
self.exc = self.try_repr(event.arg[1])
self.depth = event.depth
self.count = 0
elif self.events:
if event.depth > self.depth: # too many details
return
elif event.depth < self.depth and event.kind == 'return': # stop if function returned
op = event.code.co_code[event.frame.f_lasti]
op = op if isinstance(op, int) else ord(op)
if op == RETURN_VALUE:
self.output("{BRIGHT}{fore(BLUE)}{} tracing {} on {}{RESET}\n",
">" * 46, event.function, self.exc)
for event in self.events:
super(DumpExceptions, self).__call__(event)
if self.count > 10:
self.output("{BRIGHT}{fore(BLACK)}{} too many lines{RESET}\n",
"-" * 46)
else:
self.output("{BRIGHT}{fore(BLACK)}{} function exit{RESET}\n",
"-" * 46)
self.events = []
self.exc = None
elif self.count < self.max_count:
self.events.append(event.detach(self.try_repr))
else:
self.backlog.append(event.detach(self.try_repr))
def test_dump_exceptions(LineMatcher):
stream = StringIO()
with hunter.trace(stdlib=False, action=DumpExceptions(stream=stream)):
silenced1()
silenced2()
silenced3()
silenced4()
print("Done silenced")
try:
notsilenced()
print("Done not silenced")
except ValueError:
pass
lm = LineMatcher(stream.getvalue().splitlines())
lm.fnmatch_lines([
'*>>>>>>>>>>>>>>>>>>>>>> tracing silenced1 on RuntimeError()',
'*test_cookbook.py:*** exception error()',
'* *** ... exception value: *RuntimeError*',
'*test_cookbook.py:*** line except Exception:',
'*test_cookbook.py:*** line pass',
'*---------------------- function exit',
'*>>>>>>>>>>>>>>>>>>>>>> tracing silenced2 on RuntimeError()',
'*test_cookbook.py:*** exception error()',
'* ... exception value: *RuntimeError*',
'*test_cookbook.py:*** line except Exception as exc:',
'*test_cookbook.py:*** line print(exc)',
'*---------------------- too many lines',
'*>>>>>>>>>>>>>>>>>>>>>> tracing silenced3 on RuntimeError()',
'*test_cookbook.py:*** exception error()',
'* ... exception value: *RuntimeError*',
'*test_cookbook.py:*** line return "mwhahaha"',
'*---------------------- function exit',
'*>>>>>>>>>>>>>>>>>>>>>> tracing silenced4 on RuntimeError()',
'*test_cookbook.py:*** exception error()',
'* ... exception value: *RuntimeError*',
'*test_cookbook.py:*** line except Exception as exc:',
'*test_cookbook.py:*** line logger.info(repr(exc))',
'*---------------------- too many lines',
])
def test_examples():
print("""
CodePrinter
""")
with hunter.trace(stdlib=False, actions=[CodePrinter, VarsSnooper]):
os.path.join(*map(str, range(10)))
print("""
CallPrinter
""")
with hunter.trace(stdlib=False, actions=[CallPrinter, VarsSnooper]):
os.path.join(*map(str, range(10)))