diff options
Diffstat (limited to 'bitbake')
-rw-r--r-- | bitbake/lib/bb/exceptions.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/bitbake/lib/bb/exceptions.py b/bitbake/lib/bb/exceptions.py new file mode 100644 index 0000000000..13269de8c1 --- /dev/null +++ b/bitbake/lib/bb/exceptions.py | |||
@@ -0,0 +1,55 @@ | |||
1 | from __future__ import absolute_import | ||
2 | import inspect | ||
3 | import traceback | ||
4 | import bb.namedtuple_with_abc | ||
5 | from collections import namedtuple | ||
6 | |||
7 | |||
8 | class TracebackEntry(namedtuple.abc): | ||
9 | """Pickleable representation of a traceback entry""" | ||
10 | _fields = 'filename lineno function args code_context index' | ||
11 | _header = ' File "{0.filename}", line {0.lineno}, in {0.function}{0.args}:\n' | ||
12 | |||
13 | def format(self, formatter=None): | ||
14 | formatted = [self._header.format(self)] | ||
15 | |||
16 | for lineindex, line in enumerate(self.code_context): | ||
17 | if formatter: | ||
18 | line = formatter(line) | ||
19 | |||
20 | if lineindex == self.index: | ||
21 | formatted.append(' >%s' % line) | ||
22 | else: | ||
23 | formatted.append(' %s' % line) | ||
24 | return formatted | ||
25 | |||
26 | def __str__(self): | ||
27 | return ''.join(self.format()) | ||
28 | |||
29 | |||
30 | def extract_traceback(tb, context=1): | ||
31 | frames = inspect.getinnerframes(tb, context) | ||
32 | for frame, filename, lineno, function, code_context, index in frames: | ||
33 | args = inspect.formatargvalues(*inspect.getargvalues(frame)) | ||
34 | yield TracebackEntry(filename, lineno, function, args, code_context, index) | ||
35 | |||
36 | |||
37 | def format_extracted(extracted, formatter=None, limit=None): | ||
38 | if limit: | ||
39 | extracted = extracted[-limit:] | ||
40 | |||
41 | formatted = [] | ||
42 | for tracebackinfo in extracted: | ||
43 | formatted.extend(tracebackinfo.format(formatter)) | ||
44 | return formatted | ||
45 | |||
46 | |||
47 | def format_exception(etype, value, tb, context=1, limit=None, formatter=None): | ||
48 | formatted = ['Traceback (most recent call last):\n'] | ||
49 | |||
50 | if hasattr(tb, 'tb_next'): | ||
51 | tb = extract_traceback(tb, context) | ||
52 | |||
53 | formatted.extend(format_extracted(tb, formatter, limit)) | ||
54 | formatted.extend(traceback.format_exception_only(etype, value)) | ||
55 | return formatted | ||