1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2019 Simon Marchi <simon.marchi@efficios.com>
5 from bt2
import native_bt
6 from collections
import abc
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
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',
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
)
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
)
33 self
._str
= native_bt
.bt2_format_bt_error_cause(ptr
)
43 def module_name(self
):
44 return self
._module
_name
48 return self
._file
_name
51 def line_number(self
):
52 return self
._line
_number
55 class _ComponentErrorCause(_ErrorCause
):
56 def __init__(self
, ptr
):
58 self
._component
_name
= native_bt
.error_cause_component_actor_get_component_name(
61 self
._component
_class
_type
= native_bt
.error_cause_component_actor_get_component_class_type(
64 self
._component
_class
_name
= native_bt
.error_cause_component_actor_get_component_class_name(
67 self
._plugin
_name
= native_bt
.error_cause_component_actor_get_plugin_name(ptr
)
70 def component_name(self
):
71 return self
._component
_name
74 def component_class_type(self
):
75 return self
._component
_class
_type
78 def component_class_name(self
):
79 return self
._component
_class
_name
82 def plugin_name(self
):
83 return self
._plugin
_name
86 class _ComponentClassErrorCause(_ErrorCause
):
87 def __init__(self
, ptr
):
89 self
._component
_class
_type
= native_bt
.error_cause_component_class_actor_get_component_class_type(
92 self
._component
_class
_name
= native_bt
.error_cause_component_class_actor_get_component_class_name(
95 self
._plugin
_name
= native_bt
.error_cause_component_class_actor_get_plugin_name(
100 def component_class_type(self
):
101 return self
._component
_class
_type
104 def component_class_name(self
):
105 return self
._component
_class
_name
108 def plugin_name(self
):
109 return self
._plugin
_name
112 class _MessageIteratorErrorCause(_ErrorCause
):
113 def __init__(self
, ptr
):
114 super().__init
__(ptr
)
115 self
._component
_name
= native_bt
.error_cause_message_iterator_actor_get_component_name(
118 self
._component
_output
_port
_name
= native_bt
.error_cause_message_iterator_actor_get_component_output_port_name(
121 self
._component
_class
_type
= native_bt
.error_cause_message_iterator_actor_get_component_class_type(
124 self
._component
_class
_name
= native_bt
.error_cause_message_iterator_actor_get_component_class_name(
127 self
._plugin
_name
= native_bt
.error_cause_message_iterator_actor_get_plugin_name(
132 def component_name(self
):
133 return self
._component
_name
136 def component_output_port_name(self
):
137 return self
._component
_output
_port
_name
140 def component_class_type(self
):
141 return self
._component
_class
_type
144 def component_class_name(self
):
145 return self
._component
_class
_name
148 def plugin_name(self
):
149 return self
._plugin
_name
152 _ACTOR_TYPE_TO_CLS
= {
153 native_bt
.ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
: _ErrorCause
,
154 native_bt
.ERROR_CAUSE_ACTOR_TYPE_COMPONENT
: _ComponentErrorCause
,
155 native_bt
.ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
: _ComponentClassErrorCause
,
156 native_bt
.ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
: _MessageIteratorErrorCause
,
160 class _Error(Exception, abc
.Sequence
):
162 Babeltrace API call error.
164 This exception is raised when a call to the Babeltrace API returns with
165 the ERROR or MEMORY_ERROR status codes.
168 def __init__(self
, msg
):
169 super().__init
__(msg
)
170 # Steal the current thread's error.
171 self
._ptr
= native_bt
.current_thread_take_error()
172 assert self
._ptr
is not None
175 self
._str
= msg
+ '\n' + native_bt
.bt2_format_bt_error(self
._ptr
)
177 # Read everything we might need from the error pointer, so we don't
178 # depend on it. It's possible for the user to keep an Error object
179 # and to want to read its causes after the error pointer has been
180 # restored as the current thread's error (and is therefore
182 cause_count
= native_bt
.error_get_cause_count(self
._ptr
)
184 # We expect the library to append at least one cause (otherwise there
185 # wouldn't be an bt_error object anyway). Also, while formatting the
186 # exception, the Python `traceback` module does:
188 # if (exc_value and ...):
190 # If the cause list was empty, this would evaluate to False (which we
191 # wouldn't want), because of the __bool__ implementation of
192 # abc.Sequence. If there's at least one cause, we are sure that
193 # __bool__ will always return True and avoid any problem here.
194 assert cause_count
> 0
198 for i
in range(cause_count
):
199 cause_ptr
= native_bt
.error_borrow_cause_by_index(self
._ptr
, i
)
200 assert cause_ptr
is not None
201 cause
= _create_error_cause_from_ptr(cause_ptr
)
202 self
._causes
.append(cause
)
205 # If this exception escapes all the way out of the Python code, the
206 # native code will steal `_ptr` to restore it as the current thread's
207 # error. If the exception is caught and discarded by the Python code,
208 # the exception object still owns the error, so we must release it.
209 if self
._ptr
is not None:
210 native_bt
.error_release(self
._ptr
)
212 def __getitem__(self
, index
):
213 return self
._causes
[index
]
216 return len(self
._causes
)