1
# copied from python-2.7.3's traceback.py
3
# - some_str is replaced, trying to create unicode strings
7
def format_exception_only(etype, value):
8
"""Format the exception part of a traceback.
10
The arguments are the exception type and value such as given by
11
sys.last_type and sys.last_value. The return value is a list of
12
strings, each ending in a newline.
14
Normally, the list contains a single string; however, for
15
SyntaxError exceptions, it contains several lines that (when
16
printed) display detailed information about where the syntax
19
The message indicating which exception occurred is always the last
24
# An instance should not have a meaningful value parameter, but
25
# sometimes does, particularly for string exceptions, such as
26
# >>> raise string1, string2 # deprecated
28
# Clear these out first because issubtype(string1, SyntaxError)
29
# would throw another exception and mask the original problem.
30
if (isinstance(etype, BaseException) or
31
isinstance(etype, types.InstanceType) or
32
etype is None or type(etype) is str):
33
return [_format_final_exc_line(etype, value)]
35
stype = etype.__name__
37
if not issubclass(etype, SyntaxError):
38
return [_format_final_exc_line(stype, value)]
40
# It was a syntax error; show exactly where the problem was found.
43
msg, (filename, lineno, offset, badline) = value.args
47
filename = filename or "<string>"
48
lines.append(' File "%s", line %d\n' % (filename, lineno))
49
if badline is not None:
50
lines.append(' %s\n' % badline.strip())
51
if offset is not None:
52
caretspace = badline.rstrip('\n')[:offset].lstrip()
53
# non-space whitespace (likes tabs) must be kept for alignment
54
caretspace = ((c.isspace() and c or ' ') for c in caretspace)
55
# only three spaces to account for offset1 == pos 0
56
lines.append(' %s^\n' % ''.join(caretspace))
59
lines.append(_format_final_exc_line(stype, value))
62
def _format_final_exc_line(etype, value):
63
"""Return a list of a single line -- normal case for format_exception_only"""
64
valuestr = _some_str(value)
65
if value is None or not valuestr:
68
line = "%s: %s\n" % (etype, valuestr)
79
return '<unprintable %s object>' % type(value).__name__