bt2: report errors from Python component and component class callbacks
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i
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 <babeltrace2/graph/component-class.h>
26 %include <babeltrace2/graph/component-class-const.h>
27 %include <babeltrace2/graph/component-class-source-const.h>
28 %include <babeltrace2/graph/component-class-source.h>
29 %include <babeltrace2/graph/component-class-filter-const.h>
30 %include <babeltrace2/graph/component-class-filter.h>
31 %include <babeltrace2/graph/component-class-sink-const.h>
32 %include <babeltrace2/graph/component-class-sink.h>
33 %include <babeltrace2/graph/self-component-class-source.h>
34 %include <babeltrace2/graph/self-component-class-filter.h>
35 %include <babeltrace2/graph/self-component-class-sink.h>
36
37 %{
38 /*
39 * This hash table associates a BT component class object address to a
40 * user-defined Python class (PyObject *). The keys and values are NOT
41 * owned by this hash table. The Python class objects are owned by the
42 * Python module, which should not be unloaded until it is not possible
43 * to create a user Python component anyway.
44 *
45 * This hash table is written to when a user-defined Python component
46 * class is created by one of the bt_bt2_component_class_*_create()
47 * functions.
48 *
49 * This function is read from when a user calls bt_component_create()
50 * with a component class pointer created by one of the functions above.
51 * In this case, the original Python class needs to be found to
52 * instantiate it and associate the created Python component object with
53 * a BT component object instance.
54 */
55
56 static GHashTable *bt_cc_ptr_to_py_cls;
57
58 static
59 void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
60 PyObject *py_cls)
61 {
62 if (!bt_cc_ptr_to_py_cls) {
63 /*
64 * Lazy-initializing this GHashTable because GLib
65 * might not be initialized yet and it needs to be
66 * before we call g_hash_table_new()
67 */
68 BT_LOGD_STR("Creating native component class to Python component class hash table.");
69 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
70 BT_ASSERT(bt_cc_ptr_to_py_cls);
71 }
72
73 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
74 (gpointer) py_cls);
75 }
76
77 static
78 PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
79 {
80 if (!bt_cc_ptr_to_py_cls) {
81 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
82 "comp-cls-addr=%p", bt_cc);
83 return NULL;
84 }
85
86 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
87 (gconstpointer) bt_cc);
88 }
89
90
91 /*
92 * Useful Python objects.
93 */
94
95 static PyObject *py_mod_bt2 = NULL;
96 static PyObject *py_mod_bt2_exc_error_type = NULL;
97 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
98 static PyObject *py_mod_bt2_exc_stop_type = NULL;
99 static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL;
100 static PyObject *py_mod_bt2_exc_invalid_object_type = NULL;
101 static PyObject *py_mod_bt2_exc_invalid_params_type = NULL;
102 static PyObject *py_mod_bt2_exc_unsupported_type = NULL;
103
104 static
105 void bt_bt2_cc_init_from_bt2(void)
106 {
107 /*
108 * This is called once the bt2 package is loaded.
109 *
110 * Those modules and functions are needed while the package is
111 * used. Loading them here is safe because we know the bt2
112 * package is imported, and we know that the user cannot use the
113 * code here without importing bt2 first.
114 */
115 py_mod_bt2 = PyImport_ImportModule("bt2");
116 BT_ASSERT(py_mod_bt2);
117 py_mod_bt2_exc_error_type =
118 PyObject_GetAttrString(py_mod_bt2, "Error");
119 BT_ASSERT(py_mod_bt2_exc_error_type);
120 py_mod_bt2_exc_try_again_type =
121 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
122 BT_ASSERT(py_mod_bt2_exc_try_again_type);
123 py_mod_bt2_exc_stop_type =
124 PyObject_GetAttrString(py_mod_bt2, "Stop");
125 BT_ASSERT(py_mod_bt2_exc_stop_type);
126 py_mod_bt2_exc_invalid_object_type =
127 PyObject_GetAttrString(py_mod_bt2, "InvalidObject");
128 BT_ASSERT(py_mod_bt2_exc_invalid_object_type);
129 py_mod_bt2_exc_invalid_params_type =
130 PyObject_GetAttrString(py_mod_bt2, "InvalidParams");
131 BT_ASSERT(py_mod_bt2_exc_invalid_params_type);
132 py_mod_bt2_exc_unsupported_type =
133 PyObject_GetAttrString(py_mod_bt2, "Unsupported");
134 BT_ASSERT(py_mod_bt2_exc_unsupported_type);
135 }
136
137 static
138 void bt_bt2_cc_exit_handler(void)
139 {
140 /*
141 * This is an exit handler (set by the bt2 package).
142 *
143 * We only give back the references that we took in
144 * bt_bt2_cc_init_from_bt2() here. The global variables continue
145 * to exist for the code of this file, but they are now borrowed
146 * references. If this code is executed, it means that somehow
147 * the modules are still loaded, so it should be safe to use
148 * them even without a strong reference.
149 *
150 * We cannot do this in the library's destructor because it
151 * gets executed once Python is already finalized.
152 */
153 Py_XDECREF(py_mod_bt2);
154 Py_XDECREF(py_mod_bt2_exc_error_type);
155 Py_XDECREF(py_mod_bt2_exc_try_again_type);
156 Py_XDECREF(py_mod_bt2_exc_stop_type);
157 Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type);
158 Py_XDECREF(py_mod_bt2_exc_invalid_object_type);
159 Py_XDECREF(py_mod_bt2_exc_invalid_params_type);
160 }
161
162
163 /* Library destructor */
164
165 __attribute__((destructor))
166 static
167 void native_comp_class_dtor(void) {
168 /* Destroy component class association hash table */
169 if (bt_cc_ptr_to_py_cls) {
170 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
171 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
172 }
173 }
174
175 static inline
176 void log_exception_and_maybe_append_error(int log_level,
177 bool append_error,
178 bt_self_component_class *self_component_class,
179 bt_self_component *self_component,
180 bt_self_message_iterator *self_message_iterator)
181 {
182 GString *gstr;
183
184 BT_ASSERT(PyErr_Occurred() != NULL);
185 gstr = bt_py_common_format_exception(BT_LOG_OUTPUT_LEVEL);
186 if (!gstr) {
187 /* bt_py_common_format_exception() logs errors */
188 goto end;
189 }
190
191 BT_LOG_WRITE(log_level, BT_LOG_TAG, "%s", gstr->str);
192
193 if (append_error) {
194 if (self_component_class) {
195 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
196 self_component_class, "%s", gstr->str);
197 } else if (self_component) {
198 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
199 self_component, "%s", gstr->str);
200 } else if (self_message_iterator) {
201 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
202 self_message_iterator, "%s", gstr->str);
203 } else {
204 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
205 "Python", "%s", gstr->str);
206 }
207 }
208
209 end:
210 if (gstr) {
211 g_string_free(gstr, TRUE);
212 }
213 }
214
215 static inline
216 void loge_exception(void)
217 {
218 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL, NULL);
219 }
220
221 static
222 void loge_exception_message_iterator(
223 bt_self_message_iterator *self_message_iterator)
224 {
225 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL, self_message_iterator);
226 }
227
228 static inline
229 void logw_exception(void)
230 {
231 log_exception_and_maybe_append_error(BT_LOG_WARNING, false, NULL, NULL, NULL);
232 }
233
234 static inline
235 int py_exc_to_status(bt_self_component_class *self_component_class,
236 bt_self_component *self_component,
237 bt_self_message_iterator *self_message_iterator)
238 {
239 int status = __BT_FUNC_STATUS_OK;
240 PyObject *exc = PyErr_Occurred();
241
242 if (!exc) {
243 goto end;
244 }
245
246 if (PyErr_GivenExceptionMatches(exc,
247 py_mod_bt2_exc_try_again_type)) {
248 status = __BT_FUNC_STATUS_AGAIN;
249 } else if (PyErr_GivenExceptionMatches(exc,
250 py_mod_bt2_exc_stop_type)) {
251 status = __BT_FUNC_STATUS_END;
252 } else if (PyErr_GivenExceptionMatches(exc,
253 py_mod_bt2_exc_invalid_object_type)) {
254 status = __BT_FUNC_STATUS_INVALID_OBJECT;
255 } else if (PyErr_GivenExceptionMatches(exc,
256 py_mod_bt2_exc_invalid_params_type)) {
257 status = __BT_FUNC_STATUS_INVALID_PARAMS;
258 } else if (PyErr_GivenExceptionMatches(exc,
259 py_mod_bt2_exc_unsupported_type)) {
260 status = __BT_FUNC_STATUS_UNSUPPORTED;
261 } else {
262 /* Unknown exception: convert to general error */
263 log_exception_and_maybe_append_error(BT_LOG_WARNING, true,
264 self_component_class, self_component,
265 self_message_iterator);
266 status = __BT_FUNC_STATUS_ERROR;
267 }
268
269 end:
270 PyErr_Clear();
271 return status;
272 }
273
274 static
275 int py_exc_to_status_component_class(bt_self_component_class *self_component_class)
276 {
277 return py_exc_to_status(self_component_class, NULL, NULL);
278 }
279
280 static
281 int py_exc_to_status_component(bt_self_component *self_component)
282 {
283 return py_exc_to_status(NULL, self_component, NULL);
284 }
285
286 static
287 int py_exc_to_status_message_iterator(
288 bt_self_message_iterator *self_message_iterator)
289 {
290 return py_exc_to_status(NULL, NULL, self_message_iterator);
291 }
292
293 /* Component class proxy methods (delegate to the attached Python object) */
294
295 static
296 bt_component_class_init_method_status component_class_init(
297 bt_self_component *self_component,
298 void *self_component_v,
299 swig_type_info *self_comp_cls_type_swig_type,
300 const bt_value *params,
301 void *init_method_data)
302 {
303 const bt_component *component = bt_self_component_as_component(self_component);
304 const bt_component_class *component_class = bt_component_borrow_class_const(component);
305 bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
306 PyObject *py_cls = NULL;
307 PyObject *py_comp = NULL;
308 PyObject *py_params_ptr = NULL;
309 PyObject *py_comp_ptr = NULL;
310
311 (void) init_method_data;
312
313 BT_ASSERT(self_component);
314 BT_ASSERT(self_component_v);
315 BT_ASSERT(self_comp_cls_type_swig_type);
316
317 /*
318 * Get the user-defined Python class which created this
319 * component's class in the first place (borrowed
320 * reference).
321 */
322 py_cls = lookup_cc_ptr_to_py_cls(component_class);
323 if (!py_cls) {
324 BT_LOGE("Cannot find Python class associated to native component class: "
325 "comp-cls-addr=%p", component_class);
326 goto error;
327 }
328
329 /* Parameters pointer -> SWIG pointer Python object */
330 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
331 SWIGTYPE_p_bt_value, 0);
332 if (!py_params_ptr) {
333 BT_LOGE_STR("Failed to create a SWIG pointer object.");
334 goto error;
335 }
336
337 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
338 self_comp_cls_type_swig_type, 0);
339 if (!py_comp_ptr) {
340 BT_LOGE_STR("Failed to create a SWIG pointer object.");
341 goto error;
342 }
343
344 /*
345 * Do the equivalent of this:
346 *
347 * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr)
348 *
349 * _UserComponentType._bt_init_from_native() calls the Python
350 * component object's __init__() function.
351 */
352 py_comp = PyObject_CallMethod(py_cls,
353 "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
354 if (!py_comp) {
355 BT_LOGW("Failed to call Python class's _bt_init_from_native() method: "
356 "py-cls-addr=%p", py_cls);
357 status = py_exc_to_status_component(self_component);
358 goto end;
359 }
360
361 /*
362 * Our user Python component object is now fully created and
363 * initialized by the user. Since we just created it, this
364 * native component is its only (persistent) owner.
365 */
366 bt_self_component_set_data(self_component, py_comp);
367 py_comp = NULL;
368 goto end;
369
370 error:
371 status = __BT_FUNC_STATUS_ERROR;
372
373 /*
374 * Clear any exception: we're returning a bad status anyway. If
375 * this call originated from Python (creation from a plugin's
376 * component class, for example), then the user gets an
377 * appropriate creation error.
378 */
379 PyErr_Clear();
380
381 end:
382 Py_XDECREF(py_comp);
383 Py_XDECREF(py_params_ptr);
384 Py_XDECREF(py_comp_ptr);
385 return status;
386 }
387
388 /*
389 * Method of bt_component_class_source to initialize a bt_self_component_source
390 * of that class.
391 */
392
393 static
394 bt_component_class_init_method_status component_class_source_init(
395 bt_self_component_source *self_component_source,
396 const bt_value *params, void *init_method_data)
397 {
398 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
399 return component_class_init(
400 self_component,
401 self_component_source,
402 SWIGTYPE_p_bt_self_component_source,
403 params, init_method_data);
404 }
405
406 static
407 bt_component_class_init_method_status component_class_filter_init(
408 bt_self_component_filter *self_component_filter,
409 const bt_value *params, void *init_method_data)
410 {
411 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
412 return component_class_init(
413 self_component,
414 self_component_filter,
415 SWIGTYPE_p_bt_self_component_filter,
416 params, init_method_data);
417 }
418
419 static
420 bt_component_class_init_method_status component_class_sink_init(
421 bt_self_component_sink *self_component_sink,
422 const bt_value *params, void *init_method_data)
423 {
424 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
425 return component_class_init(
426 self_component,
427 self_component_sink,
428 SWIGTYPE_p_bt_self_component_sink,
429 params, init_method_data);
430 }
431
432 static
433 void component_class_finalize(bt_self_component *self_component)
434 {
435 PyObject *py_comp = bt_self_component_get_data(self_component);
436 BT_ASSERT(py_comp);
437
438 /* Call user's _finalize() method */
439 PyObject *py_method_result = PyObject_CallMethod(py_comp,
440 "_finalize", NULL);
441
442 if (PyErr_Occurred()) {
443 BT_LOGW("User component's _finalize() method raised an exception: ignoring:");
444 logw_exception();
445 }
446
447 /*
448 * Ignore any exception raised by the _finalize() method because
449 * it won't change anything at this point: the component is
450 * being destroyed anyway.
451 */
452 PyErr_Clear();
453 Py_XDECREF(py_method_result);
454 Py_DECREF(py_comp);
455 }
456
457 static
458 void component_class_source_finalize(bt_self_component_source *self_component_source)
459 {
460 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
461 component_class_finalize(self_component);
462 }
463
464 static
465 void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
466 {
467 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
468 component_class_finalize(self_component);
469 }
470
471 static
472 void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
473 {
474 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
475 component_class_finalize(self_component);
476 }
477
478 static
479 bt_bool component_class_can_seek_beginning(
480 bt_self_message_iterator *self_message_iterator)
481 {
482 PyObject *py_iter;
483 PyObject *py_result = NULL;
484 bt_bool can_seek_beginning = false;
485
486 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
487 BT_ASSERT(py_iter);
488
489 py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native");
490
491 BT_ASSERT(!py_result || PyBool_Check(py_result));
492
493 if (py_result) {
494 can_seek_beginning = PyObject_IsTrue(py_result);
495 } else {
496 /*
497 * Once can_seek_beginning can report errors, convert the
498 * exception to a status. For now, log and return false;
499 */
500 loge_exception_message_iterator(self_message_iterator);
501 PyErr_Clear();
502 }
503
504 Py_XDECREF(py_result);
505
506 return can_seek_beginning;
507 }
508
509 static
510 bt_component_class_message_iterator_seek_beginning_method_status
511 component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
512 {
513 PyObject *py_iter;
514 PyObject *py_result;
515 bt_component_class_message_iterator_seek_beginning_method_status status;
516
517 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
518 BT_ASSERT(py_iter);
519 py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native",
520 NULL);
521 BT_ASSERT(!py_result || py_result == Py_None);
522 status = py_exc_to_status_message_iterator(self_message_iterator);
523 Py_XDECREF(py_result);
524 return status;
525 }
526
527 static
528 bt_component_class_port_connected_method_status component_class_port_connected(
529 bt_self_component *self_component,
530 void *self_component_port,
531 swig_type_info *self_component_port_swig_type,
532 bt_port_type self_component_port_type,
533 const void *other_port,
534 swig_type_info *other_port_swig_type)
535 {
536 bt_component_class_port_connected_method_status status;
537 PyObject *py_comp = NULL;
538 PyObject *py_self_port_ptr = NULL;
539 PyObject *py_other_port_ptr = NULL;
540 PyObject *py_method_result = NULL;
541
542 py_comp = bt_self_component_get_data(self_component);
543 BT_ASSERT(py_comp);
544 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
545 self_component_port_swig_type, 0);
546 if (!py_self_port_ptr) {
547 BT_LOGF_STR("Failed to create a SWIG pointer object.");
548 status = __BT_FUNC_STATUS_MEMORY_ERROR;
549 goto end;
550 }
551
552 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
553 other_port_swig_type, 0);
554 if (!py_other_port_ptr) {
555 BT_LOGF_STR("Failed to create a SWIG pointer object.");
556 status = __BT_FUNC_STATUS_MEMORY_ERROR;
557 goto end; }
558
559 py_method_result = PyObject_CallMethod(py_comp,
560 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
561 self_component_port_type, py_other_port_ptr);
562 BT_ASSERT(!py_method_result || py_method_result == Py_None);
563 status = py_exc_to_status_component(self_component);
564
565 end:
566 Py_XDECREF(py_self_port_ptr);
567 Py_XDECREF(py_other_port_ptr);
568 Py_XDECREF(py_method_result);
569 return status;
570 }
571
572 static
573 bt_component_class_port_connected_method_status
574 component_class_source_output_port_connected(
575 bt_self_component_source *self_component_source,
576 bt_self_component_port_output *self_component_port_output,
577 const bt_port_input *other_port_input)
578 {
579 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
580
581 return component_class_port_connected(
582 self_component,
583 self_component_port_output,
584 SWIGTYPE_p_bt_self_component_port_output,
585 BT_PORT_TYPE_OUTPUT,
586 other_port_input,
587 SWIGTYPE_p_bt_port_input);
588 }
589
590 static
591 bt_component_class_port_connected_method_status
592 component_class_filter_input_port_connected(
593 bt_self_component_filter *self_component_filter,
594 bt_self_component_port_input *self_component_port_input,
595 const bt_port_output *other_port_output)
596 {
597 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
598
599 return component_class_port_connected(
600 self_component,
601 self_component_port_input,
602 SWIGTYPE_p_bt_self_component_port_input,
603 BT_PORT_TYPE_INPUT,
604 other_port_output,
605 SWIGTYPE_p_bt_port_output);
606 }
607
608 static
609 bt_component_class_port_connected_method_status
610 component_class_filter_output_port_connected(
611 bt_self_component_filter *self_component_filter,
612 bt_self_component_port_output *self_component_port_output,
613 const bt_port_input *other_port_input)
614 {
615 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
616
617 return component_class_port_connected(
618 self_component,
619 self_component_port_output,
620 SWIGTYPE_p_bt_self_component_port_output,
621 BT_PORT_TYPE_OUTPUT,
622 other_port_input,
623 SWIGTYPE_p_bt_port_input);
624 }
625
626 static
627 bt_component_class_port_connected_method_status
628 component_class_sink_input_port_connected(
629 bt_self_component_sink *self_component_sink,
630 bt_self_component_port_input *self_component_port_input,
631 const bt_port_output *other_port_output)
632 {
633 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
634
635 return component_class_port_connected(
636 self_component,
637 self_component_port_input,
638 SWIGTYPE_p_bt_self_component_port_input,
639 BT_PORT_TYPE_INPUT,
640 other_port_output,
641 SWIGTYPE_p_bt_port_output);
642 }
643
644 static
645 bt_component_class_sink_graph_is_configured_method_status
646 component_class_sink_graph_is_configured(
647 bt_self_component_sink *self_component_sink)
648 {
649 PyObject *py_comp = NULL;
650 PyObject *py_method_result = NULL;
651 bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
652 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
653
654 py_comp = bt_self_component_get_data(self_component);
655 py_method_result = PyObject_CallMethod(py_comp,
656 "_bt_graph_is_configured_from_native", NULL);
657 BT_ASSERT(!py_method_result || py_method_result == Py_None);
658 status = py_exc_to_status_component(self_component);
659 Py_XDECREF(py_method_result);
660 return status;
661 }
662
663 static
664 bt_component_class_query_method_status component_class_query(
665 const bt_component_class *component_class,
666 bt_self_component_class *self_component_class,
667 const bt_query_executor *query_executor,
668 const char *object, const bt_value *params,
669 bt_logging_level log_level,
670 const bt_value **result)
671 {
672 PyObject *py_cls = NULL;
673 PyObject *py_params_ptr = NULL;
674 PyObject *py_query_exec_ptr = NULL;
675 PyObject *py_query_func = NULL;
676 PyObject *py_object = NULL;
677 PyObject *py_results_addr = NULL;
678 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
679
680 py_cls = lookup_cc_ptr_to_py_cls(component_class);
681 if (!py_cls) {
682 BT_LOGE("Cannot find Python class associated to native component class: "
683 "comp-cls-addr=%p", component_class);
684 goto error;
685 }
686
687 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
688 SWIGTYPE_p_bt_value, 0);
689 if (!py_params_ptr) {
690 BT_LOGE_STR("Failed to create a SWIG pointer object.");
691 goto error;
692 }
693
694 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
695 SWIGTYPE_p_bt_query_executor, 0);
696 if (!py_query_exec_ptr) {
697 BT_LOGE_STR("Failed to create a SWIG pointer object.");
698 goto error;
699 }
700
701 py_object = SWIG_FromCharPtr(object);
702 if (!py_object) {
703 BT_LOGE_STR("Failed to create a Python string.");
704 goto error;
705 }
706
707 py_results_addr = PyObject_CallMethod(py_cls,
708 "_bt_query_from_native", "(OOOi)", py_query_exec_ptr,
709 py_object, py_params_ptr, (int) log_level);
710 if (!py_results_addr) {
711 BT_LOGW("Failed to call Python class's _bt_query_from_native() method: "
712 "py-cls-addr=%p", py_cls);
713 status = py_exc_to_status_component_class(self_component_class);
714 goto end;
715 }
716
717 /*
718 * The returned object, on success, is an integer object
719 * (PyLong) containing the address of a BT value object (new
720 * reference).
721 */
722 *result = PyLong_AsVoidPtr(py_results_addr);
723 BT_ASSERT(!PyErr_Occurred());
724 BT_ASSERT(*result);
725 goto end;
726
727 error:
728 PyErr_Clear();
729 status = __BT_FUNC_STATUS_ERROR;
730
731 end:
732 Py_XDECREF(py_params_ptr);
733 Py_XDECREF(py_query_exec_ptr);
734 Py_XDECREF(py_query_func);
735 Py_XDECREF(py_object);
736 Py_XDECREF(py_results_addr);
737 return status;
738 }
739
740 static
741 bt_component_class_query_method_status component_class_source_query(
742 bt_self_component_class_source *self_component_class_source,
743 const bt_query_executor *query_executor,
744 const char *object, const bt_value *params,
745 bt_logging_level log_level,
746 const bt_value **result)
747 {
748 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
749 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
750 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
751
752 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
753 }
754
755 static
756 bt_component_class_query_method_status component_class_filter_query(
757 bt_self_component_class_filter *self_component_class_filter,
758 const bt_query_executor *query_executor,
759 const char *object, const bt_value *params,
760 bt_logging_level log_level,
761 const bt_value **result)
762 {
763 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
764 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
765 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
766
767 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
768 }
769
770 static
771 bt_component_class_query_method_status component_class_sink_query(
772 bt_self_component_class_sink *self_component_class_sink,
773 const bt_query_executor *query_executor,
774 const char *object, const bt_value *params,
775 bt_logging_level log_level,
776 const bt_value **result)
777 {
778 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
779 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
780 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
781
782 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
783 }
784
785 static
786 bt_component_class_message_iterator_init_method_status
787 component_class_message_iterator_init(
788 bt_self_message_iterator *self_message_iterator,
789 bt_self_component *self_component,
790 bt_self_component_port_output *self_component_port_output)
791 {
792 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
793 PyObject *py_comp_cls = NULL;
794 PyObject *py_iter_cls = NULL;
795 PyObject *py_iter_ptr = NULL;
796 PyObject *py_component_port_output_ptr = NULL;
797 PyObject *py_init_method_result = NULL;
798 PyObject *py_iter = NULL;
799 PyObject *py_comp;
800
801 py_comp = bt_self_component_get_data(self_component);
802
803 /* Find user's Python message iterator class */
804 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
805 if (!py_comp_cls) {
806 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
807 goto python_error;
808 }
809
810 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
811 if (!py_iter_cls) {
812 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
813 goto python_error;
814 }
815
816 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
817 SWIGTYPE_p_bt_self_message_iterator, 0);
818 if (!py_iter_ptr) {
819 const char *err = "Failed to create a SWIG pointer object.";
820
821 BT_LOGE_STR(err);
822 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
823 self_message_iterator, err);
824 goto error;
825 }
826
827 /*
828 * Create object with borrowed native message iterator
829 * reference:
830 *
831 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
832 */
833 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
834 "(OO)", py_iter_cls, py_iter_ptr);
835 if (!py_iter) {
836 BT_LOGE("Failed to call Python class's __new__() method: "
837 "py-cls-addr=%p", py_iter_cls);
838 goto python_error;
839 }
840
841 /*
842 * Initialize object:
843 *
844 * py_iter.__init__(self_output_port)
845 *
846 * through the _init_for_native helper static method.
847 *
848 * At this point, py_iter._ptr is set, so this initialization
849 * function has access to self._component (which gives it the
850 * user Python component object from which the iterator was
851 * created).
852 */
853 py_component_port_output_ptr = SWIG_NewPointerObj(
854 SWIG_as_voidptr(self_component_port_output),
855 SWIGTYPE_p_bt_self_component_port_output, 0);
856 if (!py_component_port_output_ptr) {
857 const char *err = "Failed to create a SWIG pointer object.";
858
859 BT_LOGE_STR(err);
860 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
861 self_message_iterator, err);
862 goto error;
863 }
864
865 py_init_method_result = PyObject_CallMethod(py_iter,
866 "_bt_init_from_native", "O", py_component_port_output_ptr);
867 if (!py_init_method_result) {
868 BT_LOGE_STR("User's __init__() method failed:");
869 goto python_error;
870 }
871
872 /*
873 * Since the Python code can never instantiate a user-defined
874 * message iterator class, the native message iterator
875 * object does NOT belong to a user Python message iterator
876 * object (borrowed reference). However this Python object is
877 * owned by this native message iterator object.
878 *
879 * In the Python world, the lifetime of the native message
880 * iterator is managed by a _GenericMessageIterator
881 * instance:
882 *
883 * _GenericMessageIterator instance:
884 * owns a native bt_message_iterator object (iter)
885 * owns a _UserMessageIterator instance (py_iter)
886 * self._ptr is a borrowed reference to the
887 * native bt_private_connection_private_message_iterator
888 * object (iter)
889 */
890 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
891 py_iter = NULL;
892 goto end;
893
894 python_error:
895 /* Handling of errors that cause a Python exception to be set. */
896 status = py_exc_to_status_message_iterator(self_message_iterator);
897 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
898 goto end;
899
900 error:
901 /* Handling of errors that don't cause a Python exception to be set. */
902 status = __BT_FUNC_STATUS_ERROR;
903
904 end:
905 BT_ASSERT(!PyErr_Occurred());
906
907 Py_XDECREF(py_comp_cls);
908 Py_XDECREF(py_iter_cls);
909 Py_XDECREF(py_iter_ptr);
910 Py_XDECREF(py_component_port_output_ptr);
911 Py_XDECREF(py_init_method_result);
912 Py_XDECREF(py_iter);
913 return status;
914 }
915
916 static
917 bt_component_class_message_iterator_init_method_status
918 component_class_source_message_iterator_init(
919 bt_self_message_iterator *self_message_iterator,
920 bt_self_component_source *self_component_source,
921 bt_self_component_port_output *self_component_port_output)
922 {
923 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
924
925 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
926 }
927
928 static
929 bt_component_class_message_iterator_init_method_status
930 component_class_filter_message_iterator_init(
931 bt_self_message_iterator *self_message_iterator,
932 bt_self_component_filter *self_component_filter,
933 bt_self_component_port_output *self_component_port_output)
934 {
935 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
936
937 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
938 }
939
940 static
941 void component_class_message_iterator_finalize(
942 bt_self_message_iterator *message_iterator)
943 {
944 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
945 PyObject *py_method_result = NULL;
946
947 BT_ASSERT(py_message_iter);
948
949 /* Call user's _finalize() method */
950 py_method_result = PyObject_CallMethod(py_message_iter,
951 "_finalize", NULL);
952
953 if (PyErr_Occurred()) {
954 BT_LOGW("User's _finalize() method raised an exception: ignoring:");
955 logw_exception();
956 }
957
958 /*
959 * Ignore any exception raised by the _finalize() method because
960 * it won't change anything at this point: the component is
961 * being destroyed anyway.
962 */
963 PyErr_Clear();
964 Py_XDECREF(py_method_result);
965 Py_DECREF(py_message_iter);
966 }
967
968 /* Valid for both sources and filters. */
969
970 static
971 bt_component_class_message_iterator_next_method_status
972 component_class_message_iterator_next(
973 bt_self_message_iterator *message_iterator,
974 bt_message_array_const msgs, uint64_t capacity,
975 uint64_t *count)
976 {
977 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
978 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
979 PyObject *py_method_result = NULL;
980
981 BT_ASSERT(py_message_iter);
982 py_method_result = PyObject_CallMethod(py_message_iter,
983 "_bt_next_from_native", NULL);
984 if (!py_method_result) {
985 status = py_exc_to_status_message_iterator(message_iterator);
986 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
987 goto end;
988 }
989
990 /*
991 * The returned object, on success, is an integer object
992 * (PyLong) containing the address of a native message
993 * object (which is now ours).
994 */
995 msgs[0] = PyLong_AsVoidPtr(py_method_result);
996 *count = 1;
997
998 /* Clear potential overflow error; should never happen */
999 BT_ASSERT(!PyErr_Occurred());
1000 goto end;
1001
1002 end:
1003 Py_XDECREF(py_method_result);
1004 return status;
1005 }
1006
1007 static
1008 bt_component_class_sink_consume_method_status
1009 component_class_sink_consume(bt_self_component_sink *self_component_sink)
1010 {
1011 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1012 PyObject *py_comp = bt_self_component_get_data(self_component);
1013 PyObject *py_method_result = NULL;
1014 bt_component_class_sink_consume_method_status status;
1015
1016 BT_ASSERT(py_comp);
1017 py_method_result = PyObject_CallMethod(py_comp,
1018 "_consume", NULL);
1019 status = py_exc_to_status_component(self_component);
1020 if (!py_method_result && status == __BT_FUNC_STATUS_OK) {
1021 /* Pretty sure this should never happen, but just in case */
1022 BT_LOGE("User's _consume() method failed without raising an exception: "
1023 "status=%d", status);
1024 status = __BT_FUNC_STATUS_ERROR;
1025 }
1026
1027 Py_XDECREF(py_method_result);
1028 return status;
1029 }
1030
1031 static
1032 int component_class_set_help_and_desc(
1033 bt_component_class *component_class,
1034 const char *description, const char *help)
1035 {
1036 int ret;
1037
1038 if (description) {
1039 ret = bt_component_class_set_description(component_class, description);
1040 if (ret) {
1041 BT_LOGE("Cannot set component class's description: "
1042 "comp-cls-addr=%p", component_class);
1043 goto end;
1044 }
1045 }
1046
1047 if (help) {
1048 ret = bt_component_class_set_help(component_class, help);
1049 if (ret) {
1050 BT_LOGE("Cannot set component class's help text: "
1051 "comp-cls-addr=%p", component_class);
1052 goto end;
1053 }
1054 }
1055
1056 ret = 0;
1057
1058 end:
1059 return ret;
1060 }
1061
1062 static
1063 bt_component_class_source *bt_bt2_component_class_source_create(
1064 PyObject *py_cls, const char *name, const char *description,
1065 const char *help)
1066 {
1067 bt_component_class_source *component_class_source;
1068 bt_component_class *component_class;
1069 int ret;
1070
1071 BT_ASSERT(py_cls);
1072 component_class_source = bt_component_class_source_create(name,
1073 component_class_message_iterator_next);
1074 if (!component_class_source) {
1075 BT_LOGE_STR("Cannot create source component class.");
1076 goto end;
1077 }
1078
1079 component_class = bt_component_class_source_as_component_class(component_class_source);
1080
1081 if (component_class_set_help_and_desc(component_class, description, help)) {
1082 goto end;
1083 }
1084
1085 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
1086 BT_ASSERT(ret == 0);
1087 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
1088 BT_ASSERT(ret == 0);
1089 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
1090 component_class_can_seek_beginning);
1091 BT_ASSERT(ret == 0);
1092 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
1093 component_class_seek_beginning);
1094 BT_ASSERT(ret == 0);
1095 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1096 component_class_source_output_port_connected);
1097 BT_ASSERT(ret == 0);
1098 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
1099 BT_ASSERT(ret == 0);
1100 ret = bt_component_class_source_set_message_iterator_init_method(
1101 component_class_source, component_class_source_message_iterator_init);
1102 BT_ASSERT(ret == 0);
1103 ret = bt_component_class_source_set_message_iterator_finalize_method(
1104 component_class_source, component_class_message_iterator_finalize);
1105 BT_ASSERT(ret == 0);
1106 register_cc_ptr_to_py_cls(component_class, py_cls);
1107
1108 end:
1109 return component_class_source;
1110 }
1111
1112 static
1113 bt_component_class_filter *bt_bt2_component_class_filter_create(
1114 PyObject *py_cls, const char *name, const char *description,
1115 const char *help)
1116 {
1117 bt_component_class *component_class;
1118 bt_component_class_filter *component_class_filter;
1119 int ret;
1120
1121 BT_ASSERT(py_cls);
1122 component_class_filter = bt_component_class_filter_create(name,
1123 component_class_message_iterator_next);
1124 if (!component_class_filter) {
1125 BT_LOGE_STR("Cannot create filter component class.");
1126 goto end;
1127 }
1128
1129 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1130
1131 if (component_class_set_help_and_desc(component_class, description, help)) {
1132 goto end;
1133 }
1134
1135 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
1136 BT_ASSERT(ret == 0);
1137 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
1138 BT_ASSERT(ret == 0);
1139 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
1140 component_class_can_seek_beginning);
1141 BT_ASSERT(ret == 0);
1142 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
1143 component_class_seek_beginning);
1144 BT_ASSERT(ret == 0);
1145 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1146 component_class_filter_input_port_connected);
1147 BT_ASSERT(ret == 0);
1148 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1149 component_class_filter_output_port_connected);
1150 BT_ASSERT(ret == 0);
1151 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
1152 BT_ASSERT(ret == 0);
1153 ret = bt_component_class_filter_set_message_iterator_init_method(
1154 component_class_filter, component_class_filter_message_iterator_init);
1155 BT_ASSERT(ret == 0);
1156 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1157 component_class_filter, component_class_message_iterator_finalize);
1158 BT_ASSERT(ret == 0);
1159 register_cc_ptr_to_py_cls(component_class, py_cls);
1160
1161 end:
1162 return component_class_filter;
1163 }
1164
1165 static
1166 bt_component_class_sink *bt_bt2_component_class_sink_create(
1167 PyObject *py_cls, const char *name, const char *description,
1168 const char *help)
1169 {
1170 bt_component_class_sink *component_class_sink;
1171 bt_component_class *component_class;
1172 int ret;
1173
1174 BT_ASSERT(py_cls);
1175 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
1176
1177 if (!component_class_sink) {
1178 BT_LOGE_STR("Cannot create sink component class.");
1179 goto end;
1180 }
1181
1182 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1183
1184 if (component_class_set_help_and_desc(component_class, description, help)) {
1185 goto end;
1186 }
1187
1188 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
1189 BT_ASSERT(ret == 0);
1190 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
1191 BT_ASSERT(ret == 0);
1192 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1193 component_class_sink_input_port_connected);
1194 BT_ASSERT(ret == 0);
1195 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1196 component_class_sink_graph_is_configured);
1197 BT_ASSERT(ret == 0);
1198 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
1199 BT_ASSERT(ret == 0);
1200 register_cc_ptr_to_py_cls(component_class, py_cls);
1201
1202 end:
1203 return component_class_sink;
1204 }
1205 %}
1206
1207 struct bt_component_class_source *bt_bt2_component_class_source_create(
1208 PyObject *py_cls, const char *name, const char *description,
1209 const char *help);
1210 struct bt_component_class_filter *bt_bt2_component_class_filter_create(
1211 PyObject *py_cls, const char *name, const char *description,
1212 const char *help);
1213 struct bt_component_class_sink *bt_bt2_component_class_sink_create(
1214 PyObject *py_cls, const char *name, const char *description,
1215 const char *help);
1216 void bt_bt2_cc_init_from_bt2(void);
1217 void bt_bt2_cc_exit_handler(void);
This page took 0.078064 seconds and 4 git commands to generate.