added format_* functions (suggestion by Ken M)

This commit is contained in:
Guido van Rossum 1995-08-04 04:30:30 +00:00
parent fb1a0cd74f
commit 28e99fe96f
1 changed files with 31 additions and 7 deletions

View File

@ -16,12 +16,21 @@ def print_tb(tb, limit = None):
co = f.f_code co = f.f_code
filename = co.co_filename filename = co.co_filename
name = co.co_name name = co.co_name
print ' File "%s", line %d, in %s' % (filename, lineno, name) print ' File "%s", line %d, in %s' % (filename,lineno,name)
line = linecache.getline(filename, lineno) line = linecache.getline(filename, lineno)
if line: print ' ' + string.strip(line) if line: print ' ' + string.strip(line)
tb = tb.tb_next tb = tb.tb_next
n = n+1 n = n+1
def format_tb(tb, limit = None):
list = []
for filename, lineno, name, line in extract_tb(tb, limit):
item = ' File "%s", line %d, in %s\n' % (filename,lineno,name)
if line:
item = item + ' %s\n' % string.strip(line)
list.append(item)
return list
def extract_tb(tb, limit = None): def extract_tb(tb, limit = None):
if limit is None: if limit is None:
if hasattr(sys, 'tracebacklimit'): if hasattr(sys, 'tracebacklimit'):
@ -42,16 +51,29 @@ def extract_tb(tb, limit = None):
n = n+1 n = n+1
return list return list
def print_exception(etype, value, tb, limit = None): def print_exception(etype, value, tb, limit = None):
if tb: if tb:
print 'Traceback (innermost last):' print 'Traceback (innermost last):'
print_tb(tb, limit) print_tb(tb, limit)
for line in format_exception_only(etype, value):
print line,
def format_exception(etype, value, tb, limit = None):
if tb:
list = ['Traceback (innermost last):\n']
list = list + format_tb(tb, limit)
list = list + format_exception_only(etype, value)
return list
def format_exception_only(etype, value):
list = []
if type(etype) == types.ClassType: if type(etype) == types.ClassType:
stype = etype.__name__ stype = etype.__name__
else: else:
stype = etype stype = etype
if value is None: if value is None:
print stype list.append(str(stype) + '\n')
else: else:
if etype is SyntaxError: if etype is SyntaxError:
try: try:
@ -60,22 +82,24 @@ def print_exception(etype, value, tb, limit = None):
pass pass
else: else:
if not filename: filename = "<string>" if not filename: filename = "<string>"
print ' File "%s", line %d' % \ list.append(' File "%s", line %d\n' %
(filename, lineno) (filename, lineno))
i = 0 i = 0
while i < len(line) and \ while i < len(line) and \
line[i] in string.whitespace: line[i] in string.whitespace:
i = i+1 i = i+1
list.append(' %s\n' % string.strip(line))
s = ' ' s = ' '
print s + string.strip(line)
for c in line[i:offset-1]: for c in line[i:offset-1]:
if c in string.whitespace: if c in string.whitespace:
s = s + c s = s + c
else: else:
s = s + ' ' s = s + ' '
print s + '^' list.append('%s^\n' % s)
value = msg value = msg
print '%s: %s' % (stype, value) list.append('%s: %s\n' % (str(stype), str(value)))
return list
def print_exc(limit = None): def print_exc(limit = None):
print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback,