Commit | Line | Data |
---|---|---|
0235b0db MJ |
1 | # SPDX-License-Identifier: MIT |
2 | # | |
3 | # Copyright (c) 2019 Simon Marchi <simon.marchi@efficios.com> | |
4 | ||
57081273 | 5 | from bt2 import native_bt |
ce4923b0 SM |
6 | from collections import abc |
7 | ||
8 | ||
9 | class ComponentClassType: | |
10 | SOURCE = native_bt.COMPONENT_CLASS_TYPE_SOURCE | |
11 | FILTER = native_bt.COMPONENT_CLASS_TYPE_FILTER | |
12 | SINK = native_bt.COMPONENT_CLASS_TYPE_SINK | |
13 | ||
14 | ||
15 | _COMPONENT_CLASS_TYPE_TO_STR = { | |
16 | native_bt.COMPONENT_CLASS_TYPE_SOURCE: 'source', | |
17 | native_bt.COMPONENT_CLASS_TYPE_FILTER: 'filter', | |
18 | native_bt.COMPONENT_CLASS_TYPE_SINK: 'sink', | |
19 | } | |
20 | ||
21 | ||
22 | def _create_error_cause_from_ptr(ptr): | |
23 | actor_type = native_bt.error_cause_get_actor_type(ptr) | |
24 | return _ACTOR_TYPE_TO_CLS[actor_type](ptr) | |
25 | ||
26 | ||
27 | class _ErrorCause: | |
28 | def __init__(self, ptr): | |
29 | self._message = native_bt.error_cause_get_message(ptr) | |
30 | self._module_name = native_bt.error_cause_get_module_name(ptr) | |
31 | self._file_name = native_bt.error_cause_get_file_name(ptr) | |
32 | self._line_number = native_bt.error_cause_get_line_number(ptr) | |
5038e256 | 33 | self._str = native_bt.bt2_format_bt_error_cause(ptr) |
ce4923b0 SM |
34 | |
35 | def __str__(self): | |
5038e256 | 36 | return self._str |
ce4923b0 SM |
37 | |
38 | @property | |
39 | def message(self): | |
40 | return self._message | |
41 | ||
42 | @property | |
43 | def module_name(self): | |
44 | return self._module_name | |
45 | ||
46 | @property | |
47 | def file_name(self): | |
48 | return self._file_name | |
49 | ||
50 | @property | |
51 | def line_number(self): | |
52 | return self._line_number | |
53 | ||
54 | ||
55 | class _ComponentErrorCause(_ErrorCause): | |
56 | def __init__(self, ptr): | |
57 | super().__init__(ptr) | |
58 | self._component_name = native_bt.error_cause_component_actor_get_component_name( | |
59 | ptr | |
60 | ) | |
776a2a25 PP |
61 | self._component_class_type = ( |
62 | native_bt.error_cause_component_actor_get_component_class_type(ptr) | |
ce4923b0 | 63 | ) |
776a2a25 PP |
64 | self._component_class_name = ( |
65 | native_bt.error_cause_component_actor_get_component_class_name(ptr) | |
ce4923b0 SM |
66 | ) |
67 | self._plugin_name = native_bt.error_cause_component_actor_get_plugin_name(ptr) | |
68 | ||
69 | @property | |
70 | def component_name(self): | |
71 | return self._component_name | |
72 | ||
73 | @property | |
74 | def component_class_type(self): | |
75 | return self._component_class_type | |
76 | ||
77 | @property | |
78 | def component_class_name(self): | |
79 | return self._component_class_name | |
80 | ||
81 | @property | |
82 | def plugin_name(self): | |
83 | return self._plugin_name | |
84 | ||
85 | ||
86 | class _ComponentClassErrorCause(_ErrorCause): | |
87 | def __init__(self, ptr): | |
88 | super().__init__(ptr) | |
776a2a25 PP |
89 | self._component_class_type = ( |
90 | native_bt.error_cause_component_class_actor_get_component_class_type(ptr) | |
ce4923b0 | 91 | ) |
776a2a25 PP |
92 | self._component_class_name = ( |
93 | native_bt.error_cause_component_class_actor_get_component_class_name(ptr) | |
ce4923b0 SM |
94 | ) |
95 | self._plugin_name = native_bt.error_cause_component_class_actor_get_plugin_name( | |
96 | ptr | |
97 | ) | |
98 | ||
99 | @property | |
100 | def component_class_type(self): | |
101 | return self._component_class_type | |
102 | ||
103 | @property | |
104 | def component_class_name(self): | |
105 | return self._component_class_name | |
106 | ||
107 | @property | |
108 | def plugin_name(self): | |
109 | return self._plugin_name | |
110 | ||
111 | ||
112 | class _MessageIteratorErrorCause(_ErrorCause): | |
113 | def __init__(self, ptr): | |
114 | super().__init__(ptr) | |
776a2a25 PP |
115 | self._component_name = ( |
116 | native_bt.error_cause_message_iterator_actor_get_component_name(ptr) | |
ce4923b0 | 117 | ) |
776a2a25 PP |
118 | self._component_output_port_name = ( |
119 | native_bt.error_cause_message_iterator_actor_get_component_output_port_name( | |
120 | ptr | |
121 | ) | |
ce4923b0 | 122 | ) |
776a2a25 PP |
123 | self._component_class_type = ( |
124 | native_bt.error_cause_message_iterator_actor_get_component_class_type(ptr) | |
ce4923b0 | 125 | ) |
776a2a25 PP |
126 | self._component_class_name = ( |
127 | native_bt.error_cause_message_iterator_actor_get_component_class_name(ptr) | |
ce4923b0 | 128 | ) |
776a2a25 PP |
129 | self._plugin_name = ( |
130 | native_bt.error_cause_message_iterator_actor_get_plugin_name(ptr) | |
ce4923b0 SM |
131 | ) |
132 | ||
133 | @property | |
134 | def component_name(self): | |
135 | return self._component_name | |
136 | ||
137 | @property | |
138 | def component_output_port_name(self): | |
139 | return self._component_output_port_name | |
140 | ||
141 | @property | |
142 | def component_class_type(self): | |
143 | return self._component_class_type | |
144 | ||
145 | @property | |
146 | def component_class_name(self): | |
147 | return self._component_class_name | |
148 | ||
149 | @property | |
150 | def plugin_name(self): | |
151 | return self._plugin_name | |
152 | ||
153 | ||
154 | _ACTOR_TYPE_TO_CLS = { | |
155 | native_bt.ERROR_CAUSE_ACTOR_TYPE_UNKNOWN: _ErrorCause, | |
156 | native_bt.ERROR_CAUSE_ACTOR_TYPE_COMPONENT: _ComponentErrorCause, | |
157 | native_bt.ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS: _ComponentClassErrorCause, | |
158 | native_bt.ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR: _MessageIteratorErrorCause, | |
159 | } | |
160 | ||
161 | ||
694c792b | 162 | class _Error(Exception, abc.Sequence): |
ce4923b0 SM |
163 | """ |
164 | Babeltrace API call error. | |
165 | ||
166 | This exception is raised when a call to the Babeltrace API returns with | |
167 | the ERROR or MEMORY_ERROR status codes. | |
168 | """ | |
169 | ||
34d5cf2f | 170 | def __init__(self, msg): |
ce4923b0 SM |
171 | super().__init__(msg) |
172 | # Steal the current thread's error. | |
173 | self._ptr = native_bt.current_thread_take_error() | |
174 | assert self._ptr is not None | |
175 | ||
8258c4bd | 176 | self._msg = msg |
5038e256 | 177 | self._str = msg + '\n' + native_bt.bt2_format_bt_error(self._ptr) |
8258c4bd | 178 | |
ce4923b0 SM |
179 | # Read everything we might need from the error pointer, so we don't |
180 | # depend on it. It's possible for the user to keep an Error object | |
181 | # and to want to read its causes after the error pointer has been | |
182 | # restored as the current thread's error (and is therefore | |
183 | # inaccessible). | |
184 | cause_count = native_bt.error_get_cause_count(self._ptr) | |
185 | ||
186 | # We expect the library to append at least one cause (otherwise there | |
187 | # wouldn't be an bt_error object anyway). Also, while formatting the | |
188 | # exception, the Python `traceback` module does: | |
189 | # | |
190 | # if (exc_value and ...): | |
191 | # | |
192 | # If the cause list was empty, this would evaluate to False (which we | |
193 | # wouldn't want), because of the __bool__ implementation of | |
194 | # abc.Sequence. If there's at least one cause, we are sure that | |
195 | # __bool__ will always return True and avoid any problem here. | |
196 | assert cause_count > 0 | |
197 | ||
198 | self._causes = [] | |
199 | ||
200 | for i in range(cause_count): | |
201 | cause_ptr = native_bt.error_borrow_cause_by_index(self._ptr, i) | |
202 | assert cause_ptr is not None | |
203 | cause = _create_error_cause_from_ptr(cause_ptr) | |
204 | self._causes.append(cause) | |
205 | ||
206 | def __del__(self): | |
207 | # If this exception escapes all the way out of the Python code, the | |
208 | # native code will steal `_ptr` to restore it as the current thread's | |
209 | # error. If the exception is caught and discarded by the Python code, | |
210 | # the exception object still owns the error, so we must release it. | |
211 | if self._ptr is not None: | |
212 | native_bt.error_release(self._ptr) | |
213 | ||
214 | def __getitem__(self, index): | |
215 | return self._causes[index] | |
216 | ||
217 | def __len__(self): | |
218 | return len(self._causes) | |
8258c4bd SM |
219 | |
220 | def __str__(self): | |
5038e256 | 221 | return self._str |