-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys_settrace_exception.py
More file actions
42 lines (34 loc) · 880 Bytes
/
Copy pathsys_settrace_exception.py
File metadata and controls
42 lines (34 loc) · 880 Bytes
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
#!/usr/bin/env python
# encoding: utf-8
import sys
def trace_exceptions(frame, event, arg):
if event != 'exception':
return
co = frame.f_code
func_name = co.co_name
line_no = frame.f_lineno
filename = co.co_filename
exc_type, exc_value, exc_traceback = arg
print 'Tracing exception:\n%s "%s"\non line %s of %s\n' % \
(exc_type.__name__, exc_value, line_no, func_name)
def trace_calls(frame, event, arg):
if event != 'call':
return
co = frame.f_code
func_name = co.co_name
if func_name in TRACE_INTO:
return trace_exceptions
def c():
raise RuntimeError('generating exception in c()')
def b():
c()
print 'Leaving b()'
def a():
b()
print 'Leaving a()'
TRACE_INTO = ['a', 'b', 'c']
sys.settrace(trace_calls)
try:
a()
except Exception, e:
print 'Exception handler:', e