1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """Compact stack trace functions.
22
23 This module provides some functions to get a stack trace string.
24
25 These stack traces are much more compact than the ones generated by the
26 traceback module.
27 """
28
29 import os
30 import sys
31
33 try:
34 path, filename = os.path.split(n)
35 path, directory = os.path.split(path)
36
37 if directory:
38 return '/'.join((directory, filename))
39 else:
40 return filename
41 except:
42 return '???'
43
45 """Return a compact string representing the current call stack.
46
47 :Parameters:
48 - `f`: Frame object. If not specified, will use the current call
49 position.
50
51 :Return:
52 Returns a string of the current stack trace.
53 """
54 if f is None:
55 try:
56 raise ZeroDivisionError
57 except ZeroDivisionError:
58 f = sys.exc_info()[2].tb_frame.f_back
59 stack = []
60 while f is not None:
61 stack.append(
62 _get_module_name(f.f_code.co_filename) + ' ' +
63 f.f_code.co_name + '|' +
64 str(f.f_lineno)
65 )
66 f = f.f_back
67 return '[' + ('] ['.join(stack))+ ']'
68
70 """Returns a compact string representing the current exception.
71
72 If an exception is not provided as an argument, then it will get the
73 current exception from `sys.exc_info`.
74
75 :Parameters:
76 - `t`: Exception type.
77 - `v`: Exception value.
78 - `tb`: Traceback object.
79
80 :Return:
81 Returns a string of the current exception and stack trace.
82 """
83 if t is None:
84 t,v,tb = sys.exc_info()
85 tbinfo = []
86 if tb is None:
87
88
89 return 'traceback is None!!!'
90 while tb is not None:
91 tbinfo.append (
92 _get_module_name (tb.tb_frame.f_code.co_filename) + ' ' +
93 tb.tb_frame.f_code.co_name + '|' +
94 str(tb.tb_lineno)
95 )
96 tb = tb.tb_next
97
98
99 del tb
100 first = tbinfo[-1]
101 info = '[' + ('] ['.join (tbinfo)) + ']'
102 return repr(((first), str(t), str(v), info))
103