|
| 1 | +from __future__ import absolute_import, division, print_function |
| 2 | +import types |
| 3 | + |
| 4 | + |
| 5 | +def format_exception_only(etype, value): |
| 6 | + """Format the exception part of a traceback. |
| 7 | +
|
| 8 | + The arguments are the exception type and value such as given by |
| 9 | + sys.last_type and sys.last_value. The return value is a list of |
| 10 | + strings, each ending in a newline. |
| 11 | +
|
| 12 | + Normally, the list contains a single string; however, for |
| 13 | + SyntaxError exceptions, it contains several lines that (when |
| 14 | + printed) display detailed information about where the syntax |
| 15 | + error occurred. |
| 16 | +
|
| 17 | + The message indicating which exception occurred is always the last |
| 18 | + string in the list. |
| 19 | +
|
| 20 | + """ |
| 21 | + |
| 22 | + # An instance should not have a meaningful value parameter, but |
| 23 | + # sometimes does, particularly for string exceptions, such as |
| 24 | + # >>> raise string1, string2 # deprecated |
| 25 | + # |
| 26 | + # Clear these out first because issubtype(string1, SyntaxError) |
| 27 | + # would throw another exception and mask the original problem. |
| 28 | + if (isinstance(etype, BaseException) or |
| 29 | + isinstance(etype, types.InstanceType) or |
| 30 | + etype is None or type(etype) is str): # noqa: E129 |
| 31 | + return [_format_final_exc_line(etype, value)] |
| 32 | + |
| 33 | + stype = etype.__name__ |
| 34 | + |
| 35 | + if not issubclass(etype, SyntaxError): |
| 36 | + return [_format_final_exc_line(stype, value)] |
| 37 | + |
| 38 | + # It was a syntax error; show exactly where the problem was found. |
| 39 | + lines = [] |
| 40 | + try: |
| 41 | + msg, (filename, lineno, offset, badline) = value.args |
| 42 | + except Exception: |
| 43 | + pass |
| 44 | + else: |
| 45 | + filename = filename or "<string>" |
| 46 | + lines.append(' File "%s", line %d\n' % (filename, lineno)) |
| 47 | + if badline is not None: |
| 48 | + lines.append(' %s\n' % badline.strip()) |
| 49 | + if offset is not None: |
| 50 | + caretspace = badline.rstrip('\n')[:offset].lstrip() |
| 51 | + # non-space whitespace (likes tabs) must be kept for alignment |
| 52 | + caretspace = ((c.isspace() and c or ' ') for c in caretspace) |
| 53 | + # only three spaces to account for offset1 == pos 0 |
| 54 | + lines.append(' %s^\n' % ''.join(caretspace)) |
| 55 | + value = msg |
| 56 | + |
| 57 | + lines.append(_format_final_exc_line(stype, value)) |
| 58 | + return lines |
| 59 | + |
| 60 | + |
| 61 | +def _format_final_exc_line(etype, value): |
| 62 | + """Return a list of a single line -- normal case for format_exception_only""" |
| 63 | + valuestr = _some_str(value) |
| 64 | + if value is None or not valuestr: |
| 65 | + line = "%s\n" % etype |
| 66 | + else: |
| 67 | + line = "%s: %s\n" % (etype, valuestr) |
| 68 | + return line |
| 69 | + |
| 70 | + |
| 71 | +def _some_str(value): |
| 72 | + try: |
| 73 | + return str(value) |
| 74 | + except UnicodeError: |
| 75 | + try: |
| 76 | + value = unicode(value) |
| 77 | + return value.encode('utf-8', 'replace') |
| 78 | + except Exception: |
| 79 | + pass |
| 80 | + return '<unprintable %s object>' % type(value).__name__ |
0 commit comments