bt2: normalize the code to use some commonly used patterns
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i.h
CommitLineData
4212232c
PP
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "logging/comp-logging.h"
66964f3f 26#include "compat/glib.h"
4212232c
PP
27
28/*
29 * This hash table associates a BT component class object address to a
30 * user-defined Python class (PyObject *). The keys and values are NOT
31 * owned by this hash table. The Python class objects are owned by the
32 * Python module, which should not be unloaded until it is not possible
33 * to create a user Python component anyway.
34 *
35 * This hash table is written to when a user-defined Python component
36 * class is created by one of the bt_bt2_component_class_*_create()
37 * functions.
38 *
39 * This function is read from when a user calls bt_component_create()
40 * with a component class pointer created by one of the functions above.
41 * In this case, the original Python class needs to be found to
42 * instantiate it and associate the created Python component object with
43 * a BT component object instance.
44 */
45
46static GHashTable *bt_cc_ptr_to_py_cls;
47
ab1cea3f
PP
48static
49void bt_bt2_unregister_cc_ptr_to_py_cls(const bt_component_class *comp_cls)
50{
51 gboolean existed;
52
53 if (!bt_cc_ptr_to_py_cls) {
54 return;
55 }
56
57 existed = g_hash_table_remove(bt_cc_ptr_to_py_cls, comp_cls);
58 BT_ASSERT(existed);
59}
60
4212232c
PP
61static
62void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
63 PyObject *py_cls)
64{
65 if (!bt_cc_ptr_to_py_cls) {
66 /*
67 * Lazy-initializing this GHashTable because GLib
68 * might not be initialized yet and it needs to be
69 * before we call g_hash_table_new()
70 */
71 BT_LOGD_STR("Creating native component class to Python component class hash table.");
72 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
73 BT_ASSERT(bt_cc_ptr_to_py_cls);
74 }
75
76 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
77 (gpointer) py_cls);
78}
79
80static
81PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
82{
83 if (!bt_cc_ptr_to_py_cls) {
84 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
85 "comp-cls-addr=%p", bt_cc);
86 return NULL;
87 }
88
89 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
90 (gconstpointer) bt_cc);
91}
92
4212232c
PP
93/* Library destructor */
94
95__attribute__((destructor))
96static
97void native_comp_class_dtor(void) {
98 /* Destroy component class association hash table */
99 if (bt_cc_ptr_to_py_cls) {
100 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
101 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
ab1cea3f 102 bt_cc_ptr_to_py_cls = NULL;
4212232c
PP
103 }
104}
105
4212232c 106static inline
84e438af
SM
107int py_exc_to_status_clear(
108 bt_self_component_class *self_component_class,
4212232c
PP
109 bt_self_component *self_component,
110 bt_self_message_iterator *self_message_iterator,
111 const char *module_name, int active_log_level)
112{
6dbc1938 113 int status;
4212232c
PP
114 PyObject *exc = PyErr_Occurred();
115
116 if (!exc) {
6dbc1938 117 status = __BT_FUNC_STATUS_OK;
4212232c
PP
118 goto end;
119 }
120
121 if (PyErr_GivenExceptionMatches(exc,
122 py_mod_bt2_exc_try_again_type)) {
123 status = __BT_FUNC_STATUS_AGAIN;
124 } else if (PyErr_GivenExceptionMatches(exc,
125 py_mod_bt2_exc_stop_type)) {
126 status = __BT_FUNC_STATUS_END;
127 } else if (PyErr_GivenExceptionMatches(exc,
128 py_mod_bt2_exc_unknown_object_type)) {
129 status = __BT_FUNC_STATUS_UNKNOWN_OBJECT;
130 } else {
131 /*
132 * Unknown exception: convert to general error.
133 *
134 * Because we only want to fetch the log level when
135 * we actually get an exception, and not systematically
136 * when we call py_exc_to_status() (as py_exc_to_status()
137 * can return `__BT_FUNC_STATUS_OK`), we get it here
138 * depending on the actor's type.
139 */
140 if (self_component) {
141 active_log_level = get_self_component_log_level(
142 self_component);
143 } else if (self_message_iterator) {
144 active_log_level = get_self_message_iterator_log_level(
145 self_message_iterator);
146 }
147
148 BT_ASSERT(active_log_level != -1);
84e438af 149 log_exception_and_maybe_append_cause(BT_LOG_WARNING,
4212232c
PP
150 active_log_level, true,
151 self_component_class, self_component,
152 self_message_iterator, module_name);
153
154 if (PyErr_GivenExceptionMatches(exc,
155 py_mod_bt2_exc_memory_error)) {
156 status = __BT_FUNC_STATUS_MEMORY_ERROR;
157 } else {
158 status = __BT_FUNC_STATUS_ERROR;
159 }
160 }
161
162end:
163 PyErr_Clear();
164 return status;
165}
166
167static
84e438af 168int py_exc_to_status_component_class_clear(
4212232c
PP
169 bt_self_component_class *self_component_class,
170 int active_log_level)
171{
84e438af 172 return py_exc_to_status_clear(self_component_class, NULL, NULL, NULL,
4212232c
PP
173 active_log_level);
174}
175
176static
84e438af 177int py_exc_to_status_component_clear(bt_self_component *self_component)
4212232c 178{
84e438af 179 return py_exc_to_status_clear(NULL, self_component, NULL, NULL, -1);
4212232c
PP
180}
181
182static
84e438af 183int py_exc_to_status_message_iterator_clear(
4212232c
PP
184 bt_self_message_iterator *self_message_iterator)
185{
84e438af 186 return py_exc_to_status_clear(NULL, NULL, self_message_iterator, NULL, -1);
4212232c
PP
187}
188
66964f3f
PP
189static
190bool bt_bt2_is_python_component_class(const bt_component_class *comp_cls)
191{
192 return bt_g_hash_table_contains(bt_cc_ptr_to_py_cls, comp_cls);
193}
194
4212232c
PP
195/* Component class proxy methods (delegate to the attached Python object) */
196
197static
21a9f056 198bt_component_class_initialize_method_status component_class_init(
4212232c
PP
199 bt_self_component *self_component,
200 void *self_component_v,
201 swig_type_info *self_comp_cls_type_swig_type,
202 const bt_value *params,
203 void *init_method_data)
204{
205 const bt_component *component = bt_self_component_as_component(self_component);
206 const bt_component_class *component_class = bt_component_borrow_class_const(component);
6dbc1938 207 bt_component_class_initialize_method_status status;
4212232c
PP
208 PyObject *py_cls = NULL;
209 PyObject *py_comp = NULL;
210 PyObject *py_params_ptr = NULL;
211 PyObject *py_comp_ptr = NULL;
212 bt_logging_level log_level = get_self_component_log_level(
213 self_component);
214
4212232c
PP
215 BT_ASSERT(self_component);
216 BT_ASSERT(self_component_v);
217 BT_ASSERT(self_comp_cls_type_swig_type);
218
219 /*
220 * Get the user-defined Python class which created this
221 * component's class in the first place (borrowed
222 * reference).
223 */
224 py_cls = lookup_cc_ptr_to_py_cls(component_class);
225 if (!py_cls) {
226 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
227 "Cannot find Python class associated to native component class: "
228 "comp-cls-addr=%p", component_class);
229 goto error;
230 }
231
232 /* Parameters pointer -> SWIG pointer Python object */
233 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
234 SWIGTYPE_p_bt_value, 0);
235 if (!py_params_ptr) {
236 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
237 "Failed to create a SWIG pointer object.");
238 goto error;
239 }
240
241 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
242 self_comp_cls_type_swig_type, 0);
243 if (!py_comp_ptr) {
244 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
245 "Failed to create a SWIG pointer object.");
246 goto error;
247 }
248
249 /*
250 * Do the equivalent of this:
251 *
66964f3f
PP
252 * py_comp = py_cls._bt_init_from_native(py_comp_ptr,
253 * py_params_ptr, init_method_data ? init_method_data : Py_None)
4212232c
PP
254 *
255 * _UserComponentType._bt_init_from_native() calls the Python
256 * component object's __init__() function.
66964f3f
PP
257 *
258 * We don't take any reference on `init_method_data` which, if
259 * not `NULL`, is assumed to be a `PyObject *`: the user's
260 * __init__() function will eventually take a reference if
261 * needed. If `init_method_data` is `NULL`, then we pass
262 * `Py_None` as the initialization's Python object.
4212232c
PP
263 */
264 py_comp = PyObject_CallMethod(py_cls,
66964f3f
PP
265 "_bt_init_from_native", "(OOO)", py_comp_ptr, py_params_ptr,
266 init_method_data ? init_method_data : Py_None);
4212232c
PP
267 if (!py_comp) {
268 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component,
269 "Failed to call Python class's _bt_init_from_native() method: "
270 "py-cls-addr=%p", py_cls);
84e438af 271 status = py_exc_to_status_component_clear(self_component);
4212232c
PP
272 goto end;
273 }
274
275 /*
276 * Our user Python component object is now fully created and
277 * initialized by the user. Since we just created it, this
278 * native component is its only (persistent) owner.
279 */
280 bt_self_component_set_data(self_component, py_comp);
281 py_comp = NULL;
6dbc1938
SM
282
283 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
284
4212232c
PP
285 goto end;
286
287error:
6dbc1938
SM
288 /* This error path is for non-Python errors only. */
289 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
4212232c
PP
290
291end:
6dbc1938 292 BT_ASSERT(!PyErr_Occurred());
4212232c
PP
293 Py_XDECREF(py_comp);
294 Py_XDECREF(py_params_ptr);
295 Py_XDECREF(py_comp_ptr);
296 return status;
297}
298
fd57054b
PP
299static
300bt_component_class_get_supported_mip_versions_method_status
301component_class_get_supported_mip_versions(
302 const bt_component_class *component_class,
303 bt_self_component_class *self_component_class,
304 const bt_value *params, void *init_method_data,
305 bt_logging_level log_level,
306 bt_integer_range_set_unsigned *supported_versions)
307{
308 uint64_t i;
309 PyObject *py_cls = NULL;
310 PyObject *py_params_ptr = NULL;
311 PyObject *py_range_set_addr = NULL;
312 bt_integer_range_set_unsigned *ret_range_set = NULL;
6dbc1938 313 bt_component_class_get_supported_mip_versions_method_status status;
fd57054b
PP
314
315 py_cls = lookup_cc_ptr_to_py_cls(component_class);
316 if (!py_cls) {
317 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
318 "Cannot find Python class associated to native component class: "
319 "comp-cls-addr=%p", component_class);
320 goto error;
321 }
322
323 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
324 SWIGTYPE_p_bt_value, 0);
325 if (!py_params_ptr) {
326 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
327 "Failed to create a SWIG pointer object.");
328 goto error;
329 }
330
331 /*
332 * We don't take any reference on `init_method_data` which, if
333 * not `NULL`, is assumed to be a `PyObject *`: the user's
334 * _user_get_supported_mip_versions() function will eventually
335 * take a reference if needed. If `init_method_data` is `NULL`,
336 * then we pass `Py_None` as the initialization's Python object.
337 */
338 py_range_set_addr = PyObject_CallMethod(py_cls,
339 "_bt_get_supported_mip_versions_from_native", "(OOi)",
340 py_params_ptr, init_method_data ? init_method_data : Py_None,
341 (int) log_level);
342 if (!py_range_set_addr) {
343 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG,
344 "Failed to call Python class's _bt_get_supported_mip_versions_from_native() method: "
345 "py-cls-addr=%p", py_cls);
84e438af 346 status = py_exc_to_status_component_class_clear(self_component_class,
fd57054b
PP
347 log_level);
348 goto end;
349 }
350
351 /*
352 * The returned object, on success, is an integer object
353 * (PyLong) containing the address of a BT unsigned integer
354 * range set object (new reference).
355 */
356 ret_range_set = PyLong_AsVoidPtr(py_range_set_addr);
357 BT_ASSERT(!PyErr_Occurred());
358 BT_ASSERT(ret_range_set);
359
360 /* Copy returned ranges to input range set */
361 for (i = 0; i < bt_integer_range_set_get_range_count(
362 bt_integer_range_set_unsigned_as_range_set_const(ret_range_set));
363 i++) {
364 const bt_integer_range_unsigned *range =
365 bt_integer_range_set_unsigned_borrow_range_by_index_const(
366 ret_range_set, i);
367 bt_integer_range_set_add_range_status add_range_status;
368
369 add_range_status = bt_integer_range_set_unsigned_add_range(
370 supported_versions,
371 bt_integer_range_unsigned_get_lower(range),
372 bt_integer_range_unsigned_get_upper(range));
373 if (add_range_status) {
374 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
375 "Failed to add range to supported MIP versions range set.");
376 goto error;
377 }
378 }
379
6dbc1938
SM
380 status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_OK;
381
fd57054b
PP
382 goto end;
383
384error:
6dbc1938
SM
385 /* This error path is for non-Python errors only. */
386 status = BT_COMPONENT_CLASS_GET_SUPPORTED_MIP_VERSIONS_METHOD_STATUS_ERROR;
fd57054b
PP
387
388end:
6dbc1938 389 BT_ASSERT(!PyErr_Occurred());
fd57054b
PP
390 Py_XDECREF(py_params_ptr);
391 Py_XDECREF(py_range_set_addr);
392 bt_integer_range_set_unsigned_put_ref(ret_range_set);
393 return status;
394}
395
396static
397bt_component_class_get_supported_mip_versions_method_status
398component_class_source_get_supported_mip_versions(
399 bt_self_component_class_source *self_component_class_source,
400 const bt_value *params, void *init_method_data,
401 bt_logging_level log_level,
402 bt_integer_range_set_unsigned *supported_versions)
403{
404 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
405 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
406 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
407
408 return component_class_get_supported_mip_versions(
409 component_class, self_component_class,
410 params, init_method_data, log_level, supported_versions);
411}
412
413static
414bt_component_class_get_supported_mip_versions_method_status
415component_class_filter_get_supported_mip_versions(
416 bt_self_component_class_filter *self_component_class_filter,
417 const bt_value *params, void *init_method_data,
418 bt_logging_level log_level,
419 bt_integer_range_set_unsigned *supported_versions)
420{
421 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
422 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
423 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
424
425 return component_class_get_supported_mip_versions(
426 component_class, self_component_class,
427 params, init_method_data, log_level, supported_versions);
428}
429
430static
431bt_component_class_get_supported_mip_versions_method_status
432component_class_sink_get_supported_mip_versions(
433 bt_self_component_class_sink *self_component_class_sink,
434 const bt_value *params, void *init_method_data,
435 bt_logging_level log_level,
436 bt_integer_range_set_unsigned *supported_versions)
437{
438 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
439 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
440 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
441
442 return component_class_get_supported_mip_versions(
443 component_class, self_component_class,
444 params, init_method_data, log_level, supported_versions);
445}
446
4212232c
PP
447/*
448 * Method of bt_component_class_source to initialize a bt_self_component_source
449 * of that class.
450 */
451
452static
21a9f056 453bt_component_class_initialize_method_status component_class_source_init(
4212232c 454 bt_self_component_source *self_component_source,
59225a3e 455 bt_self_component_source_configuration *config,
4212232c
PP
456 const bt_value *params, void *init_method_data)
457{
458 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
459 return component_class_init(
460 self_component,
461 self_component_source,
462 SWIGTYPE_p_bt_self_component_source,
463 params, init_method_data);
464}
465
466static
21a9f056 467bt_component_class_initialize_method_status component_class_filter_init(
4212232c 468 bt_self_component_filter *self_component_filter,
59225a3e 469 bt_self_component_filter_configuration *config,
4212232c
PP
470 const bt_value *params, void *init_method_data)
471{
472 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
473 return component_class_init(
474 self_component,
475 self_component_filter,
476 SWIGTYPE_p_bt_self_component_filter,
477 params, init_method_data);
478}
479
480static
21a9f056 481bt_component_class_initialize_method_status component_class_sink_init(
4212232c 482 bt_self_component_sink *self_component_sink,
59225a3e 483 bt_self_component_sink_configuration *config,
4212232c
PP
484 const bt_value *params, void *init_method_data)
485{
486 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
487 return component_class_init(
488 self_component,
489 self_component_sink,
490 SWIGTYPE_p_bt_self_component_sink,
491 params, init_method_data);
492}
493
494static
495void component_class_finalize(bt_self_component *self_component)
496{
497 PyObject *py_comp = bt_self_component_get_data(self_component);
6dbc1938
SM
498 PyObject *py_method_result;
499
4212232c
PP
500 BT_ASSERT(py_comp);
501
502 /* Call user's _user_finalize() method */
6dbc1938
SM
503 py_method_result = PyObject_CallMethod(py_comp, "_user_finalize", NULL);
504 if (!py_method_result) {
4212232c
PP
505 bt_logging_level log_level = get_self_component_log_level(
506 self_component);
507
508 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_component,
509 "User component's _user_finalize() method raised an exception: ignoring:");
510 logw_exception(log_level);
6dbc1938
SM
511
512 /*
513 * Ignore any exception raised by the _user_finalize() method
514 * because it won't change anything at this point: the component
515 * is being destroyed anyway.
516 */
517 PyErr_Clear();
518 goto end;
4212232c
PP
519 }
520
6dbc1938
SM
521 BT_ASSERT(py_method_result == Py_None);
522
523end:
4212232c
PP
524 Py_XDECREF(py_method_result);
525 Py_DECREF(py_comp);
526}
527
528static
529void component_class_source_finalize(bt_self_component_source *self_component_source)
530{
531 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
532 component_class_finalize(self_component);
533}
534
535static
536void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
537{
538 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
539 component_class_finalize(self_component);
540}
541
542static
543void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
544{
545 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
546 component_class_finalize(self_component);
547}
548
549static
f2fb1b32
SM
550bt_component_class_message_iterator_can_seek_beginning_method_status
551component_class_can_seek_beginning(
552 bt_self_message_iterator *self_message_iterator, bt_bool *can_seek)
4212232c
PP
553{
554 PyObject *py_iter;
555 PyObject *py_result = NULL;
f2fb1b32 556 bt_component_class_message_iterator_can_seek_beginning_method_status status;
4212232c 557 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
6dbc1938 558
4212232c
PP
559 BT_ASSERT(py_iter);
560
14cfc8ce
SM
561 py_result = PyObject_CallMethod(py_iter,
562 "_bt_can_seek_beginning_from_native", NULL);
6dbc1938 563 if (!py_result) {
84e438af 564 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
6dbc1938 565 goto end;
4212232c
PP
566 }
567
6dbc1938
SM
568 BT_ASSERT(PyBool_Check(py_result));
569 *can_seek = PyObject_IsTrue(py_result);
570
571 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
572
573end:
4212232c
PP
574 Py_XDECREF(py_result);
575
f2fb1b32 576 return status;
4212232c
PP
577}
578
579static
580bt_component_class_message_iterator_seek_beginning_method_status
581component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
582{
583 PyObject *py_iter;
584 PyObject *py_result;
585 bt_component_class_message_iterator_seek_beginning_method_status status;
586
587 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
588 BT_ASSERT(py_iter);
6dbc1938
SM
589
590 py_result = PyObject_CallMethod(py_iter,
591 "_bt_seek_beginning_from_native",
4212232c 592 NULL);
6dbc1938
SM
593 if (!py_result) {
594 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
595 goto end;
596 }
597
598 BT_ASSERT(py_result == Py_None);
599
600 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK;
601
602end:
4212232c 603 Py_XDECREF(py_result);
6dbc1938 604
4212232c
PP
605 return status;
606}
607
c182d7dd
SM
608static
609bt_component_class_message_iterator_can_seek_ns_from_origin_method_status
610component_class_can_seek_ns_from_origin(
611 bt_self_message_iterator *self_message_iterator,
612 int64_t ns_from_origin, bt_bool *can_seek)
613{
614 PyObject *py_iter;
615 PyObject *py_result = NULL;
616 bt_component_class_message_iterator_can_seek_ns_from_origin_method_status status;
6dbc1938 617
c182d7dd
SM
618 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
619 BT_ASSERT(py_iter);
620
621 py_result = PyObject_CallMethod(py_iter,
622 "_bt_can_seek_ns_from_origin_from_native", "L", ns_from_origin);
6dbc1938 623 if (!py_result) {
84e438af 624 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
6dbc1938 625 goto end;
c182d7dd
SM
626 }
627
6dbc1938
SM
628 BT_ASSERT(PyBool_Check(py_result));
629 *can_seek = PyObject_IsTrue(py_result);
630
631 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
632
633end:
c182d7dd
SM
634 Py_XDECREF(py_result);
635
636 return status;
637}
638
639static
640bt_component_class_message_iterator_seek_ns_from_origin_method_status
641component_class_seek_ns_from_origin(
642 bt_self_message_iterator *self_message_iterator,
643 int64_t ns_from_origin)
644{
645 PyObject *py_iter;
646 PyObject *py_result;
647 bt_component_class_message_iterator_seek_ns_from_origin_method_status status;
648
649 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
650 BT_ASSERT(py_iter);
6dbc1938 651
c182d7dd
SM
652 py_result = PyObject_CallMethod(py_iter,
653 "_bt_seek_ns_from_origin_from_native", "L", ns_from_origin);
6dbc1938
SM
654 if (!py_result) {
655 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
656 goto end;
657 }
658
659
660 BT_ASSERT(py_result == Py_None);
661
662 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK;
663
664end:
c182d7dd 665 Py_XDECREF(py_result);
6dbc1938 666
c182d7dd
SM
667 return status;
668}
669
4212232c
PP
670static
671bt_component_class_port_connected_method_status component_class_port_connected(
672 bt_self_component *self_component,
673 void *self_component_port,
674 swig_type_info *self_component_port_swig_type,
675 bt_port_type self_component_port_type,
676 const void *other_port,
677 swig_type_info *other_port_swig_type)
678{
679 bt_component_class_port_connected_method_status status;
680 PyObject *py_comp = NULL;
681 PyObject *py_self_port_ptr = NULL;
682 PyObject *py_other_port_ptr = NULL;
683 PyObject *py_method_result = NULL;
684 bt_logging_level log_level = get_self_component_log_level(
685 self_component);
686
687 py_comp = bt_self_component_get_data(self_component);
688 BT_ASSERT(py_comp);
689 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
690 self_component_port_swig_type, 0);
691 if (!py_self_port_ptr) {
692 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
693 "Failed to create a SWIG pointer object.");
694 status = __BT_FUNC_STATUS_MEMORY_ERROR;
695 goto end;
696 }
697
698 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
699 other_port_swig_type, 0);
700 if (!py_other_port_ptr) {
701 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
702 "Failed to create a SWIG pointer object.");
703 status = __BT_FUNC_STATUS_MEMORY_ERROR;
6dbc1938
SM
704 goto end;
705 }
4212232c
PP
706
707 py_method_result = PyObject_CallMethod(py_comp,
708 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
709 self_component_port_type, py_other_port_ptr);
6dbc1938
SM
710 if (!py_method_result) {
711 status = py_exc_to_status_component_clear(self_component);
712 goto end;
713 }
714
715 BT_ASSERT(py_method_result == Py_None);
716
717 status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK;
4212232c
PP
718
719end:
720 Py_XDECREF(py_self_port_ptr);
721 Py_XDECREF(py_other_port_ptr);
722 Py_XDECREF(py_method_result);
6dbc1938 723
4212232c
PP
724 return status;
725}
726
727static
728bt_component_class_port_connected_method_status
729component_class_source_output_port_connected(
730 bt_self_component_source *self_component_source,
731 bt_self_component_port_output *self_component_port_output,
732 const bt_port_input *other_port_input)
733{
734 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
735
736 return component_class_port_connected(
737 self_component,
738 self_component_port_output,
739 SWIGTYPE_p_bt_self_component_port_output,
740 BT_PORT_TYPE_OUTPUT,
741 other_port_input,
742 SWIGTYPE_p_bt_port_input);
743}
744
745static
746bt_component_class_port_connected_method_status
747component_class_filter_input_port_connected(
748 bt_self_component_filter *self_component_filter,
749 bt_self_component_port_input *self_component_port_input,
750 const bt_port_output *other_port_output)
751{
752 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
753
754 return component_class_port_connected(
755 self_component,
756 self_component_port_input,
757 SWIGTYPE_p_bt_self_component_port_input,
758 BT_PORT_TYPE_INPUT,
759 other_port_output,
760 SWIGTYPE_p_bt_port_output);
761}
762
763static
764bt_component_class_port_connected_method_status
765component_class_filter_output_port_connected(
766 bt_self_component_filter *self_component_filter,
767 bt_self_component_port_output *self_component_port_output,
768 const bt_port_input *other_port_input)
769{
770 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
771
772 return component_class_port_connected(
773 self_component,
774 self_component_port_output,
775 SWIGTYPE_p_bt_self_component_port_output,
776 BT_PORT_TYPE_OUTPUT,
777 other_port_input,
778 SWIGTYPE_p_bt_port_input);
779}
780
781static
782bt_component_class_port_connected_method_status
783component_class_sink_input_port_connected(
784 bt_self_component_sink *self_component_sink,
785 bt_self_component_port_input *self_component_port_input,
786 const bt_port_output *other_port_output)
787{
788 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
789
790 return component_class_port_connected(
791 self_component,
792 self_component_port_input,
793 SWIGTYPE_p_bt_self_component_port_input,
794 BT_PORT_TYPE_INPUT,
795 other_port_output,
796 SWIGTYPE_p_bt_port_output);
797}
798
799static
800bt_component_class_sink_graph_is_configured_method_status
801component_class_sink_graph_is_configured(
802 bt_self_component_sink *self_component_sink)
803{
804 PyObject *py_comp = NULL;
805 PyObject *py_method_result = NULL;
6dbc1938 806 bt_component_class_sink_graph_is_configured_method_status status;
4212232c
PP
807 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
808
809 py_comp = bt_self_component_get_data(self_component);
6dbc1938 810
4212232c
PP
811 py_method_result = PyObject_CallMethod(py_comp,
812 "_bt_graph_is_configured_from_native", NULL);
6dbc1938
SM
813 if (!py_method_result) {
814 status = py_exc_to_status_component_clear(self_component);
815 goto end;
816 }
817
818 BT_ASSERT(py_method_result == Py_None);
819
820 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;;
821
822end:
4212232c
PP
823 Py_XDECREF(py_method_result);
824 return status;
825}
826
827static
828bt_component_class_query_method_status component_class_query(
829 const bt_component_class *component_class,
830 bt_self_component_class *self_component_class,
831 bt_private_query_executor *priv_query_executor,
7c14d641 832 const char *object, const bt_value *params, void *method_data,
4212232c
PP
833 const bt_value **result)
834{
835 PyObject *py_cls = NULL;
836 PyObject *py_params_ptr = NULL;
837 PyObject *py_priv_query_exec_ptr = NULL;
838 PyObject *py_query_func = NULL;
839 PyObject *py_object = NULL;
840 PyObject *py_results_addr = NULL;
841 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
842 const bt_query_executor *query_exec =
843 bt_private_query_executor_as_query_executor_const(
844 priv_query_executor);
845 bt_logging_level log_level =
846 bt_query_executor_get_logging_level(query_exec);
847
7c14d641
PP
848 /*
849 * If there's any `method_data`, assume this component class is
850 * getting queried from Python, so that `method_data` is a
851 * Python object to pass to the user's _user_query() method.
852 */
853 BT_ASSERT(!method_data ||
854 bt_bt2_is_python_component_class(component_class));
855
4212232c
PP
856 py_cls = lookup_cc_ptr_to_py_cls(component_class);
857 if (!py_cls) {
858 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
859 "Cannot find Python class associated to native component class: "
860 "comp-cls-addr=%p", component_class);
861 goto error;
862 }
863
864 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
865 SWIGTYPE_p_bt_value, 0);
866 if (!py_params_ptr) {
867 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
868 "Failed to create a SWIG pointer object.");
869 goto error;
870 }
871
872 py_priv_query_exec_ptr = SWIG_NewPointerObj(
873 SWIG_as_voidptr(priv_query_executor),
874 SWIGTYPE_p_bt_private_query_executor, 0);
875 if (!py_priv_query_exec_ptr) {
876 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
877 "Failed to create a SWIG pointer object.");
878 goto error;
879 }
880
881 py_object = SWIG_FromCharPtr(object);
882 if (!py_object) {
883 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
884 "Failed to create a Python string.");
885 goto error;
886 }
887
7c14d641
PP
888 /*
889 * We don't take any reference on `method_data` which, if not
890 * `NULL`, is assumed to be a `PyObject *`: the user's
891 * _user_query() function will eventually take a reference if
892 * needed. If `method_data` is `NULL`, then we pass `Py_None` as
893 * the initialization's Python object.
894 */
4212232c 895 py_results_addr = PyObject_CallMethod(py_cls,
7c14d641
PP
896 "_bt_query_from_native", "(OOOO)", py_priv_query_exec_ptr,
897 py_object, py_params_ptr,
898 method_data ? method_data : Py_None);
4212232c 899 if (!py_results_addr) {
84e438af 900 status = py_exc_to_status_component_class_clear(self_component_class,
4212232c 901 log_level);
561e7049
SM
902 if (status < 0) {
903 static const char *fmt =
904 "Failed to call Python class's _bt_query_from_native() method: py-cls-addr=%p";
905 BT_LOG_WRITE_CUR_LVL(BT_LOG_WARNING, log_level, BT_LOG_TAG,
906 fmt, py_cls);
907 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
908 self_component_class, fmt, py_cls);
909 }
4212232c
PP
910 goto end;
911 }
912
913 /*
914 * The returned object, on success, is an integer object
915 * (PyLong) containing the address of a BT value object (new
916 * reference).
917 */
918 *result = PyLong_AsVoidPtr(py_results_addr);
919 BT_ASSERT(!PyErr_Occurred());
920 BT_ASSERT(*result);
921 goto end;
922
923error:
924 PyErr_Clear();
925 status = __BT_FUNC_STATUS_ERROR;
926
927end:
928 Py_XDECREF(py_params_ptr);
929 Py_XDECREF(py_priv_query_exec_ptr);
930 Py_XDECREF(py_query_func);
931 Py_XDECREF(py_object);
932 Py_XDECREF(py_results_addr);
933 return status;
934}
935
936static
937bt_component_class_query_method_status component_class_source_query(
938 bt_self_component_class_source *self_component_class_source,
939 bt_private_query_executor *priv_query_executor,
7c14d641 940 const char *object, const bt_value *params, void *method_data,
4212232c
PP
941 const bt_value **result)
942{
943 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
944 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
945 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
946
947 return component_class_query(component_class, self_component_class,
7c14d641 948 priv_query_executor, object, params, method_data, result);
4212232c
PP
949}
950
951static
952bt_component_class_query_method_status component_class_filter_query(
953 bt_self_component_class_filter *self_component_class_filter,
954 bt_private_query_executor *priv_query_executor,
7c14d641 955 const char *object, const bt_value *params, void *method_data,
4212232c
PP
956 const bt_value **result)
957{
958 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
959 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
960 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
961
962 return component_class_query(component_class, self_component_class,
7c14d641 963 priv_query_executor, object, params, method_data, result);
4212232c
PP
964}
965
966static
967bt_component_class_query_method_status component_class_sink_query(
968 bt_self_component_class_sink *self_component_class_sink,
969 bt_private_query_executor *priv_query_executor,
7c14d641 970 const char *object, const bt_value *params, void *method_data,
4212232c
PP
971 const bt_value **result)
972{
973 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
974 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
975 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
976
977 return component_class_query(component_class, self_component_class,
7c14d641 978 priv_query_executor, object, params, method_data, result);
4212232c
PP
979}
980
981static
21a9f056 982bt_component_class_message_iterator_initialize_method_status
4212232c
PP
983component_class_message_iterator_init(
984 bt_self_message_iterator *self_message_iterator,
8d8b141d 985 bt_self_message_iterator_configuration *config,
4212232c
PP
986 bt_self_component *self_component,
987 bt_self_component_port_output *self_component_port_output)
988{
21a9f056 989 bt_component_class_message_iterator_initialize_method_status status = __BT_FUNC_STATUS_OK;
4212232c
PP
990 PyObject *py_comp_cls = NULL;
991 PyObject *py_iter_cls = NULL;
992 PyObject *py_iter_ptr = NULL;
8d8b141d 993 PyObject *py_config_ptr = NULL;
4212232c
PP
994 PyObject *py_component_port_output_ptr = NULL;
995 PyObject *py_init_method_result = NULL;
996 PyObject *py_iter = NULL;
997 PyObject *py_comp;
998 bt_logging_level log_level = get_self_component_log_level(
999 self_component);
1000
1001 py_comp = bt_self_component_get_data(self_component);
1002
1003 /* Find user's Python message iterator class */
1004 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
1005 if (!py_comp_cls) {
1006 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1007 "Cannot get Python object's `__class__` attribute.");
1008 goto python_error;
1009 }
1010
1011 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
1012 if (!py_iter_cls) {
1013 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1014 "Cannot get Python class's `_iter_cls` attribute.");
1015 goto python_error;
1016 }
1017
1018 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
1019 SWIGTYPE_p_bt_self_message_iterator, 0);
1020 if (!py_iter_ptr) {
1021 const char *err = "Failed to create a SWIG pointer object.";
1022
1023 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1024 "%s", err);
1025 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1026 self_message_iterator, err);
1027 goto error;
1028 }
1029
1030 /*
1031 * Create object with borrowed native message iterator
1032 * reference:
1033 *
1034 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
1035 */
1036 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
1037 "(OO)", py_iter_cls, py_iter_ptr);
1038 if (!py_iter) {
1039 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1040 "Failed to call Python class's __new__() method: "
1041 "py-cls-addr=%p", py_iter_cls);
1042 goto python_error;
1043 }
1044
1045 /*
1046 * Initialize object:
1047 *
8d8b141d 1048 * py_iter.__init__(config, self_output_port)
4212232c 1049 *
8d8b141d 1050 * through the _init_from_native helper static method.
4212232c
PP
1051 *
1052 * At this point, py_iter._ptr is set, so this initialization
1053 * function has access to self._component (which gives it the
1054 * user Python component object from which the iterator was
1055 * created).
1056 */
8d8b141d
SM
1057 py_config_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(config),
1058 SWIGTYPE_p_bt_self_message_iterator_configuration, 0);
1059 if (!py_config_ptr) {
1060 const char *err = "Failed to create a SWIG pointer object";
1061
1062 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1063 "%s", err);
1064 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1065 self_message_iterator, err);
1066 goto error;
1067 }
1068
4212232c
PP
1069 py_component_port_output_ptr = SWIG_NewPointerObj(
1070 SWIG_as_voidptr(self_component_port_output),
1071 SWIGTYPE_p_bt_self_component_port_output, 0);
1072 if (!py_component_port_output_ptr) {
1073 const char *err = "Failed to create a SWIG pointer object.";
1074
1075 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1076 "%s", err);
1077 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1078 self_message_iterator, err);
1079 goto error;
1080 }
1081
1082 py_init_method_result = PyObject_CallMethod(py_iter,
8d8b141d
SM
1083 "_bt_init_from_native", "OO", py_config_ptr,
1084 py_component_port_output_ptr);
4212232c
PP
1085 if (!py_init_method_result) {
1086 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component,
1087 "User's __init__() method failed:");
1088 goto python_error;
1089 }
1090
1091 /*
1092 * Since the Python code can never instantiate a user-defined
1093 * message iterator class, the native message iterator
1094 * object does NOT belong to a user Python message iterator
1095 * object (borrowed reference). However this Python object is
1096 * owned by this native message iterator object.
1097 *
1098 * In the Python world, the lifetime of the native message
1099 * iterator is managed by a _GenericMessageIterator
1100 * instance:
1101 *
1102 * _GenericMessageIterator instance:
1103 * owns a native bt_message_iterator object (iter)
1104 * owns a _UserMessageIterator instance (py_iter)
1105 * self._ptr is a borrowed reference to the
1106 * native bt_private_connection_private_message_iterator
1107 * object (iter)
1108 */
1109 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
1110 py_iter = NULL;
1111 goto end;
1112
1113python_error:
1114 /* Handling of errors that cause a Python exception to be set. */
84e438af 1115 status = py_exc_to_status_message_iterator_clear(self_message_iterator);
4212232c
PP
1116 goto end;
1117
1118error:
1119 /* Handling of errors that don't cause a Python exception to be set. */
1120 status = __BT_FUNC_STATUS_ERROR;
1121
1122end:
1123 BT_ASSERT(!PyErr_Occurred());
1124
1125 Py_XDECREF(py_comp_cls);
1126 Py_XDECREF(py_iter_cls);
1127 Py_XDECREF(py_iter_ptr);
1128 Py_XDECREF(py_component_port_output_ptr);
1129 Py_XDECREF(py_init_method_result);
1130 Py_XDECREF(py_iter);
1131 return status;
1132}
1133
1134static
21a9f056 1135bt_component_class_message_iterator_initialize_method_status
4212232c
PP
1136component_class_source_message_iterator_init(
1137 bt_self_message_iterator *self_message_iterator,
8d8b141d 1138 bt_self_message_iterator_configuration *config,
4212232c
PP
1139 bt_self_component_source *self_component_source,
1140 bt_self_component_port_output *self_component_port_output)
1141{
8d8b141d
SM
1142 bt_self_component *self_component =
1143 bt_self_component_source_as_self_component(self_component_source);
4212232c 1144
8d8b141d
SM
1145 return component_class_message_iterator_init(self_message_iterator,
1146 config, self_component, self_component_port_output);
4212232c
PP
1147}
1148
1149static
21a9f056 1150bt_component_class_message_iterator_initialize_method_status
4212232c
PP
1151component_class_filter_message_iterator_init(
1152 bt_self_message_iterator *self_message_iterator,
8d8b141d 1153 bt_self_message_iterator_configuration *config,
4212232c
PP
1154 bt_self_component_filter *self_component_filter,
1155 bt_self_component_port_output *self_component_port_output)
1156{
8d8b141d
SM
1157 bt_self_component *self_component =
1158 bt_self_component_filter_as_self_component(self_component_filter);
4212232c 1159
8d8b141d
SM
1160 return component_class_message_iterator_init(self_message_iterator,
1161 config, self_component, self_component_port_output);
4212232c
PP
1162}
1163
1164static
1165void component_class_message_iterator_finalize(
1166 bt_self_message_iterator *message_iterator)
1167{
1168 PyObject *py_message_iter = bt_self_message_iterator_get_data(
1169 message_iterator);
1170 PyObject *py_method_result = NULL;
1171
1172 BT_ASSERT(py_message_iter);
1173
1174 /* Call user's _user_finalize() method */
1175 py_method_result = PyObject_CallMethod(py_message_iter,
1176 "_user_finalize", NULL);
6dbc1938 1177 if (!py_method_result) {
4212232c
PP
1178 bt_self_component *self_comp =
1179 bt_self_message_iterator_borrow_component(
1180 message_iterator);
1181 bt_logging_level log_level = get_self_component_log_level(
1182 self_comp);
1183
1184 BT_COMP_LOG_CUR_LVL(BT_LOG_WARNING, log_level, self_comp,
1185 "User's _user_finalize() method raised an exception: ignoring:");
1186 logw_exception(get_self_message_iterator_log_level(
1187 message_iterator));
6dbc1938
SM
1188
1189 /*
1190 * Ignore any exception raised by the _user_finalize() method
1191 * because it won't change anything at this point: the component
1192 * is being destroyed anyway.
1193 */
1194 PyErr_Clear();
4212232c
PP
1195 }
1196
4212232c
PP
1197 Py_XDECREF(py_method_result);
1198 Py_DECREF(py_message_iter);
1199}
1200
1201/* Valid for both sources and filters. */
1202
1203static
1204bt_component_class_message_iterator_next_method_status
1205component_class_message_iterator_next(
1206 bt_self_message_iterator *message_iterator,
1207 bt_message_array_const msgs, uint64_t capacity,
1208 uint64_t *count)
1209{
6dbc1938 1210 bt_component_class_message_iterator_next_method_status status;
4212232c
PP
1211 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1212 PyObject *py_method_result = NULL;
1213
1214 BT_ASSERT(py_message_iter);
6dbc1938 1215
4212232c
PP
1216 py_method_result = PyObject_CallMethod(py_message_iter,
1217 "_bt_next_from_native", NULL);
1218 if (!py_method_result) {
84e438af 1219 status = py_exc_to_status_message_iterator_clear(message_iterator);
4212232c
PP
1220 goto end;
1221 }
1222
1223 /*
1224 * The returned object, on success, is an integer object
1225 * (PyLong) containing the address of a native message
1226 * object (which is now ours).
1227 */
1228 msgs[0] = PyLong_AsVoidPtr(py_method_result);
1229 *count = 1;
1230
6dbc1938 1231 /* Overflow errors should never happen. */
4212232c 1232 BT_ASSERT(!PyErr_Occurred());
6dbc1938
SM
1233
1234 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
4212232c
PP
1235
1236end:
1237 Py_XDECREF(py_method_result);
1238 return status;
1239}
1240
1241static
1242bt_component_class_sink_consume_method_status
1243component_class_sink_consume(bt_self_component_sink *self_component_sink)
1244{
1245 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1246 PyObject *py_comp = bt_self_component_get_data(self_component);
1247 PyObject *py_method_result = NULL;
1248 bt_component_class_sink_consume_method_status status;
1249
1250 BT_ASSERT(py_comp);
6dbc1938 1251
4212232c
PP
1252 py_method_result = PyObject_CallMethod(py_comp,
1253 "_user_consume", NULL);
6dbc1938
SM
1254 if (!py_method_result) {
1255 status = py_exc_to_status_component_clear(self_component);
1256 goto end;
1257 }
1258
1259 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
1260
1261end:
4212232c
PP
1262 Py_XDECREF(py_method_result);
1263 return status;
1264}
1265
1266static
1267int component_class_set_help_and_desc(
1268 bt_component_class *component_class,
1269 const char *description, const char *help)
1270{
1271 int ret;
1272
1273 if (description) {
1274 ret = bt_component_class_set_description(component_class, description);
1275 if (ret) {
1276 BT_LOGE("Cannot set component class's description: "
1277 "comp-cls-addr=%p", component_class);
1278 goto end;
1279 }
1280 }
1281
1282 if (help) {
1283 ret = bt_component_class_set_help(component_class, help);
1284 if (ret) {
1285 BT_LOGE("Cannot set component class's help text: "
1286 "comp-cls-addr=%p", component_class);
1287 goto end;
1288 }
1289 }
1290
1291 ret = 0;
1292
1293end:
1294 return ret;
1295}
1296
1297static
1298bt_component_class_source *bt_bt2_component_class_source_create(
1299 PyObject *py_cls, const char *name, const char *description,
1300 const char *help)
1301{
1302 bt_component_class_source *component_class_source;
1303 bt_component_class *component_class;
1304 int ret;
1305
1306 BT_ASSERT(py_cls);
1307 component_class_source = bt_component_class_source_create(name,
1308 component_class_message_iterator_next);
1309 if (!component_class_source) {
1310 BT_LOGE_STR("Cannot create source component class.");
1311 goto end;
1312 }
1313
1314 component_class = bt_component_class_source_as_component_class(component_class_source);
1315
1316 if (component_class_set_help_and_desc(component_class, description, help)) {
1317 goto end;
1318 }
1319
21a9f056 1320 ret = bt_component_class_source_set_initialize_method(component_class_source, component_class_source_init);
4212232c
PP
1321 BT_ASSERT(ret == 0);
1322 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
1323 BT_ASSERT(ret == 0);
2e1b5615
SM
1324 ret = bt_component_class_source_set_message_iterator_seek_beginning_methods(component_class_source,
1325 component_class_seek_beginning, component_class_can_seek_beginning);
1326 ret = bt_component_class_source_set_message_iterator_seek_ns_from_origin_methods(
1327 component_class_source, component_class_seek_ns_from_origin,
1328 component_class_can_seek_ns_from_origin);
4212232c
PP
1329 BT_ASSERT(ret == 0);
1330 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
1331 component_class_source_output_port_connected);
1332 BT_ASSERT(ret == 0);
1333 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
1334 BT_ASSERT(ret == 0);
fd57054b
PP
1335 ret = bt_component_class_source_set_get_supported_mip_versions_method(component_class_source, component_class_source_get_supported_mip_versions);
1336 BT_ASSERT(ret == 0);
21a9f056 1337 ret = bt_component_class_source_set_message_iterator_initialize_method(
4212232c
PP
1338 component_class_source, component_class_source_message_iterator_init);
1339 BT_ASSERT(ret == 0);
1340 ret = bt_component_class_source_set_message_iterator_finalize_method(
1341 component_class_source, component_class_message_iterator_finalize);
1342 BT_ASSERT(ret == 0);
1343 register_cc_ptr_to_py_cls(component_class, py_cls);
1344
1345end:
1346 return component_class_source;
1347}
1348
1349static
1350bt_component_class_filter *bt_bt2_component_class_filter_create(
1351 PyObject *py_cls, const char *name, const char *description,
1352 const char *help)
1353{
1354 bt_component_class *component_class;
1355 bt_component_class_filter *component_class_filter;
1356 int ret;
1357
1358 BT_ASSERT(py_cls);
1359 component_class_filter = bt_component_class_filter_create(name,
1360 component_class_message_iterator_next);
1361 if (!component_class_filter) {
1362 BT_LOGE_STR("Cannot create filter component class.");
1363 goto end;
1364 }
1365
1366 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1367
1368 if (component_class_set_help_and_desc(component_class, description, help)) {
1369 goto end;
1370 }
1371
21a9f056 1372 ret = bt_component_class_filter_set_initialize_method(component_class_filter, component_class_filter_init);
4212232c
PP
1373 BT_ASSERT(ret == 0);
1374 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
1375 BT_ASSERT(ret == 0);
2e1b5615
SM
1376 ret = bt_component_class_filter_set_message_iterator_seek_beginning_methods(component_class_filter,
1377 component_class_seek_beginning, component_class_can_seek_beginning);
c182d7dd 1378 BT_ASSERT(ret == 0);
2e1b5615
SM
1379 ret = bt_component_class_filter_set_message_iterator_seek_ns_from_origin_methods(
1380 component_class_filter, component_class_seek_ns_from_origin,
1381 component_class_can_seek_ns_from_origin);
4212232c
PP
1382 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
1383 component_class_filter_input_port_connected);
1384 BT_ASSERT(ret == 0);
1385 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
1386 component_class_filter_output_port_connected);
1387 BT_ASSERT(ret == 0);
1388 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
1389 BT_ASSERT(ret == 0);
fd57054b
PP
1390 ret = bt_component_class_filter_set_get_supported_mip_versions_method(component_class_filter, component_class_filter_get_supported_mip_versions);
1391 BT_ASSERT(ret == 0);
21a9f056 1392 ret = bt_component_class_filter_set_message_iterator_initialize_method(
4212232c
PP
1393 component_class_filter, component_class_filter_message_iterator_init);
1394 BT_ASSERT(ret == 0);
1395 ret = bt_component_class_filter_set_message_iterator_finalize_method(
1396 component_class_filter, component_class_message_iterator_finalize);
1397 BT_ASSERT(ret == 0);
1398 register_cc_ptr_to_py_cls(component_class, py_cls);
1399
1400end:
1401 return component_class_filter;
1402}
1403
1404static
1405bt_component_class_sink *bt_bt2_component_class_sink_create(
1406 PyObject *py_cls, const char *name, const char *description,
1407 const char *help)
1408{
1409 bt_component_class_sink *component_class_sink;
1410 bt_component_class *component_class;
1411 int ret;
1412
1413 BT_ASSERT(py_cls);
1414 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
1415
1416 if (!component_class_sink) {
1417 BT_LOGE_STR("Cannot create sink component class.");
1418 goto end;
1419 }
1420
1421 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1422
1423 if (component_class_set_help_and_desc(component_class, description, help)) {
1424 goto end;
1425 }
1426
21a9f056 1427 ret = bt_component_class_sink_set_initialize_method(component_class_sink, component_class_sink_init);
4212232c
PP
1428 BT_ASSERT(ret == 0);
1429 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
1430 BT_ASSERT(ret == 0);
1431 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
1432 component_class_sink_input_port_connected);
1433 BT_ASSERT(ret == 0);
1434 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
1435 component_class_sink_graph_is_configured);
1436 BT_ASSERT(ret == 0);
1437 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
1438 BT_ASSERT(ret == 0);
fd57054b
PP
1439 ret = bt_component_class_sink_set_get_supported_mip_versions_method(component_class_sink, component_class_sink_get_supported_mip_versions);
1440 BT_ASSERT(ret == 0);
4212232c
PP
1441 register_cc_ptr_to_py_cls(component_class, py_cls);
1442
1443end:
1444 return component_class_sink;
1445}
This page took 0.09182 seconds and 4 git commands to generate.