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