Rename `BT_LOG_LEVEL` -> `BT_MINIMAL_LOG_LEVEL`
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i
CommitLineData
6945df9a
SM
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/* From component-class-const.h */
26
27typedef enum bt_component_class_status {
28 BT_COMPONENT_CLASS_STATUS_OK = 0,
29 BT_COMPONENT_CLASS_STATUS_NOMEM = -12,
30} bt_component_class_status;
31
32typedef enum bt_component_class_type {
33 BT_COMPONENT_CLASS_TYPE_SOURCE = 0,
34 BT_COMPONENT_CLASS_TYPE_FILTER = 1,
35 BT_COMPONENT_CLASS_TYPE_SINK = 2,
36} bt_component_class_type;
37
38extern const char *bt_component_class_get_name(
39 const bt_component_class *component_class);
40
41extern const char *bt_component_class_get_description(
42 const bt_component_class *component_class);
43
44extern const char *bt_component_class_get_help(
45 const bt_component_class *component_class);
46
47extern bt_component_class_type bt_component_class_get_type(
48 const bt_component_class *component_class);
49
50bt_bool bt_component_class_is_source(
51 const bt_component_class *component_class);
52
53bt_bool bt_component_class_is_filter(
54 const bt_component_class *component_class);
55
56bt_bool bt_component_class_is_sink(
57 const bt_component_class *component_class);
58
59extern void bt_component_class_get_ref(
60 const bt_component_class *component_class);
61
62extern void bt_component_class_put_ref(
63 const bt_component_class *component_class);
64
65/* From component-class-source-const.h */
66
67const bt_component_class *
68bt_component_class_source_as_component_class_const(
69 const bt_component_class_source *comp_cls_source);
70
71extern void bt_component_class_source_get_ref(
72 const bt_component_class_source *component_class_source);
73
74extern void bt_component_class_source_put_ref(
75 const bt_component_class_source *component_class_source);
76
77/* From component-class-source.h */
78
79typedef bt_self_component_status
80(*bt_component_class_source_init_method)(
81 bt_self_component_source *self_component,
82 const bt_value *params, void *init_method_data);
83
84typedef void (*bt_component_class_source_finalize_method)(
85 bt_self_component_source *self_component);
86
87typedef bt_self_message_iterator_status
88(*bt_component_class_source_message_iterator_init_method)(
89 bt_self_message_iterator *message_iterator,
90 bt_self_component_source *self_component,
91 bt_self_component_port_output *port);
92
93typedef void
94(*bt_component_class_source_message_iterator_finalize_method)(
95 bt_self_message_iterator *message_iterator);
96
97typedef bt_self_message_iterator_status
98(*bt_component_class_source_message_iterator_next_method)(
99 bt_self_message_iterator *message_iterator,
100 bt_message_array_const msgs, uint64_t capacity,
101 uint64_t *count);
102
103typedef bt_self_message_iterator_status
104(*bt_component_class_source_message_iterator_seek_ns_from_origin_method)(
105 bt_self_message_iterator *message_iterator,
106 int64_t ns_from_origin);
107
108typedef bt_self_message_iterator_status
109(*bt_component_class_source_message_iterator_seek_beginning_method)(
110 bt_self_message_iterator *message_iterator);
111
112typedef bt_bool
113(*bt_component_class_source_message_iterator_can_seek_ns_from_origin_method)(
114 bt_self_message_iterator *message_iterator,
115 int64_t ns_from_origin);
116
117typedef bt_bool
118(*bt_component_class_source_message_iterator_can_seek_beginning_method)(
119 bt_self_message_iterator *message_iterator);
120
121typedef bt_query_status (*bt_component_class_source_query_method)(
122 bt_self_component_class_source *comp_class,
123 const bt_query_executor *query_executor,
124 const char *object, const bt_value *params,
f4e38e70 125 bt_logging_level log_level,
6945df9a
SM
126 const bt_value **result);
127
6945df9a
SM
128typedef bt_self_component_status
129(*bt_component_class_source_output_port_connected_method)(
130 bt_self_component_source *self_component,
131 bt_self_component_port_output *self_port,
132 const bt_port_input *other_port);
133
134bt_component_class *bt_component_class_source_as_component_class(
135 bt_component_class_source *comp_cls_source);
136
137extern
138bt_component_class_source *bt_component_class_source_create(
139 const char *name,
140 bt_component_class_source_message_iterator_next_method method);
141
142extern bt_component_class_status
143bt_component_class_source_set_init_method(
144 bt_component_class_source *comp_class,
145 bt_component_class_source_init_method method);
146
147extern bt_component_class_status
148bt_component_class_source_set_finalize_method(
149 bt_component_class_source *comp_class,
150 bt_component_class_source_finalize_method method);
151
6945df9a
SM
152extern bt_component_class_status
153bt_component_class_source_set_output_port_connected_method(
154 bt_component_class_source *comp_class,
155 bt_component_class_source_output_port_connected_method method);
156
157extern bt_component_class_status
158bt_component_class_source_set_query_method(
159 bt_component_class_source *comp_class,
160 bt_component_class_source_query_method method);
161
162extern bt_component_class_status
163bt_component_class_source_set_message_iterator_init_method(
164 bt_component_class_source *comp_class,
165 bt_component_class_source_message_iterator_init_method method);
166
167extern bt_component_class_status
168bt_component_class_source_set_message_iterator_finalize_method(
169 bt_component_class_source *comp_class,
170 bt_component_class_source_message_iterator_finalize_method method);
171
172extern bt_component_class_status
173bt_component_class_source_set_message_iterator_seek_ns_from_origin_method(
174 bt_component_class_source *comp_class,
175 bt_component_class_source_message_iterator_seek_ns_from_origin_method method);
176
177extern bt_component_class_status
178bt_component_class_source_set_message_iterator_seek_beginning_method(
179 bt_component_class_source *comp_class,
180 bt_component_class_source_message_iterator_seek_beginning_method method);
181
182extern bt_bool
183bt_component_class_source_set_message_iterator_can_seek_ns_from_origin_method(
184 bt_component_class_source *comp_class,
185 bt_component_class_source_message_iterator_can_seek_ns_from_origin_method method);
186
187extern bt_bool
188bt_component_class_source_set_message_iterator_can_seek_beginning_method(
189 bt_component_class_source *comp_class,
190 bt_component_class_source_message_iterator_can_seek_beginning_method method);
191
192/* From component-class-filter-const.h */
193
194const bt_component_class *
195bt_component_class_filter_as_component_class_const(
196 const bt_component_class_filter *comp_cls_filter);
197
198extern void bt_component_class_filter_get_ref(
199 const bt_component_class_filter *component_class_filter);
200
201extern void bt_component_class_filter_put_ref(
202 const bt_component_class_filter *component_class_filter);
203
204/* From component-class-filter.h */
205
206typedef bt_self_component_status
207(*bt_component_class_filter_init_method)(
208 bt_self_component_filter *self_component,
209 const bt_value *params, void *init_method_data);
210
211typedef void (*bt_component_class_filter_finalize_method)(
212 bt_self_component_filter *self_component);
213
214typedef bt_self_message_iterator_status
215(*bt_component_class_filter_message_iterator_init_method)(
216 bt_self_message_iterator *message_iterator,
217 bt_self_component_filter *self_component,
218 bt_self_component_port_output *port);
219
220typedef void
221(*bt_component_class_filter_message_iterator_finalize_method)(
222 bt_self_message_iterator *message_iterator);
223
224typedef bt_self_message_iterator_status
225(*bt_component_class_filter_message_iterator_next_method)(
226 bt_self_message_iterator *message_iterator,
227 bt_message_array_const msgs, uint64_t capacity,
228 uint64_t *count);
229
230typedef bt_self_message_iterator_status
231(*bt_component_class_filter_message_iterator_seek_ns_from_origin_method)(
232 bt_self_message_iterator *message_iterator,
233 int64_t ns_from_origin);
234
235typedef bt_self_message_iterator_status
236(*bt_component_class_filter_message_iterator_seek_beginning_method)(
237 bt_self_message_iterator *message_iterator);
238
239typedef bt_bool
240(*bt_component_class_filter_message_iterator_can_seek_ns_from_origin_method)(
241 bt_self_message_iterator *message_iterator,
242 int64_t ns_from_origin);
243
244typedef bt_bool
245(*bt_component_class_filter_message_iterator_can_seek_beginning_method)(
246 bt_self_message_iterator *message_iterator);
247
248typedef bt_query_status
249(*bt_component_class_filter_query_method)(
250 bt_self_component_class_filter *comp_class,
251 const bt_query_executor *query_executor,
252 const char *object, const bt_value *params,
f4e38e70 253 bt_logging_level log_level,
6945df9a
SM
254 const bt_value **result);
255
6945df9a
SM
256typedef bt_self_component_status
257(*bt_component_class_filter_input_port_connected_method)(
258 bt_self_component_filter *self_component,
259 bt_self_component_port_input *self_port,
260 const bt_port_output *other_port);
261
262typedef bt_self_component_status
263(*bt_component_class_filter_output_port_connected_method)(
264 bt_self_component_filter *self_component,
265 bt_self_component_port_output *self_port,
266 const bt_port_input *other_port);
267
268bt_component_class *bt_component_class_filter_as_component_class(
269 bt_component_class_filter *comp_cls_filter);
270
271extern
272bt_component_class_filter *bt_component_class_filter_create(
273 const char *name,
274 bt_component_class_filter_message_iterator_next_method method);
275
276extern bt_component_class_status
277bt_component_class_filter_set_init_method(
278 bt_component_class_filter *comp_class,
279 bt_component_class_filter_init_method method);
280
281extern bt_component_class_status
282bt_component_class_filter_set_finalize_method(
283 bt_component_class_filter *comp_class,
284 bt_component_class_filter_finalize_method method);
285
6945df9a
SM
286extern bt_component_class_status
287bt_component_class_filter_set_input_port_connected_method(
288 bt_component_class_filter *comp_class,
289 bt_component_class_filter_input_port_connected_method method);
290
291extern bt_component_class_status
292bt_component_class_filter_set_output_port_connected_method(
293 bt_component_class_filter *comp_class,
294 bt_component_class_filter_output_port_connected_method method);
295
296extern bt_component_class_status
297bt_component_class_filter_set_query_method(
298 bt_component_class_filter *comp_class,
299 bt_component_class_filter_query_method method);
300
301extern bt_component_class_status
302bt_component_class_filter_set_message_iterator_init_method(
303 bt_component_class_filter *comp_class,
304 bt_component_class_filter_message_iterator_init_method method);
305
306extern bt_component_class_status
307bt_component_class_filter_set_message_iterator_finalize_method(
308 bt_component_class_filter *comp_class,
309 bt_component_class_filter_message_iterator_finalize_method method);
310
311extern bt_component_class_status
312bt_component_class_filter_set_message_iterator_seek_ns_from_origin_method(
313 bt_component_class_filter *comp_class,
314 bt_component_class_filter_message_iterator_seek_ns_from_origin_method method);
315
316extern bt_component_class_status
317bt_component_class_filter_set_message_iterator_seek_beginning_method(
318 bt_component_class_filter *comp_class,
319 bt_component_class_filter_message_iterator_seek_beginning_method method);
320
321extern bt_bool
322bt_component_class_filter_set_message_iterator_can_seek_ns_from_origin_method(
323 bt_component_class_filter *comp_class,
324 bt_component_class_filter_message_iterator_can_seek_ns_from_origin_method method);
325
326extern bt_bool
327bt_component_class_filter_set_message_iterator_can_seek_beginning_method(
328 bt_component_class_filter *comp_class,
329 bt_component_class_filter_message_iterator_can_seek_beginning_method method);
330
331/* From component-class-sink-const.h */
332
333const bt_component_class *
334bt_component_class_sink_as_component_class_const(
335 const bt_component_class_sink *comp_cls_sink);
336
337extern void bt_component_class_sink_get_ref(
338 const bt_component_class_sink *component_class_sink);
339
340extern void bt_component_class_sink_put_ref(
341 const bt_component_class_sink *component_class_sink);
342
343/* From component-class-sink.h */
344
345typedef bt_self_component_status (*bt_component_class_sink_init_method)(
346 bt_self_component_sink *self_component,
347 const bt_value *params, void *init_method_data);
348
349typedef void (*bt_component_class_sink_finalize_method)(
350 bt_self_component_sink *self_component);
351
352typedef bt_query_status
353(*bt_component_class_sink_query_method)(
354 bt_self_component_class_sink *comp_class,
355 const bt_query_executor *query_executor,
356 const char *object, const bt_value *params,
f4e38e70 357 bt_logging_level log_level,
6945df9a
SM
358 const bt_value **result);
359
6945df9a
SM
360typedef bt_self_component_status
361(*bt_component_class_sink_input_port_connected_method)(
362 bt_self_component_sink *self_component,
363 bt_self_component_port_input *self_port,
364 const bt_port_output *other_port);
365
366typedef bt_self_component_status
367(*bt_component_class_sink_graph_is_configured_method)(
368 bt_self_component_sink *self_component);
369
370typedef bt_self_component_status (*bt_component_class_sink_consume_method)(
371 bt_self_component_sink *self_component);
372
373bt_component_class *bt_component_class_sink_as_component_class(
374 bt_component_class_sink *comp_cls_sink);
375
376extern
377bt_component_class_sink *bt_component_class_sink_create(
378 const char *name,
379 bt_component_class_sink_consume_method method);
380
381extern bt_component_class_status bt_component_class_sink_set_init_method(
382 bt_component_class_sink *comp_class,
383 bt_component_class_sink_init_method method);
384
385extern bt_component_class_status bt_component_class_sink_set_finalize_method(
386 bt_component_class_sink *comp_class,
387 bt_component_class_sink_finalize_method method);
388
6945df9a
SM
389extern bt_component_class_status
390bt_component_class_sink_set_input_port_connected_method(
391 bt_component_class_sink *comp_class,
392 bt_component_class_sink_input_port_connected_method method);
393
394extern bt_component_class_status
395bt_component_class_sink_set_graph_is_configured_method(
396 bt_component_class_sink *comp_class,
397 bt_component_class_sink_graph_is_configured_method method);
398
399extern bt_component_class_status bt_component_class_sink_set_query_method(
400 bt_component_class_sink *comp_class,
401 bt_component_class_sink_query_method method);
402
403/* From self-component-class-source.h */
404
405const bt_component_class_source *
406bt_self_component_class_source_as_component_class_source(
407 bt_self_component_class_source *self_comp_cls_source);
408
409/* From self-component-class-filter.h */
410
411const bt_component_class_filter *
412bt_self_component_class_filter_as_component_class_filter(
413 bt_self_component_class_filter *self_comp_cls_filter);
414
415/* From self-component-class-sink.h */
416
417const bt_component_class_sink *
418bt_self_component_class_sink_as_component_class_sink(
419 bt_self_component_class_sink *self_comp_cls_sink);
420
421%{
422/*
423 * This hash table associates a BT component class object address to a
424 * user-defined Python class (PyObject *). The keys and values are NOT
425 * owned by this hash table. The Python class objects are owned by the
426 * Python module, which should not be unloaded until it is not possible
427 * to create a user Python component anyway.
428 *
429 * This hash table is written to when a user-defined Python component
430 * class is created by one of the bt_py3_component_class_*_create()
431 * functions.
432 *
433 * This function is read from when a user calls bt_component_create()
434 * with a component class pointer created by one of the functions above.
435 * In this case, the original Python class needs to be found to
436 * instantiate it and associate the created Python component object with
437 * a BT component object instance.
438 */
439
440static GHashTable *bt_cc_ptr_to_py_cls;
441
442static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
443 PyObject *py_cls)
444{
445 if (!bt_cc_ptr_to_py_cls) {
446 /*
447 * Lazy-initializing this GHashTable because GLib
448 * might not be initialized yet and it needs to be
449 * before we call g_hash_table_new()
450 */
451 BT_LOGD_STR("Creating native component class to Python component class hash table.");
452 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
453 BT_ASSERT(bt_cc_ptr_to_py_cls);
454 }
455
456 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
457 (gpointer) py_cls);
458}
459
460static PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
461{
462 if (!bt_cc_ptr_to_py_cls) {
463 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
464 "comp-cls-addr=%p", bt_cc);
465 return NULL;
466 }
467
468 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
469 (gconstpointer) bt_cc);
470}
471
472
473/*
474 * Useful Python objects.
475 */
476
477static PyObject *py_mod_bt2 = NULL;
478static PyObject *py_mod_bt2_exc_error_type = NULL;
479static PyObject *py_mod_bt2_exc_try_again_type = NULL;
480static PyObject *py_mod_bt2_exc_stop_type = NULL;
5602ef81 481static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL;
6945df9a
SM
482static PyObject *py_mod_bt2_exc_invalid_query_object_type = NULL;
483static PyObject *py_mod_bt2_exc_invalid_query_params_type = NULL;
484
485static void bt_py3_cc_init_from_bt2(void)
486{
487 /*
488 * This is called once the bt2 package is loaded.
489 *
490 * Those modules and functions are needed while the package is
491 * used. Loading them here is safe because we know the bt2
492 * package is imported, and we know that the user cannot use the
493 * code here without importing bt2 first.
494 */
495 py_mod_bt2 = PyImport_ImportModule("bt2");
496 BT_ASSERT(py_mod_bt2);
497 py_mod_bt2_exc_error_type =
498 PyObject_GetAttrString(py_mod_bt2, "Error");
499 BT_ASSERT(py_mod_bt2_exc_error_type);
500 py_mod_bt2_exc_try_again_type =
501 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
502 BT_ASSERT(py_mod_bt2_exc_try_again_type);
503 py_mod_bt2_exc_stop_type =
504 PyObject_GetAttrString(py_mod_bt2, "Stop");
505 BT_ASSERT(py_mod_bt2_exc_stop_type);
6945df9a
SM
506 py_mod_bt2_exc_invalid_query_object_type =
507 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryObject");
508 BT_ASSERT(py_mod_bt2_exc_invalid_query_object_type);
509 py_mod_bt2_exc_invalid_query_params_type =
510 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryParams");
511 BT_ASSERT(py_mod_bt2_exc_invalid_query_params_type);
512}
513
514static void bt_py3_cc_exit_handler(void)
515{
516 /*
517 * This is an exit handler (set by the bt2 package).
518 *
519 * We only give back the references that we took in
520 * bt_py3_cc_init_from_bt2() here. The global variables continue
521 * to exist for the code of this file, but they are now borrowed
522 * references. If this code is executed, it means that somehow
523 * the modules are still loaded, so it should be safe to use
524 * them even without a strong reference.
525 *
526 * We cannot do this in the library's destructor because it
527 * gets executed once Python is already finalized.
528 */
529 Py_XDECREF(py_mod_bt2);
530 Py_XDECREF(py_mod_bt2_exc_error_type);
531 Py_XDECREF(py_mod_bt2_exc_try_again_type);
532 Py_XDECREF(py_mod_bt2_exc_stop_type);
5602ef81 533 Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type);
6945df9a
SM
534 Py_XDECREF(py_mod_bt2_exc_invalid_query_object_type);
535 Py_XDECREF(py_mod_bt2_exc_invalid_query_params_type);
536}
537
538
539/* Library destructor */
540
541__attribute__((destructor))
542static void bt_py3_native_comp_class_dtor(void) {
543 /* Destroy component class association hash table */
544 if (bt_cc_ptr_to_py_cls) {
545 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
546 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
547 }
548}
549
550
551// TODO: maybe we can wrap code in the Python methods (e.g. _query_from_native)
552// in a try catch and print the error there instead, it would be simpler.
553static
554void bt2_py_loge_exception(void)
555{
556 PyObject *type = NULL;
557 PyObject *value = NULL;
558 PyObject *traceback = NULL;
559 PyObject *traceback_module = NULL;
560 PyObject *format_exception_func = NULL;
561 PyObject *exc_str_list = NULL;
562 GString *msg_buf = NULL;
563 Py_ssize_t i;
564
565 BT_ASSERT(PyErr_Occurred() != NULL);
566
567 PyErr_Fetch(&type, &value, &traceback);
568
569 BT_ASSERT(type != NULL);
570
571 /*
572 * traceback can be NULL, when we fail to call a Python function from the
c594ab03 573 * native code (there is no Python stack at that point). E.g.:
6945df9a 574 *
c594ab03 575 * TypeError: _query_from_native() takes 5 positional arguments but 8 were given
6945df9a
SM
576 */
577
578
579 /* Make sure `value` is what we expected - an instance of `type`. */
580 PyErr_NormalizeException(&type, &value, &traceback);
581
582 traceback_module = PyImport_ImportModule("traceback");
583 if (!traceback_module) {
584 BT_LOGE_STR("Failed to log Python exception (could not import traceback module).");
585 goto end;
586 }
587
588 format_exception_func = PyObject_GetAttrString(traceback_module,
589 traceback ? "format_exception" : "format_exception_only");
590 if (!format_exception_func) {
591 BT_LOGE_STR("Failed to log Python exception (could not find format_exception).");
592 goto end;
593 }
594
595 if (!PyCallable_Check(format_exception_func)) {
596 BT_LOGE_STR("Failed to log Python exception (format_exception is not callable).");
597 goto end;
598 }
599
600 exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func, type, value, traceback, NULL);
601 if (!exc_str_list) {
602 PyErr_Print();
603 BT_LOGE_STR("Failed to log Python exception (call to format_exception failed).");
604 goto end;
605 }
606
607 msg_buf = g_string_new(NULL);
608
609 for (i = 0; i < PyList_Size(exc_str_list); i++) {
610 PyObject *exc_str = PyList_GetItem(exc_str_list, i);
611 const char *str = PyUnicode_AsUTF8(exc_str);
612 if (!str) {
613 BT_LOGE_STR("Failed to log Python exception (failed to convert exception to string).");
614 goto end;
615 }
616
617 g_string_append(msg_buf, str);
618 }
619
620 BT_LOGE_STR(msg_buf->str);
621
622end:
623 if (msg_buf) {
624 g_string_free(msg_buf, TRUE);
625 }
626 Py_XDECREF(exc_str_list);
627 Py_XDECREF(format_exception_func);
628 Py_XDECREF(traceback_module);
629
630 /* PyErr_Restore takes our references. */
631 PyErr_Restore(type, value, traceback);
632}
633
634static bt_self_component_status bt_py3_exc_to_self_component_status(void)
635{
636 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
637 PyObject *exc = PyErr_Occurred();
638
639 if (!exc) {
640 goto end;
641 }
642
643 if (PyErr_GivenExceptionMatches(exc,
644 py_mod_bt2_exc_try_again_type)) {
645 status = BT_SELF_COMPONENT_STATUS_AGAIN;
646 } else if (PyErr_GivenExceptionMatches(exc,
647 py_mod_bt2_exc_stop_type)) {
648 status = BT_SELF_COMPONENT_STATUS_END;
6945df9a
SM
649 } else {
650 bt2_py_loge_exception();
651 status = BT_SELF_COMPONENT_STATUS_ERROR;
652 }
653
654end:
655 PyErr_Clear();
656 return status;
657}
658
659/* Component class proxy methods (delegate to the attached Python object) */
660
661static bt_self_message_iterator_status
662bt_py3_exc_to_self_message_iterator_status(void)
663{
664 enum bt_self_message_iterator_status status =
665 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
666 PyObject *exc = PyErr_Occurred();
667
668 if (!exc) {
669 goto end;
670 }
671
672 if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_stop_type)) {
89d7b349 673 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
6945df9a 674 } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_try_again_type)) {
89d7b349 675 status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN;
6945df9a
SM
676 } else {
677 bt2_py_loge_exception();
89d7b349 678 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
6945df9a
SM
679 }
680
681end:
682 PyErr_Clear();
683 return status;
684}
685
686static enum bt_query_status bt_py3_exc_to_query_status(void)
687{
688 enum bt_query_status status = BT_QUERY_STATUS_OK;
689 PyObject *exc = PyErr_Occurred();
690
691 if (!exc) {
692 goto end;
693 }
694
695 if (PyErr_GivenExceptionMatches(exc,
696 py_mod_bt2_exc_invalid_query_object_type)) {
697 status = BT_QUERY_STATUS_INVALID_OBJECT;
698 } else if (PyErr_GivenExceptionMatches(exc,
699 py_mod_bt2_exc_invalid_query_params_type)) {
700 status = BT_QUERY_STATUS_INVALID_PARAMS;
701 } else if (PyErr_GivenExceptionMatches(exc,
702 py_mod_bt2_exc_try_again_type)) {
703 status = BT_QUERY_STATUS_AGAIN;
704 } else {
705 bt2_py_loge_exception();
706 status = BT_QUERY_STATUS_ERROR;
707 }
708
709end:
710 PyErr_Clear();
711 return status;
712}
713
714static bt_self_component_status
715bt_py3_component_class_init(
716 bt_self_component *self_component,
717 void *self_component_v,
718 swig_type_info *self_comp_cls_type_swig_type,
719 const bt_value *params,
720 void *init_method_data)
721{
722 const bt_component *component = bt_self_component_as_component(self_component);
723 const bt_component_class *component_class = bt_component_borrow_class_const(component);
724 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
725 PyObject *py_cls = NULL;
726 PyObject *py_comp = NULL;
727 PyObject *py_params_ptr = NULL;
728 PyObject *py_comp_ptr = NULL;
729
730 (void) init_method_data;
731
732 BT_ASSERT(self_component);
733 BT_ASSERT(self_component_v);
734 BT_ASSERT(self_comp_cls_type_swig_type);
735
736 /*
737 * Get the user-defined Python class which created this
738 * component's class in the first place (borrowed
739 * reference).
740 */
741 py_cls = lookup_cc_ptr_to_py_cls(component_class);
742 if (!py_cls) {
743 BT_LOGE("Cannot find Python class associated to native component class: "
744 "comp-cls-addr=%p", component_class);
745 goto error;
746 }
747
748 /* Parameters pointer -> SWIG pointer Python object */
749 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
750 SWIGTYPE_p_bt_value, 0);
751 if (!py_params_ptr) {
752 BT_LOGE_STR("Failed to create a SWIG pointer object.");
753 goto error;
754 }
755
756 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
757 self_comp_cls_type_swig_type, 0);
758 if (!py_comp_ptr) {
759 BT_LOGE_STR("Failed to create a SWIG pointer object.");
760 goto error;
761 }
762
763 /*
764 * Do the equivalent of this:
765 *
766 * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr)
767 *
768 * _UserComponentType._init_from_native() calls the Python
769 * component object's __init__() function.
770 */
771 py_comp = PyObject_CallMethod(py_cls,
772 "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
773 if (!py_comp) {
774 bt2_py_loge_exception();
775 BT_LOGE("Failed to call Python class's _init_from_native() method: "
776 "py-cls-addr=%p", py_cls);
777
778 goto error;
779 }
780
781 /*
782 * Our user Python component object is now fully created and
783 * initialized by the user. Since we just created it, this
784 * native component is its only (persistent) owner.
785 */
786 bt_self_component_set_data(self_component, py_comp);
787 py_comp = NULL;
788 goto end;
789
790error:
791 status = BT_SELF_COMPONENT_STATUS_ERROR;
792
793 /*
794 * Clear any exception: we're returning a bad status anyway. If
795 * this call originated from Python (creation from a plugin's
796 * component class, for example), then the user gets an
797 * appropriate creation error.
798 */
799 PyErr_Clear();
800
801end:
802 Py_XDECREF(py_comp);
803 Py_XDECREF(py_params_ptr);
804 Py_XDECREF(py_comp_ptr);
805 return status;
806}
807
808/*
809 * Method of bt_component_class_source to initialize a bt_self_component_source
810 * of that class.
811 */
812
813static bt_self_component_status
814bt_py3_component_class_source_init(bt_self_component_source *self_component_source,
815 const bt_value *params, void *init_method_data)
816{
817 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
818 return bt_py3_component_class_init(
819 self_component,
820 self_component_source,
821 SWIGTYPE_p_bt_self_component_source,
822 params, init_method_data);
823}
824
825static bt_self_component_status
826bt_py3_component_class_filter_init(bt_self_component_filter *self_component_filter,
827 const bt_value *params, void *init_method_data)
828{
829 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
830 return bt_py3_component_class_init(
831 self_component,
832 self_component_filter,
833 SWIGTYPE_p_bt_self_component_filter,
834 params, init_method_data);
835}
836
837static bt_self_component_status
838bt_py3_component_class_sink_init(bt_self_component_sink *self_component_sink,
839 const bt_value *params, void *init_method_data)
840{
841 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
842 return bt_py3_component_class_init(
843 self_component,
844 self_component_sink,
845 SWIGTYPE_p_bt_self_component_sink,
846 params, init_method_data);
847}
848
849static void bt_py3_component_class_finalize(bt_self_component *self_component)
850{
851 PyObject *py_comp = bt_self_component_get_data(self_component);
852 BT_ASSERT(py_comp);
853
854 /* Call user's _finalize() method */
855 PyObject *py_method_result = PyObject_CallMethod(py_comp,
856 "_finalize", NULL);
857
858 if (PyErr_Occurred()) {
859 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
860 }
861
862 /*
863 * Ignore any exception raised by the _finalize() method because
864 * it won't change anything at this point: the component is
865 * being destroyed anyway.
866 */
867 PyErr_Clear();
868 Py_XDECREF(py_method_result);
869 Py_DECREF(py_comp);
870}
871
872static void
873bt_py3_component_class_source_finalize(bt_self_component_source *self_component_source)
874{
875 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
876 bt_py3_component_class_finalize(self_component);
877}
878
879static void
880bt_py3_component_class_filter_finalize(bt_self_component_filter *self_component_filter)
881{
882 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
883 bt_py3_component_class_finalize(self_component);
884}
885
886static void
887bt_py3_component_class_sink_finalize(bt_self_component_sink *self_component_sink)
888{
889 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
890 bt_py3_component_class_finalize(self_component);
891}
892
6945df9a
SM
893static bt_self_component_status
894bt_py3_component_class_port_connected(
895 bt_self_component *self_component,
896 void *self_component_port,
897 swig_type_info *self_component_port_swig_type,
898 bt_port_type self_component_port_type,
899 const void *other_port,
900 swig_type_info *other_port_swig_type)
901{
902 bt_self_component_status status;
903 PyObject *py_comp = NULL;
904 PyObject *py_self_port_ptr = NULL;
905 PyObject *py_other_port_ptr = NULL;
906 PyObject *py_method_result = NULL;
907
908 py_comp = bt_self_component_get_data(self_component);
909 BT_ASSERT(py_comp);
910
911 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
912 self_component_port_swig_type, 0);
913 if (!py_self_port_ptr) {
914 BT_LOGF_STR("Failed to create a SWIG pointer object.");
915 status = BT_SELF_COMPONENT_STATUS_NOMEM;
916 goto end;
917 }
918
919 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
920 other_port_swig_type, 0);
921 if (!py_other_port_ptr) {
922 BT_LOGF_STR("Failed to create a SWIG pointer object.");
923 status = BT_SELF_COMPONENT_STATUS_NOMEM;
924 goto end; }
925
926 py_method_result = PyObject_CallMethod(py_comp,
927 "_port_connected_from_native", "(OiO)", py_self_port_ptr,
928 self_component_port_type, py_other_port_ptr);
929
930 BT_ASSERT(!py_method_result || py_method_result == Py_None);
931
932 status = bt_py3_exc_to_self_component_status();
933
934end:
935 Py_XDECREF(py_self_port_ptr);
936 Py_XDECREF(py_other_port_ptr);
937 Py_XDECREF(py_method_result);
938
939 return status;
940}
941
942static bt_self_component_status
943bt_py3_component_class_source_output_port_connected(
944 bt_self_component_source *self_component_source,
945 bt_self_component_port_output *self_component_port_output,
946 const bt_port_input *other_port_input)
947{
948 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
949
950 return bt_py3_component_class_port_connected(
951 self_component,
952 self_component_port_output,
953 SWIGTYPE_p_bt_self_component_port_output,
954 BT_PORT_TYPE_OUTPUT,
955 other_port_input,
956 SWIGTYPE_p_bt_port_input);
957}
958
959static bt_self_component_status
960bt_py3_component_class_filter_input_port_connected(
961 bt_self_component_filter *self_component_filter,
962 bt_self_component_port_input *self_component_port_input,
963 const bt_port_output *other_port_output)
964{
965 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
966
967 return bt_py3_component_class_port_connected(
968 self_component,
969 self_component_port_input,
970 SWIGTYPE_p_bt_self_component_port_input,
971 BT_PORT_TYPE_INPUT,
972 other_port_output,
973 SWIGTYPE_p_bt_port_output);
974}
975
976static bt_self_component_status
977bt_py3_component_class_filter_output_port_connected(
978 bt_self_component_filter *self_component_filter,
979 bt_self_component_port_output *self_component_port_output,
980 const bt_port_input *other_port_input)
981{
982 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
983
984 return bt_py3_component_class_port_connected(
985 self_component,
986 self_component_port_output,
987 SWIGTYPE_p_bt_self_component_port_output,
988 BT_PORT_TYPE_OUTPUT,
989 other_port_input,
990 SWIGTYPE_p_bt_port_input);
991}
992
993static bt_self_component_status
994bt_py3_component_class_sink_input_port_connected(
995 bt_self_component_sink *self_component_sink,
996 bt_self_component_port_input *self_component_port_input,
997 const bt_port_output *other_port_output)
998{
999 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1000
1001 return bt_py3_component_class_port_connected(
1002 self_component,
1003 self_component_port_input,
1004 SWIGTYPE_p_bt_self_component_port_input,
1005 BT_PORT_TYPE_INPUT,
1006 other_port_output,
1007 SWIGTYPE_p_bt_port_output);
1008}
1009
1010static bt_self_component_status
1011bt_py3_component_class_sink_graph_is_configured(bt_self_component_sink *self_component_sink)
1012{
1013 PyObject *py_comp = NULL;
1014 PyObject *py_method_result = NULL;
1015 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
1016 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1017
1018 py_comp = bt_self_component_get_data(self_component);
1019 py_method_result = PyObject_CallMethod(py_comp,
1020 "_graph_is_configured_from_native", NULL);
1021
1022 BT_ASSERT(!py_method_result || py_method_result == Py_None);
1023
1024 status = bt_py3_exc_to_self_component_status();
1025
1026 Py_XDECREF(py_method_result);
1027
1028 return status;
1029}
1030
1031static bt_query_status
1032bt_py3_component_class_query(
1033 const bt_component_class *component_class,
1034 const bt_query_executor *query_executor,
1035 const char *object, const bt_value *params,
f4e38e70 1036 bt_logging_level log_level,
6945df9a
SM
1037 const bt_value **result)
1038{
1039 PyObject *py_cls = NULL;
1040 PyObject *py_params_ptr = NULL;
1041 PyObject *py_query_exec_ptr = NULL;
1042 PyObject *py_query_func = NULL;
1043 PyObject *py_object = NULL;
1044 PyObject *py_results_addr = NULL;
1045 bt_query_status status = BT_QUERY_STATUS_OK;
1046
1047 py_cls = lookup_cc_ptr_to_py_cls(component_class);
1048 if (!py_cls) {
1049 BT_LOGE("Cannot find Python class associated to native component class: "
1050 "comp-cls-addr=%p", component_class);
1051 goto error;
1052 }
1053
1054 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
1055 SWIGTYPE_p_bt_value, 0);
1056 if (!py_params_ptr) {
1057 BT_LOGE_STR("Failed to create a SWIG pointer object.");
1058 goto error;
1059 }
1060
1061 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
1062 SWIGTYPE_p_bt_query_executor, 0);
1063 if (!py_query_exec_ptr) {
1064 BT_LOGE_STR("Failed to create a SWIG pointer object.");
1065 goto error;
1066 }
1067
1068 py_object = SWIG_FromCharPtr(object);
1069 if (!py_object) {
1070 BT_LOGE_STR("Failed to create a Python string.");
1071 goto error;
1072 }
1073
1074 py_results_addr = PyObject_CallMethod(py_cls,
f4e38e70
PP
1075 "_query_from_native", "(OOOi)", py_query_exec_ptr,
1076 py_object, py_params_ptr, (int) log_level);
6945df9a
SM
1077
1078 if (!py_results_addr) {
1079 BT_LOGE("Failed to call Python class's _query_from_native() method: "
1080 "py-cls-addr=%p", py_cls);
1081 status = bt_py3_exc_to_query_status();
1082 goto end;
1083 }
1084
1085 /*
1086 * The returned object, on success, is an integer object
1087 * (PyLong) containing the address of a BT value object (new
1088 * reference).
1089 */
babe0791 1090 *result = PyLong_AsVoidPtr(py_results_addr);
6945df9a
SM
1091 BT_ASSERT(!PyErr_Occurred());
1092 BT_ASSERT(*result);
1093 goto end;
1094
1095error:
1096 PyErr_Clear();
1097 status = BT_QUERY_STATUS_ERROR;
1098
1099end:
1100 Py_XDECREF(py_params_ptr);
1101 Py_XDECREF(py_query_exec_ptr);
1102 Py_XDECREF(py_query_func);
1103 Py_XDECREF(py_object);
1104 Py_XDECREF(py_results_addr);
1105 return status;
1106}
1107
1108static bt_query_status
1109bt_py3_component_class_source_query(
1110 bt_self_component_class_source *self_component_class_source,
1111 const bt_query_executor *query_executor,
1112 const char *object, const bt_value *params,
f4e38e70 1113 bt_logging_level log_level,
6945df9a
SM
1114 const bt_value **result)
1115{
1116 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
1117 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
f4e38e70 1118 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
6945df9a
SM
1119}
1120
1121static bt_query_status
1122bt_py3_component_class_filter_query(
1123 bt_self_component_class_filter *self_component_class_filter,
1124 const bt_query_executor *query_executor,
1125 const char *object, const bt_value *params,
f4e38e70 1126 bt_logging_level log_level,
6945df9a
SM
1127 const bt_value **result)
1128{
1129 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
1130 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
f4e38e70 1131 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
6945df9a
SM
1132}
1133
1134static bt_query_status
1135bt_py3_component_class_sink_query(
1136 bt_self_component_class_sink *self_component_class_sink,
1137 const bt_query_executor *query_executor,
1138 const char *object, const bt_value *params,
f4e38e70 1139 bt_logging_level log_level,
6945df9a
SM
1140 const bt_value **result)
1141{
1142 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
1143 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
f4e38e70 1144 return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result);
6945df9a
SM
1145}
1146
1147static bt_self_message_iterator_status
1148bt_py3_component_class_message_iterator_init(
1149 bt_self_message_iterator *self_message_iterator,
1150 bt_self_component *self_component,
1151 bt_self_component_port_output *self_component_port_output)
1152{
89d7b349 1153 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
6945df9a
SM
1154 PyObject *py_comp_cls = NULL;
1155 PyObject *py_iter_cls = NULL;
1156 PyObject *py_iter_ptr = NULL;
c5f330cd 1157 PyObject *py_component_port_output_ptr = NULL;
6945df9a
SM
1158 PyObject *py_init_method_result = NULL;
1159 PyObject *py_iter = NULL;
1160 PyObject *py_comp;
1161
1162 py_comp = bt_self_component_get_data(self_component);
1163
5602ef81 1164 /* Find user's Python message iterator class */
6945df9a
SM
1165 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
1166 if (!py_comp_cls) {
1167 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
1168 goto error;
1169 }
1170
1171 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
1172 if (!py_iter_cls) {
1173 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
1174 goto error;
1175 }
1176
1177 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
1178 SWIGTYPE_p_bt_self_message_iterator, 0);
1179 if (!py_iter_ptr) {
1180 BT_LOGE_STR("Failed to create a SWIG pointer object.");
1181 goto error;
1182 }
1183
1184 /*
5602ef81 1185 * Create object with borrowed native message iterator
6945df9a
SM
1186 * reference:
1187 *
1188 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
1189 */
1190 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
1191 "(OO)", py_iter_cls, py_iter_ptr);
1192 if (!py_iter) {
1193 BT_LOGE("Failed to call Python class's __new__() method: "
1194 "py-cls-addr=%p", py_iter_cls);
1195 bt2_py_loge_exception();
1196 goto error;
1197 }
1198
1199 /*
1200 * Initialize object:
1201 *
c5f330cd
SM
1202 * py_iter.__init__(self_output_port)
1203 *
1204 * through the _init_for_native helper static method.
6945df9a
SM
1205 *
1206 * At this point, py_iter._ptr is set, so this initialization
1207 * function has access to self._component (which gives it the
1208 * user Python component object from which the iterator was
1209 * created).
1210 */
c5f330cd
SM
1211 py_component_port_output_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port_output),
1212 SWIGTYPE_p_bt_self_component_port_output, 0);
1213 if (!py_component_port_output_ptr) {
1214 BT_LOGE_STR("Failed to create a SWIG pointer object.");
1215 goto error;
1216 }
1217
1218 py_init_method_result = PyObject_CallMethod(py_iter, "_init_from_native", "O", py_component_port_output_ptr);
6945df9a
SM
1219 if (!py_init_method_result) {
1220 BT_LOGE_STR("User's __init__() method failed.");
1221 bt2_py_loge_exception();
1222 goto error;
1223 }
1224
1225 /*
1226 * Since the Python code can never instantiate a user-defined
5602ef81
SM
1227 * message iterator class, the native message iterator
1228 * object does NOT belong to a user Python message iterator
6945df9a 1229 * object (borrowed reference). However this Python object is
5602ef81 1230 * owned by this native message iterator object.
6945df9a 1231 *
5602ef81
SM
1232 * In the Python world, the lifetime of the native message
1233 * iterator is managed by a _GenericMessageIterator
6945df9a
SM
1234 * instance:
1235 *
5602ef81
SM
1236 * _GenericMessageIterator instance:
1237 * owns a native bt_message_iterator object (iter)
6945df9a
SM
1238 * owns a _UserMessageIterator instance (py_iter)
1239 * self._ptr is a borrowed reference to the
5602ef81 1240 * native bt_private_connection_private_message_iterator
6945df9a
SM
1241 * object (iter)
1242 */
1243 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
1244 py_iter = NULL;
1245 goto end;
1246
1247error:
1248 status = bt_py3_exc_to_self_message_iterator_status();
1249 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
1250 /*
1251 * Looks like there wasn't any exception from the Python
1252 * side, but we're still in an error state here.
1253 */
1254 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
1255 }
1256
1257 /*
1258 * Clear any exception: we're returning a bad status anyway. If
1259 * this call originated from Python, then the user gets an
1260 * appropriate creation error.
1261 */
1262 PyErr_Clear();
1263
1264end:
1265 Py_XDECREF(py_comp_cls);
1266 Py_XDECREF(py_iter_cls);
1267 Py_XDECREF(py_iter_ptr);
c5f330cd 1268 Py_XDECREF(py_component_port_output_ptr);
6945df9a
SM
1269 Py_XDECREF(py_init_method_result);
1270 Py_XDECREF(py_iter);
1271 return status;
1272}
1273
1274static bt_self_message_iterator_status
1275bt_py3_component_class_source_message_iterator_init(
1276 bt_self_message_iterator *self_message_iterator,
1277 bt_self_component_source *self_component_source,
1278 bt_self_component_port_output *self_component_port_output)
1279{
1280 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
1281 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1282}
1283
1284static bt_self_message_iterator_status
1285bt_py3_component_class_filter_message_iterator_init(
1286 bt_self_message_iterator *self_message_iterator,
1287 bt_self_component_filter *self_component_filter,
1288 bt_self_component_port_output *self_component_port_output)
1289{
1290 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
1291 return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
1292}
1293
1294static void
1295bt_py3_component_class_message_iterator_finalize(
1296 bt_self_message_iterator *message_iterator)
1297{
1298 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1299 PyObject *py_method_result = NULL;
1300
1301 BT_ASSERT(py_message_iter);
1302
1303 /* Call user's _finalize() method */
1304 py_method_result = PyObject_CallMethod(py_message_iter,
1305 "_finalize", NULL);
1306
1307 if (PyErr_Occurred()) {
1308 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
1309 }
1310
1311 /*
1312 * Ignore any exception raised by the _finalize() method because
1313 * it won't change anything at this point: the component is
1314 * being destroyed anyway.
1315 */
1316 PyErr_Clear();
1317 Py_XDECREF(py_method_result);
1318 Py_DECREF(py_message_iter);
1319}
1320
1321/* Valid for both sources and filters. */
1322
1323static bt_self_message_iterator_status
1324bt_py3_component_class_message_iterator_next(
1325 bt_self_message_iterator *message_iterator,
1326 bt_message_array_const msgs, uint64_t capacity,
1327 uint64_t *count)
1328{
89d7b349 1329 bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
6945df9a
SM
1330 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1331 PyObject *py_method_result = NULL;
1332
1333 BT_ASSERT(py_message_iter);
1334 py_method_result = PyObject_CallMethod(py_message_iter,
1335 "_next_from_native", NULL);
1336 if (!py_method_result) {
1337 status = bt_py3_exc_to_self_message_iterator_status();
1338 BT_ASSERT(status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK);
1339 goto end;
1340 }
1341
1342 /*
1343 * The returned object, on success, is an integer object
5602ef81 1344 * (PyLong) containing the address of a native message
6945df9a
SM
1345 * object (which is now ours).
1346 */
babe0791 1347 msgs[0] = PyLong_AsVoidPtr(py_method_result);
6945df9a
SM
1348 *count = 1;
1349
1350 /* Clear potential overflow error; should never happen */
1351 BT_ASSERT(!PyErr_Occurred());
1352 goto end;
1353
1354end:
1355 Py_XDECREF(py_method_result);
1356 return status;
1357}
1358
1359static bt_self_component_status
1360bt_py3_component_class_sink_consume(bt_self_component_sink *self_component_sink)
1361{
1362 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1363 PyObject *py_comp = bt_self_component_get_data(self_component);
1364 PyObject *py_method_result = NULL;
1365 bt_self_component_status status;
1366
1367 BT_ASSERT(py_comp);
1368 py_method_result = PyObject_CallMethod(py_comp,
1369 "_consume", NULL);
1370
1371 status = bt_py3_exc_to_self_component_status();
1372 if (!py_method_result && status == BT_SELF_COMPONENT_STATUS_OK) {
1373 /* Pretty sure this should never happen, but just in case */
1374 BT_LOGE("User's _consume() method failed without raising an exception: "
1375 "status=%d", status);
1376 status = BT_SELF_COMPONENT_STATUS_ERROR;
1377 }
1378
1379 Py_XDECREF(py_method_result);
1380 return status;
1381}
1382
1383static
1384int bt_py3_component_class_set_help_and_desc(
1385 bt_component_class *component_class,
1386 const char *description, const char *help)
1387{
1388 int ret;
1389
1390 if (description) {
1391 ret = bt_component_class_set_description(component_class, description);
1392 if (ret) {
1393 BT_LOGE("Cannot set component class's description: "
1394 "comp-cls-addr=%p", component_class);
1395 goto end;
1396 }
1397 }
1398
1399 if (help) {
1400 ret = bt_component_class_set_help(component_class, help);
1401 if (ret) {
1402 BT_LOGE("Cannot set component class's help text: "
1403 "comp-cls-addr=%p", component_class);
1404 goto end;
1405 }
1406 }
1407
1408 ret = 0;
1409
1410end:
1411 return ret;
1412}
1413
1414static
1415bt_component_class_source *bt_py3_component_class_source_create(
1416 PyObject *py_cls, const char *name, const char *description,
1417 const char *help)
1418{
1419 bt_component_class_source *component_class_source;
1420 bt_component_class *component_class;
1421 int ret;
1422
1423 BT_ASSERT(py_cls);
1424
1425 component_class_source = bt_component_class_source_create(name,
1426 bt_py3_component_class_message_iterator_next);
1427 if (!component_class_source) {
1428 BT_LOGE_STR("Cannot create source component class.");
1429 goto end;
1430 }
1431
1432 component_class = bt_component_class_source_as_component_class(component_class_source);
1433
1434 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1435 goto end;
1436 }
1437
1438 ret = bt_component_class_source_set_init_method(component_class_source, bt_py3_component_class_source_init);
1439 BT_ASSERT(ret == 0);
1440 ret = bt_component_class_source_set_finalize_method (component_class_source, bt_py3_component_class_source_finalize);
1441 BT_ASSERT(ret == 0);
6945df9a
SM
1442 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1443 bt_py3_component_class_source_output_port_connected);
1444 BT_ASSERT(ret == 0);
1445 ret = bt_component_class_source_set_query_method(component_class_source, bt_py3_component_class_source_query);
1446 BT_ASSERT(ret == 0);
1447 ret = bt_component_class_source_set_message_iterator_init_method(
1448 component_class_source, bt_py3_component_class_source_message_iterator_init);
1449 BT_ASSERT(ret == 0);
1450 ret = bt_component_class_source_set_message_iterator_finalize_method(
1451 component_class_source, bt_py3_component_class_message_iterator_finalize);
1452 BT_ASSERT(ret == 0);
1453
1454 register_cc_ptr_to_py_cls(component_class, py_cls);
1455
1456end:
1457 return component_class_source;
1458}
1459
1460static
1461bt_component_class_filter *bt_py3_component_class_filter_create(
1462 PyObject *py_cls, const char *name, const char *description,
1463 const char *help)
1464{
1465 bt_component_class *component_class;
1466 bt_component_class_filter *component_class_filter;
1467 int ret;
1468
1469 BT_ASSERT(py_cls);
1470
1471 component_class_filter = bt_component_class_filter_create(name,
1472 bt_py3_component_class_message_iterator_next);
1473 if (!component_class_filter) {
1474 BT_LOGE_STR("Cannot create filter component class.");
1475 goto end;
1476 }
1477
1478 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1479
1480 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1481 goto end;
1482 }
1483
1484 ret = bt_component_class_filter_set_init_method(component_class_filter, bt_py3_component_class_filter_init);
1485 BT_ASSERT(ret == 0);
1486 ret = bt_component_class_filter_set_finalize_method (component_class_filter, bt_py3_component_class_filter_finalize);
1487 BT_ASSERT(ret == 0);
6945df9a
SM
1488 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1489 bt_py3_component_class_filter_input_port_connected);
1490 BT_ASSERT(ret == 0);
1491 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1492 bt_py3_component_class_filter_output_port_connected);
1493 BT_ASSERT(ret == 0);
1494 ret = bt_component_class_filter_set_query_method(component_class_filter, bt_py3_component_class_filter_query);
1495 BT_ASSERT(ret == 0);
1496 ret = bt_component_class_filter_set_message_iterator_init_method(
1497 component_class_filter, bt_py3_component_class_filter_message_iterator_init);
1498 BT_ASSERT(ret == 0);
1499 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1500 component_class_filter, bt_py3_component_class_message_iterator_finalize);
1501 BT_ASSERT(ret == 0);
1502
1503 register_cc_ptr_to_py_cls(component_class, py_cls);
1504
1505end:
1506 return component_class_filter;
1507}
1508
1509static
1510bt_component_class_sink *bt_py3_component_class_sink_create(
1511 PyObject *py_cls, const char *name, const char *description,
1512 const char *help)
1513{
1514 bt_component_class_sink *component_class_sink;
1515 bt_component_class *component_class;
1516 int ret;
1517
1518 BT_ASSERT(py_cls);
1519
1520 component_class_sink = bt_component_class_sink_create(name, bt_py3_component_class_sink_consume);
1521
1522 if (!component_class_sink) {
1523 BT_LOGE_STR("Cannot create sink component class.");
1524 goto end;
1525 }
1526
1527 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1528
1529 if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) {
1530 goto end;
1531 }
1532
1533 ret = bt_component_class_sink_set_init_method(component_class_sink, bt_py3_component_class_sink_init);
1534 BT_ASSERT(ret == 0);
1535 ret = bt_component_class_sink_set_finalize_method(component_class_sink, bt_py3_component_class_sink_finalize);
1536 BT_ASSERT(ret == 0);
6945df9a
SM
1537 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1538 bt_py3_component_class_sink_input_port_connected);
1539 BT_ASSERT(ret == 0);
1540 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1541 bt_py3_component_class_sink_graph_is_configured);
1542 BT_ASSERT(ret == 0);
1543 ret = bt_component_class_sink_set_query_method(component_class_sink, bt_py3_component_class_sink_query);
1544 BT_ASSERT(ret == 0);
1545
1546 register_cc_ptr_to_py_cls(component_class, py_cls);
1547
1548end:
1549 return component_class_sink;
1550}
1551%}
1552
1553struct bt_component_class_source *bt_py3_component_class_source_create(
1554 PyObject *py_cls, const char *name, const char *description,
1555 const char *help);
1556struct bt_component_class_filter *bt_py3_component_class_filter_create(
1557 PyObject *py_cls, const char *name, const char *description,
1558 const char *help);
1559struct bt_component_class_sink *bt_py3_component_class_sink_create(
1560 PyObject *py_cls, const char *name, const char *description,
1561 const char *help);
1562void bt_py3_cc_init_from_bt2(void);
1563void bt_py3_cc_exit_handler(void);
This page took 0.090158 seconds and 4 git commands to generate.