diff options
author | Guido van Rossum <guido@python.org> | 1995-08-04 04:30:30 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1995-08-04 04:30:30 (GMT) |
commit | 28e99fe96fe6f51ab7bd39a531c0bd5deff1e440 (patch) | |
tree | 8a8b4130576b3797cc8973b9a8261932a5ac71ef /Lib | |
parent | fb1a0cd74f0cd6ddf7c4b6604e6f4e629669446a (diff) | |
download | cpython-28e99fe96fe6f51ab7bd39a531c0bd5deff1e440.zip cpython-28e99fe96fe6f51ab7bd39a531c0bd5deff1e440.tar.gz cpython-28e99fe96fe6f51ab7bd39a531c0bd5deff1e440.tar.bz2 |
added format_* functions (suggestion by Ken M)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/traceback.py | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py index 5ab3ec6b..36e9e69 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -16,12 +16,21 @@ def print_tb(tb, limit = None): co = f.f_code filename = co.co_filename 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) if line: print ' ' + string.strip(line) tb = tb.tb_next 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): if limit is None: if hasattr(sys, 'tracebacklimit'): @@ -42,16 +51,29 @@ def extract_tb(tb, limit = None): n = n+1 return list + def print_exception(etype, value, tb, limit = None): if tb: print 'Traceback (innermost last):' 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: stype = etype.__name__ else: stype = etype if value is None: - print stype + list.append(str(stype) + '\n') else: if etype is SyntaxError: try: @@ -60,22 +82,24 @@ def print_exception(etype, value, tb, limit = None): pass else: if not filename: filename = "<string>" - print ' File "%s", line %d' % \ - (filename, lineno) + list.append(' File "%s", line %d\n' % + (filename, lineno)) i = 0 while i < len(line) and \ line[i] in string.whitespace: i = i+1 + list.append(' %s\n' % string.strip(line)) s = ' ' - print s + string.strip(line) for c in line[i:offset-1]: if c in string.whitespace: s = s + c else: s = s + ' ' - print s + '^' + list.append('%s^\n' % s) value = msg - print '%s: %s' % (stype, value) + list.append('%s: %s\n' % (str(stype), str(value))) + return list + def print_exc(limit = None): print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, |