lib: strictly type function return status enumerations
[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(int log_level)
177 {
178 GString *gstr;
179
180 BT_ASSERT(PyErr_Occurred() != NULL);
181 gstr = bt_py_common_format_exception(BT_LOG_OUTPUT_LEVEL);
182 if (!gstr) {
183 /* bt_py_common_format_exception() logs errors */
184 goto end;
185 }
186
187 BT_LOG_WRITE(log_level, BT_LOG_TAG, "%s", gstr->str);
188
189 end:
190 if (gstr) {
191 g_string_free(gstr, TRUE);
192 }
193 }
194
195 static inline
196 void loge_exception(void)
197 {
198 log_exception(BT_LOG_ERROR);
199 }
200
201 static inline
202 void logw_exception(void)
203 {
204 log_exception(BT_LOG_WARN);
205 }
206
207 static inline
208 int py_exc_to_status(void)
209 {
210 int status = __BT_FUNC_STATUS_OK;
211 PyObject *exc = PyErr_Occurred();
212
213 if (!exc) {
214 goto end;
215 }
216
217 if (PyErr_GivenExceptionMatches(exc,
218 py_mod_bt2_exc_try_again_type)) {
219 status = __BT_FUNC_STATUS_AGAIN;
220 } else if (PyErr_GivenExceptionMatches(exc,
221 py_mod_bt2_exc_stop_type)) {
222 status = __BT_FUNC_STATUS_END;
223 } else if (PyErr_GivenExceptionMatches(exc,
224 py_mod_bt2_exc_invalid_object_type)) {
225 status = __BT_FUNC_STATUS_INVALID_OBJECT;
226 } else if (PyErr_GivenExceptionMatches(exc,
227 py_mod_bt2_exc_invalid_params_type)) {
228 status = __BT_FUNC_STATUS_INVALID_PARAMS;
229 } else if (PyErr_GivenExceptionMatches(exc,
230 py_mod_bt2_exc_unsupported_type)) {
231 status = __BT_FUNC_STATUS_UNSUPPORTED;
232 } else {
233 /* Unknown exception: convert to general error */
234 logw_exception();
235 status = __BT_FUNC_STATUS_ERROR;
236 }
237
238 end:
239 PyErr_Clear();
240 return status;
241 }
242
243 /* Component class proxy methods (delegate to the attached Python object) */
244
245 static
246 bt_component_class_init_method_status component_class_init(
247 bt_self_component *self_component,
248 void *self_component_v,
249 swig_type_info *self_comp_cls_type_swig_type,
250 const bt_value *params,
251 void *init_method_data)
252 {
253 const bt_component *component = bt_self_component_as_component(self_component);
254 const bt_component_class *component_class = bt_component_borrow_class_const(component);
255 bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
256 PyObject *py_cls = NULL;
257 PyObject *py_comp = NULL;
258 PyObject *py_params_ptr = NULL;
259 PyObject *py_comp_ptr = NULL;
260
261 (void) init_method_data;
262
263 BT_ASSERT(self_component);
264 BT_ASSERT(self_component_v);
265 BT_ASSERT(self_comp_cls_type_swig_type);
266
267 /*
268 * Get the user-defined Python class which created this
269 * component's class in the first place (borrowed
270 * reference).
271 */
272 py_cls = lookup_cc_ptr_to_py_cls(component_class);
273 if (!py_cls) {
274 BT_LOGE("Cannot find Python class associated to native component class: "
275 "comp-cls-addr=%p", component_class);
276 goto error;
277 }
278
279 /* Parameters pointer -> SWIG pointer Python object */
280 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
281 SWIGTYPE_p_bt_value, 0);
282 if (!py_params_ptr) {
283 BT_LOGE_STR("Failed to create a SWIG pointer object.");
284 goto error;
285 }
286
287 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
288 self_comp_cls_type_swig_type, 0);
289 if (!py_comp_ptr) {
290 BT_LOGE_STR("Failed to create a SWIG pointer object.");
291 goto error;
292 }
293
294 /*
295 * Do the equivalent of this:
296 *
297 * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr)
298 *
299 * _UserComponentType._init_from_native() calls the Python
300 * component object's __init__() function.
301 */
302 py_comp = PyObject_CallMethod(py_cls,
303 "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
304 if (!py_comp) {
305 BT_LOGW("Failed to call Python class's _init_from_native() method: "
306 "py-cls-addr=%p", py_cls);
307 logw_exception();
308 goto error;
309 }
310
311 /*
312 * Our user Python component object is now fully created and
313 * initialized by the user. Since we just created it, this
314 * native component is its only (persistent) owner.
315 */
316 bt_self_component_set_data(self_component, py_comp);
317 py_comp = NULL;
318 goto end;
319
320 error:
321 status = __BT_FUNC_STATUS_ERROR;
322
323 /*
324 * Clear any exception: we're returning a bad status anyway. If
325 * this call originated from Python (creation from a plugin's
326 * component class, for example), then the user gets an
327 * appropriate creation error.
328 */
329 PyErr_Clear();
330
331 end:
332 Py_XDECREF(py_comp);
333 Py_XDECREF(py_params_ptr);
334 Py_XDECREF(py_comp_ptr);
335 return status;
336 }
337
338 /*
339 * Method of bt_component_class_source to initialize a bt_self_component_source
340 * of that class.
341 */
342
343 static
344 bt_component_class_init_method_status component_class_source_init(
345 bt_self_component_source *self_component_source,
346 const bt_value *params, void *init_method_data)
347 {
348 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
349 return component_class_init(
350 self_component,
351 self_component_source,
352 SWIGTYPE_p_bt_self_component_source,
353 params, init_method_data);
354 }
355
356 static
357 bt_component_class_init_method_status component_class_filter_init(
358 bt_self_component_filter *self_component_filter,
359 const bt_value *params, void *init_method_data)
360 {
361 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
362 return component_class_init(
363 self_component,
364 self_component_filter,
365 SWIGTYPE_p_bt_self_component_filter,
366 params, init_method_data);
367 }
368
369 static
370 bt_component_class_init_method_status component_class_sink_init(
371 bt_self_component_sink *self_component_sink,
372 const bt_value *params, void *init_method_data)
373 {
374 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
375 return component_class_init(
376 self_component,
377 self_component_sink,
378 SWIGTYPE_p_bt_self_component_sink,
379 params, init_method_data);
380 }
381
382 static
383 void component_class_finalize(bt_self_component *self_component)
384 {
385 PyObject *py_comp = bt_self_component_get_data(self_component);
386 BT_ASSERT(py_comp);
387
388 /* Call user's _finalize() method */
389 PyObject *py_method_result = PyObject_CallMethod(py_comp,
390 "_finalize", NULL);
391
392 if (PyErr_Occurred()) {
393 BT_LOGW("User component's _finalize() method raised an exception: ignoring:");
394 logw_exception();
395 }
396
397 /*
398 * Ignore any exception raised by the _finalize() method because
399 * it won't change anything at this point: the component is
400 * being destroyed anyway.
401 */
402 PyErr_Clear();
403 Py_XDECREF(py_method_result);
404 Py_DECREF(py_comp);
405 }
406
407 static
408 void component_class_source_finalize(bt_self_component_source *self_component_source)
409 {
410 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
411 component_class_finalize(self_component);
412 }
413
414 static
415 void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
416 {
417 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
418 component_class_finalize(self_component);
419 }
420
421 static
422 void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
423 {
424 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
425 component_class_finalize(self_component);
426 }
427
428 static
429 bt_bool component_class_can_seek_beginning(
430 bt_self_message_iterator *self_message_iterator)
431 {
432 PyObject *py_iter;
433 PyObject *py_result = NULL;
434 bt_bool can_seek_beginning = false;
435
436 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
437 BT_ASSERT(py_iter);
438
439 py_result = PyObject_GetAttrString(py_iter, "_can_seek_beginning_from_native");
440
441 BT_ASSERT(!py_result || PyBool_Check(py_result));
442
443 if (py_result) {
444 can_seek_beginning = PyObject_IsTrue(py_result);
445 } else {
446 /*
447 * Once can_seek_beginning can report errors, convert the
448 * exception to a status. For now, log and return false;
449 */
450 loge_exception();
451 PyErr_Clear();
452 }
453
454 Py_XDECREF(py_result);
455
456 return can_seek_beginning;
457 }
458
459 static
460 bt_component_class_message_iterator_seek_beginning_method_status
461 component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
462 {
463 PyObject *py_iter;
464 PyObject *py_result;
465 bt_component_class_message_iterator_seek_beginning_method_status status;
466
467 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
468 BT_ASSERT(py_iter);
469 py_result = PyObject_CallMethod(py_iter, "_seek_beginning_from_native",
470 NULL);
471 BT_ASSERT(!py_result || py_result == Py_None);
472 status = py_exc_to_status();
473 Py_XDECREF(py_result);
474 return status;
475 }
476
477 static
478 bt_component_class_port_connected_method_status component_class_port_connected(
479 bt_self_component *self_component,
480 void *self_component_port,
481 swig_type_info *self_component_port_swig_type,
482 bt_port_type self_component_port_type,
483 const void *other_port,
484 swig_type_info *other_port_swig_type)
485 {
486 bt_component_class_port_connected_method_status status;
487 PyObject *py_comp = NULL;
488 PyObject *py_self_port_ptr = NULL;
489 PyObject *py_other_port_ptr = NULL;
490 PyObject *py_method_result = NULL;
491
492 py_comp = bt_self_component_get_data(self_component);
493 BT_ASSERT(py_comp);
494 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
495 self_component_port_swig_type, 0);
496 if (!py_self_port_ptr) {
497 BT_LOGF_STR("Failed to create a SWIG pointer object.");
498 status = __BT_FUNC_STATUS_MEMORY_ERROR;
499 goto end;
500 }
501
502 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
503 other_port_swig_type, 0);
504 if (!py_other_port_ptr) {
505 BT_LOGF_STR("Failed to create a SWIG pointer object.");
506 status = __BT_FUNC_STATUS_MEMORY_ERROR;
507 goto end; }
508
509 py_method_result = PyObject_CallMethod(py_comp,
510 "_port_connected_from_native", "(OiO)", py_self_port_ptr,
511 self_component_port_type, py_other_port_ptr);
512 BT_ASSERT(!py_method_result || py_method_result == Py_None);
513 status = py_exc_to_status();
514
515 end:
516 Py_XDECREF(py_self_port_ptr);
517 Py_XDECREF(py_other_port_ptr);
518 Py_XDECREF(py_method_result);
519 return status;
520 }
521
522 static
523 bt_component_class_port_connected_method_status
524 component_class_source_output_port_connected(
525 bt_self_component_source *self_component_source,
526 bt_self_component_port_output *self_component_port_output,
527 const bt_port_input *other_port_input)
528 {
529 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
530
531 return component_class_port_connected(
532 self_component,
533 self_component_port_output,
534 SWIGTYPE_p_bt_self_component_port_output,
535 BT_PORT_TYPE_OUTPUT,
536 other_port_input,
537 SWIGTYPE_p_bt_port_input);
538 }
539
540 static
541 bt_component_class_port_connected_method_status
542 component_class_filter_input_port_connected(
543 bt_self_component_filter *self_component_filter,
544 bt_self_component_port_input *self_component_port_input,
545 const bt_port_output *other_port_output)
546 {
547 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
548
549 return component_class_port_connected(
550 self_component,
551 self_component_port_input,
552 SWIGTYPE_p_bt_self_component_port_input,
553 BT_PORT_TYPE_INPUT,
554 other_port_output,
555 SWIGTYPE_p_bt_port_output);
556 }
557
558 static
559 bt_component_class_port_connected_method_status
560 component_class_filter_output_port_connected(
561 bt_self_component_filter *self_component_filter,
562 bt_self_component_port_output *self_component_port_output,
563 const bt_port_input *other_port_input)
564 {
565 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
566
567 return component_class_port_connected(
568 self_component,
569 self_component_port_output,
570 SWIGTYPE_p_bt_self_component_port_output,
571 BT_PORT_TYPE_OUTPUT,
572 other_port_input,
573 SWIGTYPE_p_bt_port_input);
574 }
575
576 static
577 bt_component_class_port_connected_method_status
578 component_class_sink_input_port_connected(
579 bt_self_component_sink *self_component_sink,
580 bt_self_component_port_input *self_component_port_input,
581 const bt_port_output *other_port_output)
582 {
583 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
584
585 return component_class_port_connected(
586 self_component,
587 self_component_port_input,
588 SWIGTYPE_p_bt_self_component_port_input,
589 BT_PORT_TYPE_INPUT,
590 other_port_output,
591 SWIGTYPE_p_bt_port_output);
592 }
593
594 static
595 bt_component_class_sink_graph_is_configured_method_status
596 component_class_sink_graph_is_configured(
597 bt_self_component_sink *self_component_sink)
598 {
599 PyObject *py_comp = NULL;
600 PyObject *py_method_result = NULL;
601 bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
602 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
603
604 py_comp = bt_self_component_get_data(self_component);
605 py_method_result = PyObject_CallMethod(py_comp,
606 "_graph_is_configured_from_native", NULL);
607 BT_ASSERT(!py_method_result || py_method_result == Py_None);
608 status = py_exc_to_status();
609 Py_XDECREF(py_method_result);
610 return status;
611 }
612
613 static
614 bt_component_class_query_method_status component_class_query(
615 const bt_component_class *component_class,
616 const bt_query_executor *query_executor,
617 const char *object, const bt_value *params,
618 bt_logging_level log_level,
619 const bt_value **result)
620 {
621 PyObject *py_cls = NULL;
622 PyObject *py_params_ptr = NULL;
623 PyObject *py_query_exec_ptr = NULL;
624 PyObject *py_query_func = NULL;
625 PyObject *py_object = NULL;
626 PyObject *py_results_addr = NULL;
627 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
628
629 py_cls = lookup_cc_ptr_to_py_cls(component_class);
630 if (!py_cls) {
631 BT_LOGE("Cannot find Python class associated to native component class: "
632 "comp-cls-addr=%p", component_class);
633 goto error;
634 }
635
636 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
637 SWIGTYPE_p_bt_value, 0);
638 if (!py_params_ptr) {
639 BT_LOGE_STR("Failed to create a SWIG pointer object.");
640 goto error;
641 }
642
643 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
644 SWIGTYPE_p_bt_query_executor, 0);
645 if (!py_query_exec_ptr) {
646 BT_LOGE_STR("Failed to create a SWIG pointer object.");
647 goto error;
648 }
649
650 py_object = SWIG_FromCharPtr(object);
651 if (!py_object) {
652 BT_LOGE_STR("Failed to create a Python string.");
653 goto error;
654 }
655
656 py_results_addr = PyObject_CallMethod(py_cls,
657 "_query_from_native", "(OOOi)", py_query_exec_ptr,
658 py_object, py_params_ptr, (int) log_level);
659 if (!py_results_addr) {
660 BT_LOGW("Failed to call Python class's _query_from_native() method: "
661 "py-cls-addr=%p", py_cls);
662 status = py_exc_to_status();
663 goto end;
664 }
665
666 /*
667 * The returned object, on success, is an integer object
668 * (PyLong) containing the address of a BT value object (new
669 * reference).
670 */
671 *result = PyLong_AsVoidPtr(py_results_addr);
672 BT_ASSERT(!PyErr_Occurred());
673 BT_ASSERT(*result);
674 goto end;
675
676 error:
677 PyErr_Clear();
678 status = __BT_FUNC_STATUS_ERROR;
679
680 end:
681 Py_XDECREF(py_params_ptr);
682 Py_XDECREF(py_query_exec_ptr);
683 Py_XDECREF(py_query_func);
684 Py_XDECREF(py_object);
685 Py_XDECREF(py_results_addr);
686 return status;
687 }
688
689 static
690 bt_component_class_query_method_status component_class_source_query(
691 bt_self_component_class_source *self_component_class_source,
692 const bt_query_executor *query_executor,
693 const char *object, const bt_value *params,
694 bt_logging_level log_level,
695 const bt_value **result)
696 {
697 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
698 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
699
700 return component_class_query(component_class, query_executor, object, params, log_level, result);
701 }
702
703 static
704 bt_component_class_query_method_status component_class_filter_query(
705 bt_self_component_class_filter *self_component_class_filter,
706 const bt_query_executor *query_executor,
707 const char *object, const bt_value *params,
708 bt_logging_level log_level,
709 const bt_value **result)
710 {
711 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
712 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
713
714 return component_class_query(component_class, query_executor, object, params, log_level, result);
715 }
716
717 static
718 bt_component_class_query_method_status component_class_sink_query(
719 bt_self_component_class_sink *self_component_class_sink,
720 const bt_query_executor *query_executor,
721 const char *object, const bt_value *params,
722 bt_logging_level log_level,
723 const bt_value **result)
724 {
725 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
726 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
727
728 return component_class_query(component_class, query_executor, object, params, log_level, result);
729 }
730
731 static
732 bt_component_class_message_iterator_init_method_status
733 component_class_message_iterator_init(
734 bt_self_message_iterator *self_message_iterator,
735 bt_self_component *self_component,
736 bt_self_component_port_output *self_component_port_output)
737 {
738 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
739 PyObject *py_comp_cls = NULL;
740 PyObject *py_iter_cls = NULL;
741 PyObject *py_iter_ptr = NULL;
742 PyObject *py_component_port_output_ptr = NULL;
743 PyObject *py_init_method_result = NULL;
744 PyObject *py_iter = NULL;
745 PyObject *py_comp;
746
747 py_comp = bt_self_component_get_data(self_component);
748
749 /* Find user's Python message iterator class */
750 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
751 if (!py_comp_cls) {
752 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
753 goto error;
754 }
755
756 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
757 if (!py_iter_cls) {
758 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
759 goto error;
760 }
761
762 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
763 SWIGTYPE_p_bt_self_message_iterator, 0);
764 if (!py_iter_ptr) {
765 BT_LOGE_STR("Failed to create a SWIG pointer object.");
766 goto error;
767 }
768
769 /*
770 * Create object with borrowed native message iterator
771 * reference:
772 *
773 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
774 */
775 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
776 "(OO)", py_iter_cls, py_iter_ptr);
777 if (!py_iter) {
778 BT_LOGE("Failed to call Python class's __new__() method: "
779 "py-cls-addr=%p", py_iter_cls);
780 loge_exception();
781 goto error;
782 }
783
784 /*
785 * Initialize object:
786 *
787 * py_iter.__init__(self_output_port)
788 *
789 * through the _init_for_native helper static method.
790 *
791 * At this point, py_iter._ptr is set, so this initialization
792 * function has access to self._component (which gives it the
793 * user Python component object from which the iterator was
794 * created).
795 */
796 py_component_port_output_ptr = SWIG_NewPointerObj(
797 SWIG_as_voidptr(self_component_port_output),
798 SWIGTYPE_p_bt_self_component_port_output, 0);
799 if (!py_component_port_output_ptr) {
800 BT_LOGE_STR("Failed to create a SWIG pointer object.");
801 goto error;
802 }
803
804 py_init_method_result = PyObject_CallMethod(py_iter,
805 "_init_from_native", "O", py_component_port_output_ptr);
806 if (!py_init_method_result) {
807 BT_LOGW_STR("User's __init__() method failed:");
808 logw_exception();
809 goto error;
810 }
811
812 /*
813 * Since the Python code can never instantiate a user-defined
814 * message iterator class, the native message iterator
815 * object does NOT belong to a user Python message iterator
816 * object (borrowed reference). However this Python object is
817 * owned by this native message iterator object.
818 *
819 * In the Python world, the lifetime of the native message
820 * iterator is managed by a _GenericMessageIterator
821 * instance:
822 *
823 * _GenericMessageIterator instance:
824 * owns a native bt_message_iterator object (iter)
825 * owns a _UserMessageIterator instance (py_iter)
826 * self._ptr is a borrowed reference to the
827 * native bt_private_connection_private_message_iterator
828 * object (iter)
829 */
830 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
831 py_iter = NULL;
832 goto end;
833
834 error:
835 status = py_exc_to_status();
836 if (status == __BT_FUNC_STATUS_OK) {
837 /*
838 * Looks like there wasn't any exception from the Python
839 * side, but we're still in an error state here.
840 */
841 status = __BT_FUNC_STATUS_ERROR;
842 }
843
844 /*
845 * Clear any exception: we're returning a bad status anyway. If
846 * this call originated from Python, then the user gets an
847 * appropriate creation error.
848 */
849 PyErr_Clear();
850
851 end:
852 Py_XDECREF(py_comp_cls);
853 Py_XDECREF(py_iter_cls);
854 Py_XDECREF(py_iter_ptr);
855 Py_XDECREF(py_component_port_output_ptr);
856 Py_XDECREF(py_init_method_result);
857 Py_XDECREF(py_iter);
858 return status;
859 }
860
861 static
862 bt_component_class_message_iterator_init_method_status
863 component_class_source_message_iterator_init(
864 bt_self_message_iterator *self_message_iterator,
865 bt_self_component_source *self_component_source,
866 bt_self_component_port_output *self_component_port_output)
867 {
868 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
869
870 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
871 }
872
873 static
874 bt_component_class_message_iterator_init_method_status
875 component_class_filter_message_iterator_init(
876 bt_self_message_iterator *self_message_iterator,
877 bt_self_component_filter *self_component_filter,
878 bt_self_component_port_output *self_component_port_output)
879 {
880 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
881
882 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
883 }
884
885 static
886 void component_class_message_iterator_finalize(
887 bt_self_message_iterator *message_iterator)
888 {
889 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
890 PyObject *py_method_result = NULL;
891
892 BT_ASSERT(py_message_iter);
893
894 /* Call user's _finalize() method */
895 py_method_result = PyObject_CallMethod(py_message_iter,
896 "_finalize", NULL);
897
898 if (PyErr_Occurred()) {
899 BT_LOGW("User's _finalize() method raised an exception: ignoring:");
900 logw_exception();
901 }
902
903 /*
904 * Ignore any exception raised by the _finalize() method because
905 * it won't change anything at this point: the component is
906 * being destroyed anyway.
907 */
908 PyErr_Clear();
909 Py_XDECREF(py_method_result);
910 Py_DECREF(py_message_iter);
911 }
912
913 /* Valid for both sources and filters. */
914
915 static
916 bt_component_class_message_iterator_next_method_status
917 component_class_message_iterator_next(
918 bt_self_message_iterator *message_iterator,
919 bt_message_array_const msgs, uint64_t capacity,
920 uint64_t *count)
921 {
922 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
923 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
924 PyObject *py_method_result = NULL;
925
926 BT_ASSERT(py_message_iter);
927 py_method_result = PyObject_CallMethod(py_message_iter,
928 "_next_from_native", NULL);
929 if (!py_method_result) {
930 status = py_exc_to_status();
931 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
932 goto end;
933 }
934
935 /*
936 * The returned object, on success, is an integer object
937 * (PyLong) containing the address of a native message
938 * object (which is now ours).
939 */
940 msgs[0] = PyLong_AsVoidPtr(py_method_result);
941 *count = 1;
942
943 /* Clear potential overflow error; should never happen */
944 BT_ASSERT(!PyErr_Occurred());
945 goto end;
946
947 end:
948 Py_XDECREF(py_method_result);
949 return status;
950 }
951
952 static
953 bt_component_class_sink_consume_method_status
954 component_class_sink_consume(bt_self_component_sink *self_component_sink)
955 {
956 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
957 PyObject *py_comp = bt_self_component_get_data(self_component);
958 PyObject *py_method_result = NULL;
959 bt_component_class_sink_consume_method_status status;
960
961 BT_ASSERT(py_comp);
962 py_method_result = PyObject_CallMethod(py_comp,
963 "_consume", NULL);
964 status = py_exc_to_status();
965 if (!py_method_result && status == __BT_FUNC_STATUS_OK) {
966 /* Pretty sure this should never happen, but just in case */
967 BT_LOGE("User's _consume() method failed without raising an exception: "
968 "status=%d", status);
969 status = __BT_FUNC_STATUS_ERROR;
970 }
971
972 Py_XDECREF(py_method_result);
973 return status;
974 }
975
976 static
977 int component_class_set_help_and_desc(
978 bt_component_class *component_class,
979 const char *description, const char *help)
980 {
981 int ret;
982
983 if (description) {
984 ret = bt_component_class_set_description(component_class, description);
985 if (ret) {
986 BT_LOGE("Cannot set component class's description: "
987 "comp-cls-addr=%p", component_class);
988 goto end;
989 }
990 }
991
992 if (help) {
993 ret = bt_component_class_set_help(component_class, help);
994 if (ret) {
995 BT_LOGE("Cannot set component class's help text: "
996 "comp-cls-addr=%p", component_class);
997 goto end;
998 }
999 }
1000
1001 ret = 0;
1002
1003 end:
1004 return ret;
1005 }
1006
1007 static
1008 bt_component_class_source *bt_bt2_component_class_source_create(
1009 PyObject *py_cls, const char *name, const char *description,
1010 const char *help)
1011 {
1012 bt_component_class_source *component_class_source;
1013 bt_component_class *component_class;
1014 int ret;
1015
1016 BT_ASSERT(py_cls);
1017 component_class_source = bt_component_class_source_create(name,
1018 component_class_message_iterator_next);
1019 if (!component_class_source) {
1020 BT_LOGE_STR("Cannot create source component class.");
1021 goto end;
1022 }
1023
1024 component_class = bt_component_class_source_as_component_class(component_class_source);
1025
1026 if (component_class_set_help_and_desc(component_class, description, help)) {
1027 goto end;
1028 }
1029
1030 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
1031 BT_ASSERT(ret == 0);
1032 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
1033 BT_ASSERT(ret == 0);
1034 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
1035 component_class_can_seek_beginning);
1036 BT_ASSERT(ret == 0);
1037 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
1038 component_class_seek_beginning);
1039 BT_ASSERT(ret == 0);
1040 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1041 component_class_source_output_port_connected);
1042 BT_ASSERT(ret == 0);
1043 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
1044 BT_ASSERT(ret == 0);
1045 ret = bt_component_class_source_set_message_iterator_init_method(
1046 component_class_source, component_class_source_message_iterator_init);
1047 BT_ASSERT(ret == 0);
1048 ret = bt_component_class_source_set_message_iterator_finalize_method(
1049 component_class_source, component_class_message_iterator_finalize);
1050 BT_ASSERT(ret == 0);
1051 register_cc_ptr_to_py_cls(component_class, py_cls);
1052
1053 end:
1054 return component_class_source;
1055 }
1056
1057 static
1058 bt_component_class_filter *bt_bt2_component_class_filter_create(
1059 PyObject *py_cls, const char *name, const char *description,
1060 const char *help)
1061 {
1062 bt_component_class *component_class;
1063 bt_component_class_filter *component_class_filter;
1064 int ret;
1065
1066 BT_ASSERT(py_cls);
1067 component_class_filter = bt_component_class_filter_create(name,
1068 component_class_message_iterator_next);
1069 if (!component_class_filter) {
1070 BT_LOGE_STR("Cannot create filter component class.");
1071 goto end;
1072 }
1073
1074 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1075
1076 if (component_class_set_help_and_desc(component_class, description, help)) {
1077 goto end;
1078 }
1079
1080 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
1081 BT_ASSERT(ret == 0);
1082 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
1083 BT_ASSERT(ret == 0);
1084 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
1085 component_class_can_seek_beginning);
1086 BT_ASSERT(ret == 0);
1087 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
1088 component_class_seek_beginning);
1089 BT_ASSERT(ret == 0);
1090 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1091 component_class_filter_input_port_connected);
1092 BT_ASSERT(ret == 0);
1093 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1094 component_class_filter_output_port_connected);
1095 BT_ASSERT(ret == 0);
1096 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
1097 BT_ASSERT(ret == 0);
1098 ret = bt_component_class_filter_set_message_iterator_init_method(
1099 component_class_filter, component_class_filter_message_iterator_init);
1100 BT_ASSERT(ret == 0);
1101 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1102 component_class_filter, component_class_message_iterator_finalize);
1103 BT_ASSERT(ret == 0);
1104 register_cc_ptr_to_py_cls(component_class, py_cls);
1105
1106 end:
1107 return component_class_filter;
1108 }
1109
1110 static
1111 bt_component_class_sink *bt_bt2_component_class_sink_create(
1112 PyObject *py_cls, const char *name, const char *description,
1113 const char *help)
1114 {
1115 bt_component_class_sink *component_class_sink;
1116 bt_component_class *component_class;
1117 int ret;
1118
1119 BT_ASSERT(py_cls);
1120 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
1121
1122 if (!component_class_sink) {
1123 BT_LOGE_STR("Cannot create sink component class.");
1124 goto end;
1125 }
1126
1127 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1128
1129 if (component_class_set_help_and_desc(component_class, description, help)) {
1130 goto end;
1131 }
1132
1133 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
1134 BT_ASSERT(ret == 0);
1135 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
1136 BT_ASSERT(ret == 0);
1137 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1138 component_class_sink_input_port_connected);
1139 BT_ASSERT(ret == 0);
1140 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1141 component_class_sink_graph_is_configured);
1142 BT_ASSERT(ret == 0);
1143 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
1144 BT_ASSERT(ret == 0);
1145 register_cc_ptr_to_py_cls(component_class, py_cls);
1146
1147 end:
1148 return component_class_sink;
1149 }
1150 %}
1151
1152 struct bt_component_class_source *bt_bt2_component_class_source_create(
1153 PyObject *py_cls, const char *name, const char *description,
1154 const char *help);
1155 struct bt_component_class_filter *bt_bt2_component_class_filter_create(
1156 PyObject *py_cls, const char *name, const char *description,
1157 const char *help);
1158 struct bt_component_class_sink *bt_bt2_component_class_sink_create(
1159 PyObject *py_cls, const char *name, const char *description,
1160 const char *help);
1161 void bt_bt2_cc_init_from_bt2(void);
1162 void bt_bt2_cc_exit_handler(void);
This page took 0.054271 seconds and 4 git commands to generate.