Commit | Line | Data |
---|---|---|
0235b0db MJ |
1 | # SPDX-License-Identifier: BSD-2-Clause |
2 | # | |
3 | # Copyright (c) 2016, Matt Layman | |
4 | ||
b85894a3 MJ |
5 | import traceback |
6 | ||
7 | ||
8 | def format_exception(exception): | |
9 | """Format an exception as diagnostics output. | |
10 | ||
11 | exception is the tuple as expected from sys.exc_info. | |
12 | """ | |
13 | exception_lines = traceback.format_exception(*exception) | |
14 | # The lines returned from format_exception do not strictly contain | |
15 | # one line per element in the list (i.e. some elements have new | |
16 | # line characters in the middle). Normalize that oddity. | |
17 | lines = ''.join(exception_lines).splitlines(True) | |
18 | return format_as_diagnostics(lines) | |
19 | ||
20 | ||
21 | def format_as_diagnostics(lines): | |
22 | """Format the lines as diagnostics output by prepending the diagnostic #. | |
23 | ||
24 | This function makes no assumptions about the line endings. | |
25 | """ | |
26 | return ''.join(['# ' + line for line in lines]) |