diff options
author | Chris Larson <chris_larson@mentor.com> | 2011-05-05 16:53:52 -0700 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2011-06-08 20:38:23 +0100 |
commit | 22522d5f0db604674f48998bd8a057e50a70a17d (patch) | |
tree | eb5417af4476cb93f0790563575fc96566107d95 /bitbake/lib/bb | |
parent | 83708e43c2cd647dfded66338bfa61ea2000d030 (diff) | |
download | poky-22522d5f0db604674f48998bd8a057e50a70a17d.tar.gz |
bb.exceptions: add code to create pickleable traceback entries
This lets you get as much useful information as possible from traceback
entries while staying pickleable.
In addition, it has improved traceback formatting. It shows the values of the
arguments for the functions, lines of context from the file for the code, and
has an optional formatter to do things like syntax highlighting for the code
lines.
(Bitbake rev: ad8ad3fcae29eafbdc09286984495d693a4b73ef)
Signed-off-by: Chris Larson <chris_larson@mentor.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb')
-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 | ||