Project

General

Profile

« Previous | Next » 

Revision 1368

exc.py: str_(): Add the traceback at the end of the exception string. Added add_exc_info() and get_exc_info() for providing traceback info for str_().

View differences:

lib/exc.py
2 2

  
3 3
import atexit
4 4
import sys
5
import traceback
5 6

  
6 7
import format
7 8
import strings
......
10 11

  
11 12
def raise_(e): raise e
12 13

  
14
def add_exc_info(e):
15
    '''Note that this will create a circular reference in the traceback
16
    (See <http://docs.python.org/library/sys.html#sys.exc_info>)'''
17
    e.exc_info = sys.exc_info()
18

  
19
def get_exc_info(e):
20
    try: exc_info = e.exc_info
21
    except AttributeError: exc_info = sys.exc_info()
22
    assert exc_info[1] is e
23
    return exc_info
24

  
13 25
def add_msg(e, msg): e.args = (strings.ensure_newl(str(e))+msg,)
14 26

  
15 27
def repl_msg(e, **repls): e.args = (str(e) % repls,)
......
21 33
        Exception.__init__(self, msg)
22 34
        if cause != None: add_msg(self, 'cause: '+str_(cause))
23 35

  
24
def str_(e, with_name=True):
25
    msg = strings.ensure_newl(str(e))
26
    if with_name: msg = util.type_name(e)+': '+msg
27
    return msg
36
def str_(e, with_traceback=True):
37
    lines = traceback.format_exception_only(type(e), e)
38
    if with_traceback: lines += traceback.format_tb(get_exc_info(e)[2])
39
    return ''.join(lines)
28 40

  
29 41
def print_ex(e, emph=True, **format):
30 42
    msg = str_(e, **format)

Also available in: Unified diff