7
7
8
8
def _print (file , str = '' , terminator = '\n ' ):
9
9
file .write (str + terminator )
10
+
11
+
12
+ def print_list (extracted_list , file = None ):
13
+ if not file :
14
+ file = sys .stderr
15
+ for filename , lineno , name , line in extracted_list :
16
+ _print (file ,
17
+ ' File "%s", line %d, in %s' % (filename ,lineno ,name ))
18
+ if line :
19
+ _print (file , ' %s' % string .strip (line ))
20
+
21
+ def format_list (extracted_list ):
22
+ list = []
23
+ for filename , lineno , name , line in extracted_list :
24
+ item = ' File "%s", line %d, in %s\n ' % (filename ,lineno ,name )
25
+ if line :
26
+ item = item + ' %s\n ' % string .strip (line )
27
+ list .append (item )
28
+ return list
10
29
11
30
12
31
def print_tb (tb , limit = None , file = None ):
@@ -30,13 +49,7 @@ def print_tb(tb, limit=None, file=None):
30
49
n = n + 1
31
50
32
51
def format_tb (tb , limit = None ):
33
- list = []
34
- for filename , lineno , name , line in extract_tb (tb , limit ):
35
- item = ' File "%s", line %d, in %s\n ' % (filename ,lineno ,name )
36
- if line :
37
- item = item + ' %s\n ' % string .strip (line )
38
- list .append (item )
39
- return list
52
+ return format_list (extract_tb (tb , limit ))
40
53
41
54
def extract_tb (tb , limit = None ):
42
55
if limit is None :
@@ -123,3 +136,48 @@ def print_last(limit=None, file=None):
123
136
file = sys .stderr
124
137
print_exception (sys .last_type , sys .last_value , sys .last_traceback ,
125
138
limit , file )
139
+
140
+
141
+ def print_stack (f = None , limit = None , file = None ):
142
+ if f is None :
143
+ try :
144
+ raise ZeroDivisionError
145
+ except ZeroDivisionError :
146
+ tb = sys .exc_traceback
147
+ f = tb .tb_frame .f_back
148
+ print_list (extract_stack (f , limit ), file )
149
+
150
+ def format_stack (f = None , limit = None ):
151
+ if f is None :
152
+ try :
153
+ raise ZeroDivisionError
154
+ except ZeroDivisionError :
155
+ tb = sys .exc_traceback
156
+ f = tb .tb_frame .f_back
157
+ return format_list (extract_stack (t , limit ))
158
+
159
+ def extract_stack (f = None , limit = None ):
160
+ if f is None :
161
+ try :
162
+ raise ZeroDivisionError
163
+ except ZeroDivisionError :
164
+ tb = sys .exc_traceback
165
+ f = tb .tb_frame .f_back
166
+ if limit is None :
167
+ if hasattr (sys , 'tracebacklimit' ):
168
+ limit = sys .tracebacklimit
169
+ list = []
170
+ n = 0
171
+ while f is not None a
8ACA
nd (limit is None or n < limit ):
172
+ lineno = f .f_lineno
173
+ co = f .f_code
174
+ filename = co .co_filename
175
+ name = co .co_name
176
+ line = linecache .getline (filename , lineno )
177
+ if line : line = string .strip (line )
178
+ else : line = None
179
+ list .append (filename , lineno , name , line )
180
+ f = f .f_back
181
+ n = n + 1
182
+ list .reverse ()
183
+ return list
0 commit comments