Debian Codegraph @

neutron_lib/tests/_post_mortem_debug.py

raw
1 # Copyright 2013 Red Hat, Inc.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import functools
17 import traceback
18
19
20 def get_exception_handler(debugger_name):
21 debugger = _get_debugger(debugger_name)
22 return functools.partial(_exception_handler, debugger)
23
24
25 def _get_debugger(debugger_name):
26 try:
27 debugger = __import__(debugger_name)
28 except ImportError as exc:
29 raise ValueError(
30 f"can't import {debugger_name} module as a "
31 "post mortem debugger") from exc
32 if 'post_mortem' in dir(debugger):
33 return debugger
34 else:
35 raise ValueError(
36 f"{debugger_name} is not a supported post mortem debugger")
37
38
39 def _exception_handler(debugger, exc_info):
40 """Exception handler enabling post-mortem debugging.
41
42 A class extending testtools.TestCase can add this handler in setUp():
43
44 self.addOnException(post_mortem_debug.exception_handler)
45
46 When an exception occurs, the user will be dropped into a debugger
47 session in the execution environment of the failure.
48
49 Frames associated with the testing framework are excluded so that
50 the post-mortem session for an assertion failure will start at the
51 assertion call (e.g. self.assertTrue) rather than the framework code
52 that raises the failure exception (e.g. the assertTrue method).
53 """
54 tb = exc_info[2]
55 ignored_traceback = get_ignored_traceback(tb)
56 if ignored_traceback:
57 tb = FilteredTraceback(tb, ignored_traceback)
58 traceback.print_exception(exc_info[0], exc_info[1], tb)
59 debugger.post_mortem(tb)
60
61
62 def get_ignored_traceback(tb):
63 """Retrieve the first traceback of an ignored trailing chain.
64
65 Given an initial traceback, find the first traceback of a trailing
66 chain of tracebacks that should be ignored. The criteria for
67 whether a traceback should be ignored is whether its frame's
68 globals include the __unittest marker variable. This criteria is
69 culled from:
70
71 unittest.TestResult._is_relevant_tb_level
72
73 For example:
74
75 tb.tb_next => tb0.tb_next => tb1.tb_next
76
77 - If no tracebacks were to be ignored, None would be returned.
78 - If only tb1 was to be ignored, tb1 would be returned.
79 - If tb0 and tb1 were to be ignored, tb0 would be returned.
80 - If either of only tb or only tb0 was to be ignored, None would
81 be returned because neither tb or tb0 would be part of a
82 trailing chain of ignored tracebacks.
83 """
84 # Turn the traceback chain into a list
85 tb_list = []
86 while tb:
87 tb_list.append(tb)
88 tb = tb.tb_next
89
90 # Find all members of an ignored trailing chain
91 ignored_tracebacks = []
92 for memb in reversed(tb_list):
93 if '__unittest' in tb.tb_frame.f_globals:
94 ignored_tracebacks.append(memb)
95 else:
96 break
97
98 # Return the first member of the ignored trailing chain
99 if ignored_tracebacks:
100 return ignored_tracebacks[-1]
101
102
103 class FilteredTraceback:
104 """Wraps a traceback to filter unwanted frames."""
105
106 def __init__(self, tb, filtered_traceback):
107 """Constructor.
108
109 :param tb: The start of the traceback chain to filter.
110 :param filtered_traceback: The first traceback of a trailing
111 chain that is to be filtered.
112 """
113 self._tb = tb
114 self.tb_lasti = self._tb.tb_lasti
115 self.tb_lineno = self._tb.tb_lineno
116 self.tb_frame = self._tb.tb_frame
117 self._filtered_traceback = filtered_traceback
118
119 @property
120 def tb_next(self):
121 tb_next = self._tb.tb_next
122 if tb_next and tb_next != self._filtered_traceback:
123 return FilteredTraceback(tb_next, self._filtered_traceback)