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