10000 Added routines to print, format and extract the current, ``live'' stack. · python/cpython@dcc057a · GitHub
[go: up one dir, main page]

Skip to content

Commit dcc057a

Browse files
committed
Added routines to print, format and extract the current, ``live'' stack.
Also added print_list() and format_list() which format the output from an extract_*() routine.
1 parent 0dfcf75 commit dcc057a

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

Lib/traceback.py

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77

88
def _print(file, str='', terminator='\n'):
99
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
1029

1130

1231
def print_tb(tb, limit=None, file=None):
@@ -30,13 +49,7 @@ def print_tb(tb, limit=None, file=None):
3049
n = n+1
3150

3251
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))
4053

4154
def extract_tb(tb, limit = None):
4255
if limit is None:
@@ -123,3 +136,48 @@ def print_last(limit=None, file=None):
123136
file = sys.stderr
124137
print_exception(sys.last_type, sys.last_value, sys.last_traceback,
125138
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

Comments
 (0)
0