Commit | Line | Data |
---|---|---|
5fa16feb PP |
1 | /* |
2 | * The MIT License (MIT) | |
3 | * | |
4 | * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "logging/comp-logging.h" | |
b20382e2 | 26 | #include "compat/glib.h" |
5fa16feb PP |
27 | |
28 | /* | |
29 | * This hash table associates a BT component class object address to a | |
30 | * user-defined Python class (PyObject *). The keys and values are NOT | |
31 | * owned by this hash table. The Python class objects are owned by the | |
32 | * Python module, which should not be unloaded until it is not possible | |
33 | * to create a user Python component anyway. | |
34 | * | |
35 | * This hash table is written to when a user-defined Python component | |
36 | * class is created by one of the bt_bt2_component_class_*_create() | |
37 | * functions. | |
38 | * | |
39 | * This function is read from when a user calls bt_component_create() | |
40 | * with a component class pointer created by one of the functions above. | |
41 | * In this case, the original Python class needs to be found to | |
42 | * instantiate it and associate the created Python component object with | |
43 | * a BT component object instance. | |
44 | */ | |
45 | ||
46 | static GHashTable *bt_cc_ptr_to_py_cls; | |
47 | ||
774b7f28 PP |
48 | static |
49 | void bt_bt2_unregister_cc_ptr_to_py_cls(const bt_component_class *comp_cls) | |
50 | { | |
51 | gboolean existed; | |
52 | ||
53 | if (!bt_cc_ptr_to_py_cls) { | |
54 | return; | |
55 | } | |
56 | ||
57 | existed = g_hash_table_remove(bt_cc_ptr_to_py_cls, comp_cls); | |
58 | BT_ASSERT(existed); | |
59 | } | |
60 | ||
5fa16feb PP |
61 | static |
62 | void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc, | |
63 | PyObject *py_cls) | |
64 | { | |
65 | if (!bt_cc_ptr_to_py_cls) { | |
66 | /* | |
67 | * Lazy-initializing this GHashTable because GLib | |
68 | * might not be initialized yet and it needs to be | |
69 | * before we call g_hash_table_new() | |
70 | */ | |
71 | BT_LOGD_STR("Creating native component class to Python component class hash table."); | |
72 | bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal); | |
73 | BT_ASSERT(bt_cc_ptr_to_py_cls); | |
74 | } | |
75 | ||
76 | g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc, | |
77 | (gpointer) py_cls); | |
78 | } | |
79 | ||
80 | static | |
81 | PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc) | |
82 | { | |
83 | if (!bt_cc_ptr_to_py_cls) { | |
84 | BT_LOGW("Cannot look up Python component class because hash table is NULL: " | |
85 | "comp-cls-addr=%p", bt_cc); | |
86 | return NULL; | |
87 | } | |
88 | ||
89 | return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls, | |
90 | (gconstpointer) bt_cc); | |
91 | } | |
92 | ||
5fa16feb PP |
93 | /* Library destructor */ |
94 | ||
95 | __attribute__((destructor)) | |
96 | static | |
97 | void native_comp_class_dtor(void) { | |
98 | /* Destroy component class association hash table */ | |
99 | if (bt_cc_ptr_to_py_cls) { | |
100 | BT_LOGD_STR("Destroying native component class to Python component class hash table."); | |
101 | g_hash_table_destroy(bt_cc_ptr_to_py_cls); | |
774b7f28 | 102 | bt_cc_ptr_to_py_cls = NULL; |
5fa16feb PP |
103 | } |
104 | } | |
105 | ||
5fa16feb | 106 | static inline |
27ba49f1 SM |
107 | int py_exc_to_status_clear( |
108 | bt_self_component_class *self_component_class, | |
5fa16feb PP |
109 | bt_self_component *self_component, |
110 | bt_self_message_iterator *self_message_iterator, | |
111 | const char *module_name, int active_log_level) | |
112 | { | |
113 | int status = __BT_FUNC_STATUS_OK; | |
114 | PyObject *exc = PyErr_Occurred(); | |
115 | ||
116 | if (!exc) { | |
117 | goto end; | |
118 | } | |
119 | ||
120 | if (PyErr_GivenExceptionMatches(exc, | |
121 | py_mod_bt2_exc_try_again_type)) { | |
122 | status = __BT_FUNC_STATUS_AGAIN; | |
123 | } else if (PyErr_GivenExceptionMatches(exc, | |
124 | py_mod_bt2_exc_stop_type)) { | |
125 | status = __BT_FUNC_STATUS_END; | |
126 | } else if (PyErr_GivenExceptionMatches(exc, | |
127 | py_mod_bt2_exc_unknown_object_type)) { | |
128 | status = __BT_FUNC_STATUS_UNKNOWN_OBJECT; | |
129 | } else { | |
130 | /* | |
131 | * Unknown exception: convert to general error. | |
132 | * | |
133 | * Because we only want to fetch the log level when | |
134 | * we actually get an exception, and not systematically | |
135 | * when we call py_exc_to_status() (as py_exc_to_status() | |
136 | * can return `__BT_FUNC_STATUS_OK`), we get it here | |
137 | * depending on the actor's type. | |
138 | */ | |
139 | if (self_component) { | |
140 | active_log_level = get_self_component_log_level( | |
141 | self_component); | |
142 | } else if (self_message_iterator) { | |
143 | active_log_level = get_self_message_iterator_log_level( | |
144 | self_message_iterator); | |
145 | } | |
146 | ||
147 | BT_ASSERT(active_log_level != -1); | |
27ba49f1 | 148 | log_exception_and_maybe_append_cause(BT_LOG_WARNING, |
5fa16feb PP |
149 | active_log_level, true, |
150 | self_component_class, self_component, | |
151 | self_message_iterator, module_name); | |
152 | ||
153 | if (PyErr_GivenExceptionMatches(exc, | |
154 | py_mod_bt2_exc_memory_error)) { | |
155 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
156 | } else { | |
157 | status = __BT_FUNC_STATUS_ERROR; | |
158 | } | |
159 | } | |
160 | ||
161 | end: | |
162 | PyErr_Clear(); | |
163 | return status; | |
164 | } | |
165 | ||
166 | static | |
27ba49f1 | 167 | int py_exc_to_status_component_class_clear( |
5fa16feb PP |
168 | bt_self_component_class *self_component_class, |
169 | int active_log_level) | |
170 | { | |
27ba49f1 | 171 | return py_exc_to_status_clear(self_component_class, NULL, NULL, NULL, |
5fa16feb PP |
172 | active_log_level); |
173 | } | |
174 | ||
175 | static | |
27ba49f1 | 176 | int py_exc_to_status_component_clear(bt_self_component *self_component) |
5fa16feb | 177 | { |
27ba49f1 | 178 | return py_exc_to_status_clear(NULL, self_component, NULL, NULL, -1); |
5fa16feb PP |
179 | } |
180 | ||
181 | static | |
27ba49f1 | 182 | int py_exc_to_status_message_iterator_clear( |
5fa16feb PP |
183 | bt_self_message_iterator *self_message_iterator) |
184 | { | |
27ba49f1 | 185 | return py_exc_to_status_clear(NULL, NULL, self_message_iterator, NULL, -1); |
5fa16feb PP |
186 | } |
187 | ||
b20382e2 PP |
188 | static |
189 | bool bt_bt2_is_python_component_class(const bt_component_class *comp_cls) | |
190 | { | |
191 | return bt_g_hash_table_contains(bt_cc_ptr_to_py_cls, comp_cls); | |
192 | } | |
193 | ||
5fa16feb PP |
194 | /* Component class proxy methods (delegate to the attached Python object) */ |
195 | ||
196 | static | |
4175c1d5 | 197 | bt_component_class_initialize_method_status component_class_init( |
5fa16feb PP |
198 | bt_self_component *self_component, |
199 | void *self_component_v, | |
200 | swig_type_info *self_comp_cls_type_swig_type, | |
201 | const bt_value *params, | |
202 | void *init_method_data) | |
203 | { | |
204 | const bt_component *component = bt_self_component_as_component(self_component); | |
205 | const bt_component_class *component_class = bt_component_borrow_class_const(component); | |
4175c1d5 | 206 | bt_component_class_initialize_method_status status = __BT_FUNC_STATUS_OK; |
5fa16feb PP |
207 | PyObject *py_cls = NULL; |
208 | PyObject *py_comp = NULL; | |
209 | PyObject *py_params_ptr = NULL; | |
210 | PyObject *py_comp_ptr = NULL; | |
211 | bt_logging_level log_level = get_self_component_log_level( | |
212 | self_component); | |
213 | ||
5fa16feb PP |
214 | BT_ASSERT(self_component); |
215 | BT_ASSERT(self_component_v); | |
216 | BT_ASSERT(self_comp_cls_type_swig_type); | |
217 | ||
218 | /* | |
219 | * Get the user-defined Python class which created this | |
220 | * component's class in the first place (borrowed | |
221 | * reference). | |
222 | */ | |
223 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
224 | if (!py_cls) { | |
225 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
226 | "Cannot find Python class associated to native component class: " | |
227 | "comp-cls-addr=%p", component_class); | |
228 | goto error; | |
229 | } | |
230 | ||
231 | /* Parameters pointer -> SWIG pointer Python object */ | |
232 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
233 | SWIGTYPE_p_bt_value, 0); | |
234 | if (!py_params_ptr) { | |
235 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
236 | "Failed to create a SWIG pointer object."); | |
237 | goto error; | |
238 | } | |
239 | ||
240 | py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v), | |
241 | self_comp_cls_type_swig_type, 0); | |
242 | if (!py_comp_ptr) { | |
243 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
244 | "Failed to create a SWIG pointer object."); | |
245 | goto error; | |
246 | } | |
247 | ||
248 | /* | |
249 | * Do the equivalent of this: | |
250 | * | |
b20382e2 PP |
251 | * py_comp = py_cls._bt_init_from_native(py_comp_ptr, |
252 | * py_params_ptr, init_method_data ? init_method_data : Py_None) | |
5fa16feb PP |
253 | * |
254 | * _UserComponentType._bt_init_from_native() calls the Python | |
255 | * component object's __init__() function. | |
b20382e2 PP |
256 | * |
257 | * We don't take any reference on `init_method_data` which, if | |
258 | * not `NULL`, is assumed to be a `PyObject *`: the user's | |
259 | * __init__() function will eventually take a reference if | |
260 | * needed. If `init_method_data` is `NULL`, then we pass | |
261 | * `Py_None` as the initialization's Python object. | |
5fa16feb PP |
262 | */ |
263 | py_comp = PyObject_CallMethod(py_cls, | |
b20382e2 PP |
264 | "_bt_init_from_native", "(OOO)", py_comp_ptr, py_params_ptr, |
265 | init_method_data ? init_method_data : Py_None); | |
5fa16feb PP |
266 | if (!py_comp) { |
267 | BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component, | |
268 | "Failed to call Python class's _bt_init_from_native() method: " | |
269 | "py-cls-addr=%p", py_cls); | |
27ba49f1 | 270 | status = py_exc_to_status_component_clear(self_component); |
5fa16feb PP |
271 | goto end; |
272 | } | |
273 | ||
274 | /* | |
275 | * Our user Python component object is now fully created and | |
276 | * initialized by the user. Since we just created it, this | |
277 | * native component is its only (persistent) owner. | |
278 | */ | |
279 | bt_self_component_set_data(self_component, py_comp); | |
280 | py_comp = NULL; | |
281 | goto end; | |
282 | ||
283 | error: | |
284 | status = __BT_FUNC_STATUS_ERROR; | |
285 | ||
286 | /* | |
287 | * Clear any exception: we're returning a bad status anyway. If | |
288 | * this call originated from Python (creation from a plugin's | |
289 | * component class, for example), then the user gets an | |
290 | * appropriate creation error. | |
291 | */ | |
292 | PyErr_Clear(); | |
293 | ||
294 | end: | |
295 | Py_XDECREF(py_comp); | |
296 | Py_XDECREF(py_params_ptr); | |
297 | Py_XDECREF(py_comp_ptr); | |
298 | return status; | |
299 | } | |
300 | ||
ed68d910 PP |
301 | static |
302 | bt_component_class_get_supported_mip_versions_method_status | |
303 | component_class_get_supported_mip_versions( | |
304 | const bt_component_class *component_class, | |
305 | bt_self_component_class *self_component_class, | |
306 | const bt_value *params, void *init_method_data, | |
307 | bt_logging_level log_level, | |
308 | bt_integer_range_set_unsigned *supported_versions) | |
309 | { | |
310 | uint64_t i; | |
311 | PyObject *py_cls = NULL; | |
312 | PyObject *py_params_ptr = NULL; | |
313 | PyObject *py_range_set_addr = NULL; | |
314 | bt_integer_range_set_unsigned *ret_range_set = NULL; | |
315 | bt_component_class_get_supported_mip_versions_method_status status = | |
316 | __BT_FUNC_STATUS_OK; | |
317 | ||
318 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
319 | if (!py_cls) { | |
320 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
321 | "Cannot find Python class associated to native component class: " | |
322 | "comp-cls-addr=%p", component_class); | |
323 | goto error; | |
324 | } | |
325 | ||
326 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
327 | SWIGTYPE_p_bt_value, 0); | |
328 | if (!py_params_ptr) { | |
329 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
330 | "Failed to create a SWIG pointer object."); | |
331 | goto error; | |
332 | } | |
333 | ||
334 | /* | |
335 | * We don't take any reference on `init_method_data` which, if | |
336 | * not `NULL`, is assumed to be a `PyObject *`: the user's | |
337 | * _user_get_supported_mip_versions() function will eventually | |
338 | * take a reference if needed. If `init_method_data` is `NULL`, | |
339 | * then we pass `Py_None` as the initialization's Python object. | |
340 | */ | |
341 | py_range_set_addr = PyObject_CallMethod(py_cls, | |
342 | "_bt_get_supported_mip_versions_from_native", "(OOi)", | |
343 | py_params_ptr, init_method_data ? init_method_data : Py_None, | |
344 | (int) log_level); | |
345 | if (!py_range_set_addr) { | |
346 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG, | |
347 | "Failed to call Python class's _bt_get_supported_mip_versions_from_native() method: " | |
348 | "py-cls-addr=%p", py_cls); | |
27ba49f1 | 349 | status = py_exc_to_status_component_class_clear(self_component_class, |
ed68d910 PP |
350 | log_level); |
351 | goto end; | |
352 | } | |
353 | ||
354 | /* | |
355 | * The returned object, on success, is an integer object | |
356 | * (PyLong) containing the address of a BT unsigned integer | |
357 | * range set object (new reference). | |
358 | */ | |
359 | ret_range_set = PyLong_AsVoidPtr(py_range_set_addr); | |
360 | BT_ASSERT(!PyErr_Occurred()); | |
361 | BT_ASSERT(ret_range_set); | |
362 | ||
363 | /* Copy returned ranges to input range set */ | |
364 | for (i = 0; i < bt_integer_range_set_get_range_count( | |
365 | bt_integer_range_set_unsigned_as_range_set_const(ret_range_set)); | |
366 | i++) { | |
367 | const bt_integer_range_unsigned *range = | |
368 | bt_integer_range_set_unsigned_borrow_range_by_index_const( | |
369 | ret_range_set, i); | |
370 | bt_integer_range_set_add_range_status add_range_status; | |
371 | ||
372 | add_range_status = bt_integer_range_set_unsigned_add_range( | |
373 | supported_versions, | |
374 | bt_integer_range_unsigned_get_lower(range), | |
375 | bt_integer_range_unsigned_get_upper(range)); | |
376 | if (add_range_status) { | |
377 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
378 | "Failed to add range to supported MIP versions range set."); | |
379 | goto error; | |
380 | } | |
381 | } | |
382 | ||
383 | goto end; | |
384 | ||
385 | error: | |
386 | PyErr_Clear(); | |
387 | status = __BT_FUNC_STATUS_ERROR; | |
388 | ||
389 | end: | |
390 | Py_XDECREF(py_params_ptr); | |
391 | Py_XDECREF(py_range_set_addr); | |
392 | bt_integer_range_set_unsigned_put_ref(ret_range_set); | |
393 | return status; | |
394 | } | |
395 | ||
396 | static | |
397 | bt_component_class_get_supported_mip_versions_method_status | |
398 | component_class_source_get_supported_mip_versions( | |
399 | bt_self_component_class_source *self_component_class_source, | |
400 | const bt_value *params, void *init_method_data, | |
401 | bt_logging_level log_level, | |
402 | bt_integer_range_set_unsigned *supported_versions) | |
403 | { | |
404 | const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); | |
405 | const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); | |
406 | bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source); | |
407 | ||
408 | return component_class_get_supported_mip_versions( | |
409 | component_class, self_component_class, | |
410 | params, init_method_data, log_level, supported_versions); | |
411 | } | |
412 | ||
413 | static | |
414 | bt_component_class_get_supported_mip_versions_method_status | |
415 | component_class_filter_get_supported_mip_versions( | |
416 | bt_self_component_class_filter *self_component_class_filter, | |
417 | const bt_value *params, void *init_method_data, | |
418 | bt_logging_level log_level, | |
419 | bt_integer_range_set_unsigned *supported_versions) | |
420 | { | |
421 | const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); | |
422 | const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); | |
423 | bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter); | |
424 | ||
425 | return component_class_get_supported_mip_versions( | |
426 | component_class, self_component_class, | |
427 | params, init_method_data, log_level, supported_versions); | |
428 | } | |
429 | ||
430 | static | |
431 | bt_component_class_get_supported_mip_versions_method_status | |
432 | component_class_sink_get_supported_mip_versions( | |
433 | bt_self_component_class_sink *self_component_class_sink, | |
434 | const bt_value *params, void *init_method_data, | |
435 | bt_logging_level log_level, | |
436 | bt_integer_range_set_unsigned *supported_versions) | |
437 | { | |
438 | const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); | |
439 | const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); | |
440 | bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink); | |
441 | ||
442 | return component_class_get_supported_mip_versions( | |
443 | component_class, self_component_class, | |
444 | params, init_method_data, log_level, supported_versions); | |
445 | } | |
446 | ||
5fa16feb PP |
447 | /* |
448 | * Method of bt_component_class_source to initialize a bt_self_component_source | |
449 | * of that class. | |
450 | */ | |
451 | ||
452 | static | |
4175c1d5 | 453 | bt_component_class_initialize_method_status component_class_source_init( |
5fa16feb | 454 | bt_self_component_source *self_component_source, |
e3250e61 | 455 | bt_self_component_source_configuration *config, |
5fa16feb PP |
456 | const bt_value *params, void *init_method_data) |
457 | { | |
458 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
459 | return component_class_init( | |
460 | self_component, | |
461 | self_component_source, | |
462 | SWIGTYPE_p_bt_self_component_source, | |
463 | params, init_method_data); | |
464 | } | |
465 | ||
466 | static | |
4175c1d5 | 467 | bt_component_class_initialize_method_status component_class_filter_init( |
5fa16feb | 468 | bt_self_component_filter *self_component_filter, |
e3250e61 | 469 | bt_self_component_filter_configuration *config, |
5fa16feb PP |
470 | const bt_value *params, void *init_method_data) |
471 | { | |
472 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
473 | return component_class_init( | |
474 | self_component, | |
475 | self_component_filter, | |
476 | SWIGTYPE_p_bt_self_component_filter, | |
477 | params, init_method_data); | |
478 | } | |
479 | ||
480 | static | |
4175c1d5 | 481 | bt_component_class_initialize_method_status component_class_sink_init( |
5fa16feb | 482 | bt_self_component_sink *self_component_sink, |
e3250e61 | 483 | bt_self_component_sink_configuration *config, |
5fa16feb PP |
484 | const bt_value *params, void *init_method_data) |
485 | { | |
486 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
487 | return component_class_init( | |
488 | self_component, | |
489 | self_component_sink, | |
490 | SWIGTYPE_p_bt_self_component_sink, | |
491 | params, init_method_data); | |
492 | } | |
493 | ||
494 | static | |
495 | void component_class_finalize(bt_self_component *self_component) | |
496 | { | |
497 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
498 | BT_ASSERT(py_comp); | |
499 | ||
500 | /* Call user's _user_finalize() method */ | |
501 | PyObject *py_method_result = PyObject_CallMethod(py_comp, | |
502 | "_user_finalize", NULL); | |
503 | ||
504 | if (PyErr_Occurred()) { | |
505 | bt_logging_level log_level = get_self_component_log_level( | |
506 | self_component); | |
507 | ||
508 | BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component, | |
509 | "User component's _user_finalize() method raised an exception: ignoring:"); | |
510 | logw_exception(log_level); | |
511 | } | |
512 | ||
513 | /* | |
514 | * Ignore any exception raised by the _user_finalize() method | |
515 | * because it won't change anything at this point: the component | |
516 | * is being destroyed anyway. | |
517 | */ | |
518 | PyErr_Clear(); | |
519 | Py_XDECREF(py_method_result); | |
520 | Py_DECREF(py_comp); | |
521 | } | |
522 | ||
523 | static | |
524 | void component_class_source_finalize(bt_self_component_source *self_component_source) | |
525 | { | |
526 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
527 | component_class_finalize(self_component); | |
528 | } | |
529 | ||
530 | static | |
531 | void component_class_filter_finalize(bt_self_component_filter *self_component_filter) | |
532 | { | |
533 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
534 | component_class_finalize(self_component); | |
535 | } | |
536 | ||
537 | static | |
538 | void component_class_sink_finalize(bt_self_component_sink *self_component_sink) | |
539 | { | |
540 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
541 | component_class_finalize(self_component); | |
542 | } | |
543 | ||
544 | static | |
9e8e8b43 SM |
545 | bt_component_class_message_iterator_can_seek_beginning_method_status |
546 | component_class_can_seek_beginning( | |
547 | bt_self_message_iterator *self_message_iterator, bt_bool *can_seek) | |
5fa16feb PP |
548 | { |
549 | PyObject *py_iter; | |
550 | PyObject *py_result = NULL; | |
9e8e8b43 | 551 | bt_component_class_message_iterator_can_seek_beginning_method_status status; |
5fa16feb PP |
552 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); |
553 | BT_ASSERT(py_iter); | |
554 | ||
5c836b95 SM |
555 | py_result = PyObject_CallMethod(py_iter, |
556 | "_bt_can_seek_beginning_from_native", NULL); | |
5fa16feb PP |
557 | |
558 | BT_ASSERT(!py_result || PyBool_Check(py_result)); | |
559 | ||
560 | if (py_result) { | |
9e8e8b43 SM |
561 | *can_seek = PyObject_IsTrue(py_result); |
562 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK; | |
5fa16feb | 563 | } else { |
27ba49f1 | 564 | status = py_exc_to_status_message_iterator_clear(self_message_iterator); |
9e8e8b43 | 565 | BT_ASSERT(status != __BT_FUNC_STATUS_OK); |
5fa16feb PP |
566 | } |
567 | ||
568 | Py_XDECREF(py_result); | |
569 | ||
9e8e8b43 | 570 | return status; |
5fa16feb PP |
571 | } |
572 | ||
573 | static | |
574 | bt_component_class_message_iterator_seek_beginning_method_status | |
575 | component_class_seek_beginning(bt_self_message_iterator *self_message_iterator) | |
576 | { | |
577 | PyObject *py_iter; | |
578 | PyObject *py_result; | |
579 | bt_component_class_message_iterator_seek_beginning_method_status status; | |
580 | ||
581 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); | |
582 | BT_ASSERT(py_iter); | |
583 | py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native", | |
584 | NULL); | |
585 | BT_ASSERT(!py_result || py_result == Py_None); | |
27ba49f1 | 586 | status = py_exc_to_status_message_iterator_clear(self_message_iterator); |
5fa16feb PP |
587 | Py_XDECREF(py_result); |
588 | return status; | |
589 | } | |
590 | ||
24a68958 SM |
591 | static |
592 | bt_component_class_message_iterator_can_seek_ns_from_origin_method_status | |
593 | component_class_can_seek_ns_from_origin( | |
594 | bt_self_message_iterator *self_message_iterator, | |
595 | int64_t ns_from_origin, bt_bool *can_seek) | |
596 | { | |
597 | PyObject *py_iter; | |
598 | PyObject *py_result = NULL; | |
599 | bt_component_class_message_iterator_can_seek_ns_from_origin_method_status status; | |
600 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); | |
601 | BT_ASSERT(py_iter); | |
602 | ||
603 | py_result = PyObject_CallMethod(py_iter, | |
604 | "_bt_can_seek_ns_from_origin_from_native", "L", ns_from_origin); | |
605 | ||
606 | BT_ASSERT(!py_result || PyBool_Check(py_result)); | |
607 | ||
608 | if (py_result) { | |
609 | *can_seek = PyObject_IsTrue(py_result); | |
610 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; | |
611 | } else { | |
27ba49f1 | 612 | status = py_exc_to_status_message_iterator_clear(self_message_iterator); |
24a68958 SM |
613 | BT_ASSERT(status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK); |
614 | } | |
615 | ||
616 | Py_XDECREF(py_result); | |
617 | ||
618 | return status; | |
619 | } | |
620 | ||
621 | static | |
622 | bt_component_class_message_iterator_seek_ns_from_origin_method_status | |
623 | component_class_seek_ns_from_origin( | |
624 | bt_self_message_iterator *self_message_iterator, | |
625 | int64_t ns_from_origin) | |
626 | { | |
627 | PyObject *py_iter; | |
628 | PyObject *py_result; | |
629 | bt_component_class_message_iterator_seek_ns_from_origin_method_status status; | |
630 | ||
631 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); | |
632 | BT_ASSERT(py_iter); | |
633 | py_result = PyObject_CallMethod(py_iter, | |
634 | "_bt_seek_ns_from_origin_from_native", "L", ns_from_origin); | |
635 | BT_ASSERT(!py_result || py_result == Py_None); | |
27ba49f1 | 636 | status = py_exc_to_status_message_iterator_clear(self_message_iterator); |
24a68958 SM |
637 | Py_XDECREF(py_result); |
638 | return status; | |
639 | } | |
640 | ||
5fa16feb PP |
641 | static |
642 | bt_component_class_port_connected_method_status component_class_port_connected( | |
643 | bt_self_component *self_component, | |
644 | void *self_component_port, | |
645 | swig_type_info *self_component_port_swig_type, | |
646 | bt_port_type self_component_port_type, | |
647 | const void *other_port, | |
648 | swig_type_info *other_port_swig_type) | |
649 | { | |
650 | bt_component_class_port_connected_method_status status; | |
651 | PyObject *py_comp = NULL; | |
652 | PyObject *py_self_port_ptr = NULL; | |
653 | PyObject *py_other_port_ptr = NULL; | |
654 | PyObject *py_method_result = NULL; | |
655 | bt_logging_level log_level = get_self_component_log_level( | |
656 | self_component); | |
657 | ||
658 | py_comp = bt_self_component_get_data(self_component); | |
659 | BT_ASSERT(py_comp); | |
660 | py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port), | |
661 | self_component_port_swig_type, 0); | |
662 | if (!py_self_port_ptr) { | |
663 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
664 | "Failed to create a SWIG pointer object."); | |
665 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
666 | goto end; | |
667 | } | |
668 | ||
669 | py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port), | |
670 | other_port_swig_type, 0); | |
671 | if (!py_other_port_ptr) { | |
672 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
673 | "Failed to create a SWIG pointer object."); | |
674 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
675 | goto end; } | |
676 | ||
677 | py_method_result = PyObject_CallMethod(py_comp, | |
678 | "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr, | |
679 | self_component_port_type, py_other_port_ptr); | |
680 | BT_ASSERT(!py_method_result || py_method_result == Py_None); | |
27ba49f1 | 681 | status = py_exc_to_status_component_clear(self_component); |
5fa16feb PP |
682 | |
683 | end: | |
684 | Py_XDECREF(py_self_port_ptr); | |
685 | Py_XDECREF(py_other_port_ptr); | |
686 | Py_XDECREF(py_method_result); | |
687 | return status; | |
688 | } | |
689 | ||
690 | static | |
691 | bt_component_class_port_connected_method_status | |
692 | component_class_source_output_port_connected( | |
693 | bt_self_component_source *self_component_source, | |
694 | bt_self_component_port_output *self_component_port_output, | |
695 | const bt_port_input *other_port_input) | |
696 | { | |
697 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
698 | ||
699 | return component_class_port_connected( | |
700 | self_component, | |
701 | self_component_port_output, | |
702 | SWIGTYPE_p_bt_self_component_port_output, | |
703 | BT_PORT_TYPE_OUTPUT, | |
704 | other_port_input, | |
705 | SWIGTYPE_p_bt_port_input); | |
706 | } | |
707 | ||
708 | static | |
709 | bt_component_class_port_connected_method_status | |
710 | component_class_filter_input_port_connected( | |
711 | bt_self_component_filter *self_component_filter, | |
712 | bt_self_component_port_input *self_component_port_input, | |
713 | const bt_port_output *other_port_output) | |
714 | { | |
715 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
716 | ||
717 | return component_class_port_connected( | |
718 | self_component, | |
719 | self_component_port_input, | |
720 | SWIGTYPE_p_bt_self_component_port_input, | |
721 | BT_PORT_TYPE_INPUT, | |
722 | other_port_output, | |
723 | SWIGTYPE_p_bt_port_output); | |
724 | } | |
725 | ||
726 | static | |
727 | bt_component_class_port_connected_method_status | |
728 | component_class_filter_output_port_connected( | |
729 | bt_self_component_filter *self_component_filter, | |
730 | bt_self_component_port_output *self_component_port_output, | |
731 | const bt_port_input *other_port_input) | |
732 | { | |
733 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
734 | ||
735 | return component_class_port_connected( | |
736 | self_component, | |
737 | self_component_port_output, | |
738 | SWIGTYPE_p_bt_self_component_port_output, | |
739 | BT_PORT_TYPE_OUTPUT, | |
740 | other_port_input, | |
741 | SWIGTYPE_p_bt_port_input); | |
742 | } | |
743 | ||
744 | static | |
745 | bt_component_class_port_connected_method_status | |
746 | component_class_sink_input_port_connected( | |
747 | bt_self_component_sink *self_component_sink, | |
748 | bt_self_component_port_input *self_component_port_input, | |
749 | const bt_port_output *other_port_output) | |
750 | { | |
751 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
752 | ||
753 | return component_class_port_connected( | |
754 | self_component, | |
755 | self_component_port_input, | |
756 | SWIGTYPE_p_bt_self_component_port_input, | |
757 | BT_PORT_TYPE_INPUT, | |
758 | other_port_output, | |
759 | SWIGTYPE_p_bt_port_output); | |
760 | } | |
761 | ||
762 | static | |
763 | bt_component_class_sink_graph_is_configured_method_status | |
764 | component_class_sink_graph_is_configured( | |
765 | bt_self_component_sink *self_component_sink) | |
766 | { | |
767 | PyObject *py_comp = NULL; | |
768 | PyObject *py_method_result = NULL; | |
769 | bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK; | |
770 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
771 | ||
772 | py_comp = bt_self_component_get_data(self_component); | |
773 | py_method_result = PyObject_CallMethod(py_comp, | |
774 | "_bt_graph_is_configured_from_native", NULL); | |
775 | BT_ASSERT(!py_method_result || py_method_result == Py_None); | |
27ba49f1 | 776 | status = py_exc_to_status_component_clear(self_component); |
5fa16feb PP |
777 | Py_XDECREF(py_method_result); |
778 | return status; | |
779 | } | |
780 | ||
781 | static | |
782 | bt_component_class_query_method_status component_class_query( | |
783 | const bt_component_class *component_class, | |
784 | bt_self_component_class *self_component_class, | |
785 | bt_private_query_executor *priv_query_executor, | |
f6e305ce | 786 | const char *object, const bt_value *params, void *method_data, |
5fa16feb PP |
787 | const bt_value **result) |
788 | { | |
789 | PyObject *py_cls = NULL; | |
790 | PyObject *py_params_ptr = NULL; | |
791 | PyObject *py_priv_query_exec_ptr = NULL; | |
792 | PyObject *py_query_func = NULL; | |
793 | PyObject *py_object = NULL; | |
794 | PyObject *py_results_addr = NULL; | |
795 | bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK; | |
796 | const bt_query_executor *query_exec = | |
797 | bt_private_query_executor_as_query_executor_const( | |
798 | priv_query_executor); | |
799 | bt_logging_level log_level = | |
800 | bt_query_executor_get_logging_level(query_exec); | |
801 | ||
f6e305ce PP |
802 | /* |
803 | * If there's any `method_data`, assume this component class is | |
804 | * getting queried from Python, so that `method_data` is a | |
805 | * Python object to pass to the user's _user_query() method. | |
806 | */ | |
807 | BT_ASSERT(!method_data || | |
808 | bt_bt2_is_python_component_class(component_class)); | |
809 | ||
5fa16feb PP |
810 | py_cls = lookup_cc_ptr_to_py_cls(component_class); |
811 | if (!py_cls) { | |
812 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
813 | "Cannot find Python class associated to native component class: " | |
814 | "comp-cls-addr=%p", component_class); | |
815 | goto error; | |
816 | } | |
817 | ||
818 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
819 | SWIGTYPE_p_bt_value, 0); | |
820 | if (!py_params_ptr) { | |
821 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
822 | "Failed to create a SWIG pointer object."); | |
823 | goto error; | |
824 | } | |
825 | ||
826 | py_priv_query_exec_ptr = SWIG_NewPointerObj( | |
827 | SWIG_as_voidptr(priv_query_executor), | |
828 | SWIGTYPE_p_bt_private_query_executor, 0); | |
829 | if (!py_priv_query_exec_ptr) { | |
830 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
831 | "Failed to create a SWIG pointer object."); | |
832 | goto error; | |
833 | } | |
834 | ||
835 | py_object = SWIG_FromCharPtr(object); | |
836 | if (!py_object) { | |
837 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, | |
838 | "Failed to create a Python string."); | |
839 | goto error; | |
840 | } | |
841 | ||
f6e305ce PP |
842 | /* |
843 | * We don't take any reference on `method_data` which, if not | |
844 | * `NULL`, is assumed to be a `PyObject *`: the user's | |
845 | * _user_query() function will eventually take a reference if | |
846 | * needed. If `method_data` is `NULL`, then we pass `Py_None` as | |
847 | * the initialization's Python object. | |
848 | */ | |
5fa16feb | 849 | py_results_addr = PyObject_CallMethod(py_cls, |
f6e305ce PP |
850 | "_bt_query_from_native", "(OOOO)", py_priv_query_exec_ptr, |
851 | py_object, py_params_ptr, | |
852 | method_data ? method_data : Py_None); | |
5fa16feb | 853 | if (!py_results_addr) { |
27ba49f1 | 854 | status = py_exc_to_status_component_class_clear(self_component_class, |
5fa16feb | 855 | log_level); |
afe3f8ce SM |
856 | if (status < 0) { |
857 | static const char *fmt = | |
858 | "Failed to call Python class's _bt_query_from_native() method: py-cls-addr=%p"; | |
859 | BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG, | |
860 | fmt, py_cls); | |
861 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( | |
862 | self_component_class, fmt, py_cls); | |
863 | } | |
5fa16feb PP |
864 | goto end; |
865 | } | |
866 | ||
867 | /* | |
868 | * The returned object, on success, is an integer object | |
869 | * (PyLong) containing the address of a BT value object (new | |
870 | * reference). | |
871 | */ | |
872 | *result = PyLong_AsVoidPtr(py_results_addr); | |
873 | BT_ASSERT(!PyErr_Occurred()); | |
874 | BT_ASSERT(*result); | |
875 | goto end; | |
876 | ||
877 | error: | |
878 | PyErr_Clear(); | |
879 | status = __BT_FUNC_STATUS_ERROR; | |
880 | ||
881 | end: | |
882 | Py_XDECREF(py_params_ptr); | |
883 | Py_XDECREF(py_priv_query_exec_ptr); | |
884 | Py_XDECREF(py_query_func); | |
885 | Py_XDECREF(py_object); | |
886 | Py_XDECREF(py_results_addr); | |
887 | return status; | |
888 | } | |
889 | ||
890 | static | |
891 | bt_component_class_query_method_status component_class_source_query( | |
892 | bt_self_component_class_source *self_component_class_source, | |
893 | bt_private_query_executor *priv_query_executor, | |
f6e305ce | 894 | const char *object, const bt_value *params, void *method_data, |
5fa16feb PP |
895 | const bt_value **result) |
896 | { | |
897 | const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); | |
898 | const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); | |
899 | bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source); | |
900 | ||
901 | return component_class_query(component_class, self_component_class, | |
f6e305ce | 902 | priv_query_executor, object, params, method_data, result); |
5fa16feb PP |
903 | } |
904 | ||
905 | static | |
906 | bt_component_class_query_method_status component_class_filter_query( | |
907 | bt_self_component_class_filter *self_component_class_filter, | |
908 | bt_private_query_executor *priv_query_executor, | |
f6e305ce | 909 | const char *object, const bt_value *params, void *method_data, |
5fa16feb PP |
910 | const bt_value **result) |
911 | { | |
912 | const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); | |
913 | const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); | |
914 | bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter); | |
915 | ||
916 | return component_class_query(component_class, self_component_class, | |
f6e305ce | 917 | priv_query_executor, object, params, method_data, result); |
5fa16feb PP |
918 | } |
919 | ||
920 | static | |
921 | bt_component_class_query_method_status component_class_sink_query( | |
922 | bt_self_component_class_sink *self_component_class_sink, | |
923 | bt_private_query_executor *priv_query_executor, | |
f6e305ce | 924 | const char *object, const bt_value *params, void *method_data, |
5fa16feb PP |
925 | const bt_value **result) |
926 | { | |
927 | const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); | |
928 | const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); | |
929 | bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink); | |
930 | ||
931 | return component_class_query(component_class, self_component_class, | |
f6e305ce | 932 | priv_query_executor, object, params, method_data, result); |
5fa16feb PP |
933 | } |
934 | ||
935 | static | |
4175c1d5 | 936 | bt_component_class_message_iterator_initialize_method_status |
5fa16feb PP |
937 | component_class_message_iterator_init( |
938 | bt_self_message_iterator *self_message_iterator, | |
9415de1c | 939 | bt_self_message_iterator_configuration *config, |
5fa16feb PP |
940 | bt_self_component *self_component, |
941 | bt_self_component_port_output *self_component_port_output) | |
942 | { | |
4175c1d5 | 943 | bt_component_class_message_iterator_initialize_method_status status = __BT_FUNC_STATUS_OK; |
5fa16feb PP |
944 | PyObject *py_comp_cls = NULL; |
945 | PyObject *py_iter_cls = NULL; | |
946 | PyObject *py_iter_ptr = NULL; | |
9415de1c | 947 | PyObject *py_config_ptr = NULL; |
5fa16feb PP |
948 | PyObject *py_component_port_output_ptr = NULL; |
949 | PyObject *py_init_method_result = NULL; | |
950 | PyObject *py_iter = NULL; | |
951 | PyObject *py_comp; | |
952 | bt_logging_level log_level = get_self_component_log_level( | |
953 | self_component); | |
954 | ||
955 | py_comp = bt_self_component_get_data(self_component); | |
956 | ||
957 | /* Find user's Python message iterator class */ | |
958 | py_comp_cls = PyObject_GetAttrString(py_comp, "__class__"); | |
959 | if (!py_comp_cls) { | |
960 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
961 | "Cannot get Python object's `__class__` attribute."); | |
962 | goto python_error; | |
963 | } | |
964 | ||
965 | py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls"); | |
966 | if (!py_iter_cls) { | |
967 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
968 | "Cannot get Python class's `_iter_cls` attribute."); | |
969 | goto python_error; | |
970 | } | |
971 | ||
972 | py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator), | |
973 | SWIGTYPE_p_bt_self_message_iterator, 0); | |
974 | if (!py_iter_ptr) { | |
975 | const char *err = "Failed to create a SWIG pointer object."; | |
976 | ||
977 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
978 | "%s", err); | |
979 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( | |
980 | self_message_iterator, err); | |
981 | goto error; | |
982 | } | |
983 | ||
984 | /* | |
985 | * Create object with borrowed native message iterator | |
986 | * reference: | |
987 | * | |
988 | * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr) | |
989 | */ | |
990 | py_iter = PyObject_CallMethod(py_iter_cls, "__new__", | |
991 | "(OO)", py_iter_cls, py_iter_ptr); | |
992 | if (!py_iter) { | |
993 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
994 | "Failed to call Python class's __new__() method: " | |
995 | "py-cls-addr=%p", py_iter_cls); | |
996 | goto python_error; | |
997 | } | |
998 | ||
999 | /* | |
1000 | * Initialize object: | |
1001 | * | |
9415de1c | 1002 | * py_iter.__init__(config, self_output_port) |
5fa16feb | 1003 | * |
9415de1c | 1004 | * through the _init_from_native helper static method. |
5fa16feb PP |
1005 | * |
1006 | * At this point, py_iter._ptr is set, so this initialization | |
1007 | * function has access to self._component (which gives it the | |
1008 | * user Python component object from which the iterator was | |
1009 | * created). | |
1010 | */ | |
9415de1c SM |
1011 | py_config_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(config), |
1012 | SWIGTYPE_p_bt_self_message_iterator_configuration, 0); | |
1013 | if (!py_config_ptr) { | |
1014 | const char *err = "Failed to create a SWIG pointer object"; | |
1015 | ||
1016 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
1017 | "%s", err); | |
1018 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( | |
1019 | self_message_iterator, err); | |
1020 | goto error; | |
1021 | } | |
1022 | ||
5fa16feb PP |
1023 | py_component_port_output_ptr = SWIG_NewPointerObj( |
1024 | SWIG_as_voidptr(self_component_port_output), | |
1025 | SWIGTYPE_p_bt_self_component_port_output, 0); | |
1026 | if (!py_component_port_output_ptr) { | |
1027 | const char *err = "Failed to create a SWIG pointer object."; | |
1028 | ||
1029 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
1030 | "%s", err); | |
1031 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( | |
1032 | self_message_iterator, err); | |
1033 | goto error; | |
1034 | } | |
1035 | ||
1036 | py_init_method_result = PyObject_CallMethod(py_iter, | |
9415de1c SM |
1037 | "_bt_init_from_native", "OO", py_config_ptr, |
1038 | py_component_port_output_ptr); | |
5fa16feb PP |
1039 | if (!py_init_method_result) { |
1040 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, | |
1041 | "User's __init__() method failed:"); | |
1042 | goto python_error; | |
1043 | } | |
1044 | ||
1045 | /* | |
1046 | * Since the Python code can never instantiate a user-defined | |
1047 | * message iterator class, the native message iterator | |
1048 | * object does NOT belong to a user Python message iterator | |
1049 | * object (borrowed reference). However this Python object is | |
1050 | * owned by this native message iterator object. | |
1051 | * | |
1052 | * In the Python world, the lifetime of the native message | |
1053 | * iterator is managed by a _GenericMessageIterator | |
1054 | * instance: | |
1055 | * | |
1056 | * _GenericMessageIterator instance: | |
1057 | * owns a native bt_message_iterator object (iter) | |
1058 | * owns a _UserMessageIterator instance (py_iter) | |
1059 | * self._ptr is a borrowed reference to the | |
1060 | * native bt_private_connection_private_message_iterator | |
1061 | * object (iter) | |
1062 | */ | |
1063 | bt_self_message_iterator_set_data(self_message_iterator, py_iter); | |
1064 | py_iter = NULL; | |
1065 | goto end; | |
1066 | ||
1067 | python_error: | |
1068 | /* Handling of errors that cause a Python exception to be set. */ | |
27ba49f1 | 1069 | status = py_exc_to_status_message_iterator_clear(self_message_iterator); |
5fa16feb PP |
1070 | BT_ASSERT(status != __BT_FUNC_STATUS_OK); |
1071 | goto end; | |
1072 | ||
1073 | error: | |
1074 | /* Handling of errors that don't cause a Python exception to be set. */ | |
1075 | status = __BT_FUNC_STATUS_ERROR; | |
1076 | ||
1077 | end: | |
1078 | BT_ASSERT(!PyErr_Occurred()); | |
1079 | ||
1080 | Py_XDECREF(py_comp_cls); | |
1081 | Py_XDECREF(py_iter_cls); | |
1082 | Py_XDECREF(py_iter_ptr); | |
1083 | Py_XDECREF(py_component_port_output_ptr); | |
1084 | Py_XDECREF(py_init_method_result); | |
1085 | Py_XDECREF(py_iter); | |
1086 | return status; | |
1087 | } | |
1088 | ||
1089 | static | |
4175c1d5 | 1090 | bt_component_class_message_iterator_initialize_method_status |
5fa16feb PP |
1091 | component_class_source_message_iterator_init( |
1092 | bt_self_message_iterator *self_message_iterator, | |
9415de1c | 1093 | bt_self_message_iterator_configuration *config, |
5fa16feb PP |
1094 | bt_self_component_source *self_component_source, |
1095 | bt_self_component_port_output *self_component_port_output) | |
1096 | { | |
9415de1c SM |
1097 | bt_self_component *self_component = |
1098 | bt_self_component_source_as_self_component(self_component_source); | |
5fa16feb | 1099 | |
9415de1c SM |
1100 | return component_class_message_iterator_init(self_message_iterator, |
1101 | config, self_component, self_component_port_output); | |
5fa16feb PP |
1102 | } |
1103 | ||
1104 | static | |
4175c1d5 | 1105 | bt_component_class_message_iterator_initialize_method_status |
5fa16feb PP |
1106 | component_class_filter_message_iterator_init( |
1107 | bt_self_message_iterator *self_message_iterator, | |
9415de1c | 1108 | bt_self_message_iterator_configuration *config, |
5fa16feb PP |
1109 | bt_self_component_filter *self_component_filter, |
1110 | bt_self_component_port_output *self_component_port_output) | |
1111 | { | |
9415de1c SM |
1112 | bt_self_component *self_component = |
1113 | bt_self_component_filter_as_self_component(self_component_filter); | |
5fa16feb | 1114 | |
9415de1c SM |
1115 | return component_class_message_iterator_init(self_message_iterator, |
1116 | config, self_component, self_component_port_output); | |
5fa16feb PP |
1117 | } |
1118 | ||
1119 | static | |
1120 | void component_class_message_iterator_finalize( | |
1121 | bt_self_message_iterator *message_iterator) | |
1122 | { | |
1123 | PyObject *py_message_iter = bt_self_message_iterator_get_data( | |
1124 | message_iterator); | |
1125 | PyObject *py_method_result = NULL; | |
1126 | ||
1127 | BT_ASSERT(py_message_iter); | |
1128 | ||
1129 | /* Call user's _user_finalize() method */ | |
1130 | py_method_result = PyObject_CallMethod(py_message_iter, | |
1131 | "_user_finalize", NULL); | |
1132 | ||
1133 | if (PyErr_Occurred()) { | |
1134 | bt_self_component *self_comp = | |
1135 | bt_self_message_iterator_borrow_component( | |
1136 | message_iterator); | |
1137 | bt_logging_level log_level = get_self_component_log_level( | |
1138 | self_comp); | |
1139 | ||
1140 | BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp, | |
1141 | "User's _user_finalize() method raised an exception: ignoring:"); | |
1142 | logw_exception(get_self_message_iterator_log_level( | |
1143 | message_iterator)); | |
1144 | } | |
1145 | ||
1146 | /* | |
1147 | * Ignore any exception raised by the _user_finalize() method | |
1148 | * because it won't change anything at this point: the component | |
1149 | * is being destroyed anyway. | |
1150 | */ | |
1151 | PyErr_Clear(); | |
1152 | Py_XDECREF(py_method_result); | |
1153 | Py_DECREF(py_message_iter); | |
1154 | } | |
1155 | ||
1156 | /* Valid for both sources and filters. */ | |
1157 | ||
1158 | static | |
1159 | bt_component_class_message_iterator_next_method_status | |
1160 | component_class_message_iterator_next( | |
1161 | bt_self_message_iterator *message_iterator, | |
1162 | bt_message_array_const msgs, uint64_t capacity, | |
1163 | uint64_t *count) | |
1164 | { | |
1165 | bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK; | |
1166 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); | |
1167 | PyObject *py_method_result = NULL; | |
1168 | ||
1169 | BT_ASSERT(py_message_iter); | |
1170 | py_method_result = PyObject_CallMethod(py_message_iter, | |
1171 | "_bt_next_from_native", NULL); | |
1172 | if (!py_method_result) { | |
27ba49f1 | 1173 | status = py_exc_to_status_message_iterator_clear(message_iterator); |
5fa16feb PP |
1174 | BT_ASSERT(status != __BT_FUNC_STATUS_OK); |
1175 | goto end; | |
1176 | } | |
1177 | ||
1178 | /* | |
1179 | * The returned object, on success, is an integer object | |
1180 | * (PyLong) containing the address of a native message | |
1181 | * object (which is now ours). | |
1182 | */ | |
1183 | msgs[0] = PyLong_AsVoidPtr(py_method_result); | |
1184 | *count = 1; | |
1185 | ||
1186 | /* Clear potential overflow error; should never happen */ | |
1187 | BT_ASSERT(!PyErr_Occurred()); | |
1188 | goto end; | |
1189 | ||
1190 | end: | |
1191 | Py_XDECREF(py_method_result); | |
1192 | return status; | |
1193 | } | |
1194 | ||
1195 | static | |
1196 | bt_component_class_sink_consume_method_status | |
1197 | component_class_sink_consume(bt_self_component_sink *self_component_sink) | |
1198 | { | |
1199 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
1200 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
1201 | PyObject *py_method_result = NULL; | |
1202 | bt_component_class_sink_consume_method_status status; | |
1203 | ||
1204 | BT_ASSERT(py_comp); | |
1205 | py_method_result = PyObject_CallMethod(py_comp, | |
1206 | "_user_consume", NULL); | |
27ba49f1 | 1207 | status = py_exc_to_status_component_clear(self_component); |
5fa16feb PP |
1208 | BT_ASSERT(py_method_result || status != __BT_FUNC_STATUS_OK); |
1209 | Py_XDECREF(py_method_result); | |
1210 | return status; | |
1211 | } | |
1212 | ||
1213 | static | |
1214 | int component_class_set_help_and_desc( | |
1215 | bt_component_class *component_class, | |
1216 | const char *description, const char *help) | |
1217 | { | |
1218 | int ret; | |
1219 | ||
1220 | if (description) { | |
1221 | ret = bt_component_class_set_description(component_class, description); | |
1222 | if (ret) { | |
1223 | BT_LOGE("Cannot set component class's description: " | |
1224 | "comp-cls-addr=%p", component_class); | |
1225 | goto end; | |
1226 | } | |
1227 | } | |
1228 | ||
1229 | if (help) { | |
1230 | ret = bt_component_class_set_help(component_class, help); | |
1231 | if (ret) { | |
1232 | BT_LOGE("Cannot set component class's help text: " | |
1233 | "comp-cls-addr=%p", component_class); | |
1234 | goto end; | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | ret = 0; | |
1239 | ||
1240 | end: | |
1241 | return ret; | |
1242 | } | |
1243 | ||
1244 | static | |
1245 | bt_component_class_source *bt_bt2_component_class_source_create( | |
1246 | PyObject *py_cls, const char *name, const char *description, | |
1247 | const char *help) | |
1248 | { | |
1249 | bt_component_class_source *component_class_source; | |
1250 | bt_component_class *component_class; | |
1251 | int ret; | |
1252 | ||
1253 | BT_ASSERT(py_cls); | |
1254 | component_class_source = bt_component_class_source_create(name, | |
1255 | component_class_message_iterator_next); | |
1256 | if (!component_class_source) { | |
1257 | BT_LOGE_STR("Cannot create source component class."); | |
1258 | goto end; | |
1259 | } | |
1260 | ||
1261 | component_class = bt_component_class_source_as_component_class(component_class_source); | |
1262 | ||
1263 | if (component_class_set_help_and_desc(component_class, description, help)) { | |
1264 | goto end; | |
1265 | } | |
1266 | ||
4175c1d5 | 1267 | ret = bt_component_class_source_set_initialize_method(component_class_source, component_class_source_init); |
5fa16feb PP |
1268 | BT_ASSERT(ret == 0); |
1269 | ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize); | |
1270 | BT_ASSERT(ret == 0); | |
7f8e969d SM |
1271 | ret = bt_component_class_source_set_message_iterator_seek_beginning_methods(component_class_source, |
1272 | component_class_seek_beginning, component_class_can_seek_beginning); | |
1273 | ret = bt_component_class_source_set_message_iterator_seek_ns_from_origin_methods( | |
1274 | component_class_source, component_class_seek_ns_from_origin, | |
1275 | component_class_can_seek_ns_from_origin); | |
5fa16feb PP |
1276 | BT_ASSERT(ret == 0); |
1277 | ret = bt_component_class_source_set_output_port_connected_method(component_class_source, | |
1278 | component_class_source_output_port_connected); | |
1279 | BT_ASSERT(ret == 0); | |
1280 | ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query); | |
1281 | BT_ASSERT(ret == 0); | |
ed68d910 PP |
1282 | ret = bt_component_class_source_set_get_supported_mip_versions_method(component_class_source, component_class_source_get_supported_mip_versions); |
1283 | BT_ASSERT(ret == 0); | |
4175c1d5 | 1284 | ret = bt_component_class_source_set_message_iterator_initialize_method( |
5fa16feb PP |
1285 | component_class_source, component_class_source_message_iterator_init); |
1286 | BT_ASSERT(ret == 0); | |
1287 | ret = bt_component_class_source_set_message_iterator_finalize_method( | |
1288 | component_class_source, component_class_message_iterator_finalize); | |
1289 | BT_ASSERT(ret == 0); | |
1290 | register_cc_ptr_to_py_cls(component_class, py_cls); | |
1291 | ||
1292 | end: | |
1293 | return component_class_source; | |
1294 | } | |
1295 | ||
1296 | static | |
1297 | bt_component_class_filter *bt_bt2_component_class_filter_create( | |
1298 | PyObject *py_cls, const char *name, const char *description, | |
1299 | const char *help) | |
1300 | { | |
1301 | bt_component_class *component_class; | |
1302 | bt_component_class_filter *component_class_filter; | |
1303 | int ret; | |
1304 | ||
1305 | BT_ASSERT(py_cls); | |
1306 | component_class_filter = bt_component_class_filter_create(name, | |
1307 | component_class_message_iterator_next); | |
1308 | if (!component_class_filter) { | |
1309 | BT_LOGE_STR("Cannot create filter component class."); | |
1310 | goto end; | |
1311 | } | |
1312 | ||
1313 | component_class = bt_component_class_filter_as_component_class(component_class_filter); | |
1314 | ||
1315 | if (component_class_set_help_and_desc(component_class, description, help)) { | |
1316 | goto end; | |
1317 | } | |
1318 | ||
4175c1d5 | 1319 | ret = bt_component_class_filter_set_initialize_method(component_class_filter, component_class_filter_init); |
5fa16feb PP |
1320 | BT_ASSERT(ret == 0); |
1321 | ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize); | |
1322 | BT_ASSERT(ret == 0); | |
7f8e969d SM |
1323 | ret = bt_component_class_filter_set_message_iterator_seek_beginning_methods(component_class_filter, |
1324 | component_class_seek_beginning, component_class_can_seek_beginning); | |
24a68958 | 1325 | BT_ASSERT(ret == 0); |
7f8e969d SM |
1326 | ret = bt_component_class_filter_set_message_iterator_seek_ns_from_origin_methods( |
1327 | component_class_filter, component_class_seek_ns_from_origin, | |
1328 | component_class_can_seek_ns_from_origin); | |
5fa16feb PP |
1329 | ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter, |
1330 | component_class_filter_input_port_connected); | |
1331 | BT_ASSERT(ret == 0); | |
1332 | ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter, | |
1333 | component_class_filter_output_port_connected); | |
1334 | BT_ASSERT(ret == 0); | |
1335 | ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query); | |
1336 | BT_ASSERT(ret == 0); | |
ed68d910 PP |
1337 | ret = bt_component_class_filter_set_get_supported_mip_versions_method(component_class_filter, component_class_filter_get_supported_mip_versions); |
1338 | BT_ASSERT(ret == 0); | |
4175c1d5 | 1339 | ret = bt_component_class_filter_set_message_iterator_initialize_method( |
5fa16feb PP |
1340 | component_class_filter, component_class_filter_message_iterator_init); |
1341 | BT_ASSERT(ret == 0); | |
1342 | ret = bt_component_class_filter_set_message_iterator_finalize_method( | |
1343 | component_class_filter, component_class_message_iterator_finalize); | |
1344 | BT_ASSERT(ret == 0); | |
1345 | register_cc_ptr_to_py_cls(component_class, py_cls); | |
1346 | ||
1347 | end: | |
1348 | return component_class_filter; | |
1349 | } | |
1350 | ||
1351 | static | |
1352 | bt_component_class_sink *bt_bt2_component_class_sink_create( | |
1353 | PyObject *py_cls, const char *name, const char *description, | |
1354 | const char *help) | |
1355 | { | |
1356 | bt_component_class_sink *component_class_sink; | |
1357 | bt_component_class *component_class; | |
1358 | int ret; | |
1359 | ||
1360 | BT_ASSERT(py_cls); | |
1361 | component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume); | |
1362 | ||
1363 | if (!component_class_sink) { | |
1364 | BT_LOGE_STR("Cannot create sink component class."); | |
1365 | goto end; | |
1366 | } | |
1367 | ||
1368 | component_class = bt_component_class_sink_as_component_class(component_class_sink); | |
1369 | ||
1370 | if (component_class_set_help_and_desc(component_class, description, help)) { | |
1371 | goto end; | |
1372 | } | |
1373 | ||
4175c1d5 | 1374 | ret = bt_component_class_sink_set_initialize_method(component_class_sink, component_class_sink_init); |
5fa16feb PP |
1375 | BT_ASSERT(ret == 0); |
1376 | ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize); | |
1377 | BT_ASSERT(ret == 0); | |
1378 | ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink, | |
1379 | component_class_sink_input_port_connected); | |
1380 | BT_ASSERT(ret == 0); | |
1381 | ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink, | |
1382 | component_class_sink_graph_is_configured); | |
1383 | BT_ASSERT(ret == 0); | |
1384 | ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query); | |
1385 | BT_ASSERT(ret == 0); | |
ed68d910 PP |
1386 | ret = bt_component_class_sink_set_get_supported_mip_versions_method(component_class_sink, component_class_sink_get_supported_mip_versions); |
1387 | BT_ASSERT(ret == 0); | |
5fa16feb PP |
1388 | register_cc_ptr_to_py_cls(component_class, py_cls); |
1389 | ||
1390 | end: | |
1391 | return component_class_sink; | |
1392 | } |