bt2: rename CreationError to MemoryError, handle it in and out of Python bindings
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_component_class.i
CommitLineData
6945df9a
SM
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
d24d5663 25%include <babeltrace2/graph/component-class.h>
d6bb425c
SM
26%include <babeltrace2/graph/component-class-const.h>
27%include <babeltrace2/graph/component-class-source-const.h>
28%include <babeltrace2/graph/component-class-source.h>
29%include <babeltrace2/graph/component-class-filter-const.h>
30%include <babeltrace2/graph/component-class-filter.h>
31%include <babeltrace2/graph/component-class-sink-const.h>
32%include <babeltrace2/graph/component-class-sink.h>
33%include <babeltrace2/graph/self-component-class-source.h>
34%include <babeltrace2/graph/self-component-class-filter.h>
35%include <babeltrace2/graph/self-component-class-sink.h>
6945df9a
SM
36
37%{
38/*
39 * This hash table associates a BT component class object address to a
40 * user-defined Python class (PyObject *). The keys and values are NOT
41 * owned by this hash table. The Python class objects are owned by the
42 * Python module, which should not be unloaded until it is not possible
43 * to create a user Python component anyway.
44 *
45 * This hash table is written to when a user-defined Python component
d24d5663 46 * class is created by one of the bt_bt2_component_class_*_create()
6945df9a
SM
47 * functions.
48 *
49 * This function is read from when a user calls bt_component_create()
50 * with a component class pointer created by one of the functions above.
51 * In this case, the original Python class needs to be found to
52 * instantiate it and associate the created Python component object with
53 * a BT component object instance.
54 */
55
56static GHashTable *bt_cc_ptr_to_py_cls;
57
7744859c
SM
58static
59void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
6945df9a
SM
60 PyObject *py_cls)
61{
62 if (!bt_cc_ptr_to_py_cls) {
63 /*
64 * Lazy-initializing this GHashTable because GLib
65 * might not be initialized yet and it needs to be
66 * before we call g_hash_table_new()
67 */
68 BT_LOGD_STR("Creating native component class to Python component class hash table.");
69 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
70 BT_ASSERT(bt_cc_ptr_to_py_cls);
71 }
72
73 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
74 (gpointer) py_cls);
75}
76
7744859c
SM
77static
78PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc)
6945df9a
SM
79{
80 if (!bt_cc_ptr_to_py_cls) {
81 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
82 "comp-cls-addr=%p", bt_cc);
83 return NULL;
84 }
85
86 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
87 (gconstpointer) bt_cc);
88}
89
90
91/*
92 * Useful Python objects.
93 */
94
95static PyObject *py_mod_bt2 = NULL;
96static PyObject *py_mod_bt2_exc_error_type = NULL;
4acc866e 97static PyObject *py_mod_bt2_exc_memory_error = NULL;
6945df9a
SM
98static PyObject *py_mod_bt2_exc_try_again_type = NULL;
99static PyObject *py_mod_bt2_exc_stop_type = NULL;
5602ef81 100static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL;
d24d5663
PP
101static PyObject *py_mod_bt2_exc_invalid_object_type = NULL;
102static PyObject *py_mod_bt2_exc_invalid_params_type = NULL;
103static PyObject *py_mod_bt2_exc_unsupported_type = NULL;
6945df9a 104
7744859c 105static
d24d5663 106void bt_bt2_cc_init_from_bt2(void)
6945df9a
SM
107{
108 /*
109 * This is called once the bt2 package is loaded.
110 *
111 * Those modules and functions are needed while the package is
112 * used. Loading them here is safe because we know the bt2
113 * package is imported, and we know that the user cannot use the
114 * code here without importing bt2 first.
115 */
116 py_mod_bt2 = PyImport_ImportModule("bt2");
117 BT_ASSERT(py_mod_bt2);
118 py_mod_bt2_exc_error_type =
119 PyObject_GetAttrString(py_mod_bt2, "Error");
120 BT_ASSERT(py_mod_bt2_exc_error_type);
4acc866e
SM
121 py_mod_bt2_exc_memory_error =
122 PyObject_GetAttrString(py_mod_bt2, "MemoryError");
123 BT_ASSERT(py_mod_bt2_exc_memory_error);
6945df9a
SM
124 py_mod_bt2_exc_try_again_type =
125 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
126 BT_ASSERT(py_mod_bt2_exc_try_again_type);
127 py_mod_bt2_exc_stop_type =
128 PyObject_GetAttrString(py_mod_bt2, "Stop");
129 BT_ASSERT(py_mod_bt2_exc_stop_type);
d24d5663
PP
130 py_mod_bt2_exc_invalid_object_type =
131 PyObject_GetAttrString(py_mod_bt2, "InvalidObject");
132 BT_ASSERT(py_mod_bt2_exc_invalid_object_type);
133 py_mod_bt2_exc_invalid_params_type =
134 PyObject_GetAttrString(py_mod_bt2, "InvalidParams");
135 BT_ASSERT(py_mod_bt2_exc_invalid_params_type);
136 py_mod_bt2_exc_unsupported_type =
137 PyObject_GetAttrString(py_mod_bt2, "Unsupported");
138 BT_ASSERT(py_mod_bt2_exc_unsupported_type);
6945df9a
SM
139}
140
7744859c 141static
d24d5663 142void bt_bt2_cc_exit_handler(void)
6945df9a
SM
143{
144 /*
145 * This is an exit handler (set by the bt2 package).
146 *
147 * We only give back the references that we took in
d24d5663 148 * bt_bt2_cc_init_from_bt2() here. The global variables continue
6945df9a
SM
149 * to exist for the code of this file, but they are now borrowed
150 * references. If this code is executed, it means that somehow
151 * the modules are still loaded, so it should be safe to use
152 * them even without a strong reference.
153 *
154 * We cannot do this in the library's destructor because it
155 * gets executed once Python is already finalized.
156 */
157 Py_XDECREF(py_mod_bt2);
158 Py_XDECREF(py_mod_bt2_exc_error_type);
159 Py_XDECREF(py_mod_bt2_exc_try_again_type);
160 Py_XDECREF(py_mod_bt2_exc_stop_type);
5602ef81 161 Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type);
d24d5663
PP
162 Py_XDECREF(py_mod_bt2_exc_invalid_object_type);
163 Py_XDECREF(py_mod_bt2_exc_invalid_params_type);
6945df9a
SM
164}
165
166
167/* Library destructor */
168
169__attribute__((destructor))
7744859c 170static
d24d5663 171void native_comp_class_dtor(void) {
6945df9a
SM
172 /* Destroy component class association hash table */
173 if (bt_cc_ptr_to_py_cls) {
174 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
175 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
176 }
177}
178
ce4923b0
SM
179static
180void restore_current_thread_error_and_append_exception_chain_recursive(
181 PyObject *py_exc_value,
182 bt_self_component_class *self_component_class,
183 bt_self_component *self_component,
184 bt_self_message_iterator *self_message_iterator,
185 const char *module_name)
186{
187 PyObject *py_exc_cause_value;
188 PyObject *py_exc_type = NULL;
189 PyObject *py_exc_tb = NULL;
190 GString *gstr = NULL;
191
192 /* If this exception has a cause, handle that one first. */
193 py_exc_cause_value = PyException_GetCause(py_exc_value);
194 if (py_exc_cause_value) {
195 restore_current_thread_error_and_append_exception_chain_recursive(
196 py_exc_cause_value, self_component_class,
197 self_component, self_message_iterator, module_name);
198 }
199
200 /*
201 * If the raised exception is a bt2.Error, restore the wrapped error.
202 */
203 if (PyErr_GivenExceptionMatches(py_exc_value, py_mod_bt2_exc_error_type)) {
204 PyObject *py_error_swig_ptr;
205 const bt_error *error;
206 int ret;
207
208 /*
209 * We never raise a bt2.Error with a cause: it should be the
210 * end of the chain.
211 */
212 BT_ASSERT(!py_exc_cause_value);
213
214 /*
215 * We steal the error object from the exception, to move
216 * it back as the current thread's error.
217 */
218 py_error_swig_ptr = PyObject_GetAttrString(py_exc_value, "_ptr");
219 BT_ASSERT(py_error_swig_ptr);
220
221 ret = PyObject_SetAttrString(py_exc_value, "_ptr", Py_None);
222 BT_ASSERT(ret == 0);
223
224 ret = SWIG_ConvertPtr(py_error_swig_ptr, (void **) &error,
225 SWIGTYPE_p_bt_error, 0);
226 BT_ASSERT(ret == 0);
227
228 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(error);
229
230 Py_DECREF(py_error_swig_ptr);
231 }
232
233 py_exc_type = PyObject_Type(py_exc_value);
234 py_exc_tb = PyException_GetTraceback(py_exc_value);
235
236 gstr = bt_py_common_format_exception(py_exc_type, py_exc_value,
237 py_exc_tb, BT_LOG_OUTPUT_LEVEL, false);
238 if (!gstr) {
239 /* bt_py_common_format_exception has already warned. */
240 goto end;
241 }
242
243 if (self_component_class) {
244 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
245 self_component_class, "%s", gstr->str);
246 } else if (self_component) {
247 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
248 self_component, "%s", gstr->str);
249 } else if (self_message_iterator) {
250 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
251 self_message_iterator, "%s", gstr->str);
252 } else {
253 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
254 module_name, "%s", gstr->str);
255 }
256
257end:
258 if (gstr) {
259 g_string_free(gstr, TRUE);
260 }
261
262 Py_XDECREF(py_exc_cause_value);
263 Py_XDECREF(py_exc_type);
264 Py_XDECREF(py_exc_tb);
265}
266
267/*
268 * If you have the following code:
269 *
270 * try:
271 * try:
272 * something_that_raises_bt2_error()
273 * except bt2.Error as e1:
274 * raise ValueError from e1
275 * except ValueError as e2:
276 * raise TypeError from e2
277 *
278 * We will have the following exception chain:
279 *
280 * TypeError -> ValueError -> bt2.Error
281 *
282 * Where the TypeError is the current exception (obtained from PyErr_Fetch).
283 *
284 * The bt2.Error contains a `struct bt_error *` that used to be the current
285 * thread's error, at the moment the exception was raised.
286 *
287 * This function gets to the bt2.Error and restores the wrapped
288 * `struct bt_error *` as the current thread's error.
289 *
290 * Then, for each exception in the chain, starting with the oldest one, it adds
291 * an error cause to the current thread's error.
292 */
293static
294void restore_bt_error_and_append_current_exception_chain(
295 bt_self_component_class *self_component_class,
296 bt_self_component *self_component,
297 bt_self_message_iterator *self_message_iterator,
298 const char *module_name)
299{
300 BT_ASSERT(PyErr_Occurred());
301
302 /* Used to access and restore the current exception. */
303 PyObject *py_exc_type;
304 PyObject *py_exc_value;
305 PyObject *py_exc_tb;
306
307 /* Fetch and normalize the Python exception. */
308 PyErr_Fetch(&py_exc_type, &py_exc_value, &py_exc_tb);
309 PyErr_NormalizeException(&py_exc_type, &py_exc_value, &py_exc_tb);
310 BT_ASSERT(py_exc_type);
311 BT_ASSERT(py_exc_value);
312 BT_ASSERT(py_exc_tb);
313
314 /*
315 * Set the exception's traceback so it's possible to get it using
316 * PyException_GetTraceback in
317 * restore_current_thread_error_and_append_exception_chain_recursive.
318 */
319 PyException_SetTraceback(py_exc_value, py_exc_tb);
320
321 restore_current_thread_error_and_append_exception_chain_recursive(py_exc_value,
322 self_component_class, self_component, self_message_iterator,
323 module_name);
324
325 PyErr_Restore(py_exc_type, py_exc_value, py_exc_tb);
326}
327
d24d5663 328static inline
ce95fb26
SM
329void log_exception_and_maybe_append_error(int log_level,
330 bool append_error,
331 bt_self_component_class *self_component_class,
332 bt_self_component *self_component,
ce4923b0
SM
333 bt_self_message_iterator *self_message_iterator,
334 const char *module_name)
6945df9a 335{
7085eeaa 336 GString *gstr;
6945df9a 337
5084732e 338 BT_ASSERT(PyErr_Occurred());
cacd0713 339 gstr = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL);
7085eeaa 340 if (!gstr) {
cacd0713 341 /* bt_py_common_format_current_exception() logs errors */
6945df9a
SM
342 goto end;
343 }
344
d24d5663 345 BT_LOG_WRITE(log_level, BT_LOG_TAG, "%s", gstr->str);
6945df9a 346
ce95fb26 347 if (append_error) {
ce4923b0
SM
348 restore_bt_error_and_append_current_exception_chain(
349 self_component_class, self_component,
350 self_message_iterator, module_name);
351
ce95fb26
SM
352 }
353
6945df9a 354end:
7085eeaa
PP
355 if (gstr) {
356 g_string_free(gstr, TRUE);
6945df9a 357 }
6945df9a
SM
358}
359
d24d5663 360static inline
ce4923b0 361void loge_exception(const char *module_name)
6945df9a 362{
ce4923b0
SM
363 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL,
364 NULL, module_name);
ce95fb26
SM
365}
366
367static
368void loge_exception_message_iterator(
369 bt_self_message_iterator *self_message_iterator)
370{
ce4923b0
SM
371 log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL,
372 self_message_iterator, NULL);
6945df9a
SM
373}
374
d24d5663
PP
375static inline
376void logw_exception(void)
6945df9a 377{
ce4923b0
SM
378 log_exception_and_maybe_append_error(BT_LOG_WARNING, false, NULL, NULL,
379 NULL, NULL);
6945df9a
SM
380}
381
d24d5663 382static inline
ce95fb26
SM
383int py_exc_to_status(bt_self_component_class *self_component_class,
384 bt_self_component *self_component,
ce4923b0
SM
385 bt_self_message_iterator *self_message_iterator,
386 const char *module_name)
6945df9a 387{
d24d5663 388 int status = __BT_FUNC_STATUS_OK;
6945df9a
SM
389 PyObject *exc = PyErr_Occurred();
390
391 if (!exc) {
392 goto end;
393 }
394
395 if (PyErr_GivenExceptionMatches(exc,
d24d5663
PP
396 py_mod_bt2_exc_try_again_type)) {
397 status = __BT_FUNC_STATUS_AGAIN;
6945df9a 398 } else if (PyErr_GivenExceptionMatches(exc,
d24d5663
PP
399 py_mod_bt2_exc_stop_type)) {
400 status = __BT_FUNC_STATUS_END;
6945df9a 401 } else if (PyErr_GivenExceptionMatches(exc,
d24d5663
PP
402 py_mod_bt2_exc_invalid_object_type)) {
403 status = __BT_FUNC_STATUS_INVALID_OBJECT;
404 } else if (PyErr_GivenExceptionMatches(exc,
405 py_mod_bt2_exc_invalid_params_type)) {
406 status = __BT_FUNC_STATUS_INVALID_PARAMS;
407 } else if (PyErr_GivenExceptionMatches(exc,
408 py_mod_bt2_exc_unsupported_type)) {
409 status = __BT_FUNC_STATUS_UNSUPPORTED;
6945df9a 410 } else {
d24d5663 411 /* Unknown exception: convert to general error */
ce95fb26
SM
412 log_exception_and_maybe_append_error(BT_LOG_WARNING, true,
413 self_component_class, self_component,
ce4923b0 414 self_message_iterator, module_name);
4acc866e
SM
415
416 if (PyErr_GivenExceptionMatches(exc,
417 py_mod_bt2_exc_memory_error)) {
418 status = __BT_FUNC_STATUS_MEMORY_ERROR;
419 } else {
420 status = __BT_FUNC_STATUS_ERROR;
421 }
6945df9a
SM
422 }
423
424end:
425 PyErr_Clear();
426 return status;
427}
428
ce95fb26
SM
429static
430int py_exc_to_status_component_class(bt_self_component_class *self_component_class)
431{
ce4923b0 432 return py_exc_to_status(self_component_class, NULL, NULL, NULL);
ce95fb26
SM
433}
434
435static
436int py_exc_to_status_component(bt_self_component *self_component)
437{
ce4923b0 438 return py_exc_to_status(NULL, self_component, NULL, NULL);
ce95fb26
SM
439}
440
441static
442int py_exc_to_status_message_iterator(
443 bt_self_message_iterator *self_message_iterator)
444{
ce4923b0 445 return py_exc_to_status(NULL, NULL, self_message_iterator, NULL);
ce95fb26
SM
446}
447
d24d5663
PP
448/* Component class proxy methods (delegate to the attached Python object) */
449
7744859c 450static
d24d5663 451bt_component_class_init_method_status component_class_init(
6945df9a
SM
452 bt_self_component *self_component,
453 void *self_component_v,
454 swig_type_info *self_comp_cls_type_swig_type,
455 const bt_value *params,
456 void *init_method_data)
457{
458 const bt_component *component = bt_self_component_as_component(self_component);
459 const bt_component_class *component_class = bt_component_borrow_class_const(component);
d24d5663 460 bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
461 PyObject *py_cls = NULL;
462 PyObject *py_comp = NULL;
463 PyObject *py_params_ptr = NULL;
464 PyObject *py_comp_ptr = NULL;
465
466 (void) init_method_data;
467
468 BT_ASSERT(self_component);
469 BT_ASSERT(self_component_v);
470 BT_ASSERT(self_comp_cls_type_swig_type);
471
472 /*
473 * Get the user-defined Python class which created this
474 * component's class in the first place (borrowed
475 * reference).
476 */
477 py_cls = lookup_cc_ptr_to_py_cls(component_class);
478 if (!py_cls) {
479 BT_LOGE("Cannot find Python class associated to native component class: "
480 "comp-cls-addr=%p", component_class);
481 goto error;
482 }
483
484 /* Parameters pointer -> SWIG pointer Python object */
485 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
486 SWIGTYPE_p_bt_value, 0);
487 if (!py_params_ptr) {
488 BT_LOGE_STR("Failed to create a SWIG pointer object.");
489 goto error;
490 }
491
492 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v),
493 self_comp_cls_type_swig_type, 0);
494 if (!py_comp_ptr) {
495 BT_LOGE_STR("Failed to create a SWIG pointer object.");
496 goto error;
497 }
498
499 /*
500 * Do the equivalent of this:
501 *
85906b6b 502 * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr)
6945df9a 503 *
85906b6b 504 * _UserComponentType._bt_init_from_native() calls the Python
6945df9a
SM
505 * component object's __init__() function.
506 */
507 py_comp = PyObject_CallMethod(py_cls,
85906b6b 508 "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
6945df9a 509 if (!py_comp) {
85906b6b 510 BT_LOGW("Failed to call Python class's _bt_init_from_native() method: "
6945df9a 511 "py-cls-addr=%p", py_cls);
ce95fb26
SM
512 status = py_exc_to_status_component(self_component);
513 goto end;
6945df9a
SM
514 }
515
516 /*
517 * Our user Python component object is now fully created and
518 * initialized by the user. Since we just created it, this
519 * native component is its only (persistent) owner.
520 */
521 bt_self_component_set_data(self_component, py_comp);
522 py_comp = NULL;
523 goto end;
524
525error:
d24d5663 526 status = __BT_FUNC_STATUS_ERROR;
6945df9a
SM
527
528 /*
529 * Clear any exception: we're returning a bad status anyway. If
530 * this call originated from Python (creation from a plugin's
531 * component class, for example), then the user gets an
532 * appropriate creation error.
533 */
534 PyErr_Clear();
535
536end:
537 Py_XDECREF(py_comp);
538 Py_XDECREF(py_params_ptr);
539 Py_XDECREF(py_comp_ptr);
540 return status;
541}
542
543/*
544 * Method of bt_component_class_source to initialize a bt_self_component_source
545 * of that class.
546 */
547
7744859c 548static
d24d5663 549bt_component_class_init_method_status component_class_source_init(
7744859c 550 bt_self_component_source *self_component_source,
6945df9a
SM
551 const bt_value *params, void *init_method_data)
552{
553 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
d24d5663 554 return component_class_init(
6945df9a
SM
555 self_component,
556 self_component_source,
557 SWIGTYPE_p_bt_self_component_source,
558 params, init_method_data);
559}
560
7744859c 561static
d24d5663 562bt_component_class_init_method_status component_class_filter_init(
7744859c 563 bt_self_component_filter *self_component_filter,
6945df9a
SM
564 const bt_value *params, void *init_method_data)
565{
566 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
d24d5663 567 return component_class_init(
6945df9a
SM
568 self_component,
569 self_component_filter,
570 SWIGTYPE_p_bt_self_component_filter,
571 params, init_method_data);
572}
573
7744859c 574static
d24d5663 575bt_component_class_init_method_status component_class_sink_init(
7744859c 576 bt_self_component_sink *self_component_sink,
6945df9a
SM
577 const bt_value *params, void *init_method_data)
578{
579 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
d24d5663 580 return component_class_init(
6945df9a
SM
581 self_component,
582 self_component_sink,
583 SWIGTYPE_p_bt_self_component_sink,
584 params, init_method_data);
585}
586
7744859c 587static
d24d5663 588void component_class_finalize(bt_self_component *self_component)
6945df9a
SM
589{
590 PyObject *py_comp = bt_self_component_get_data(self_component);
591 BT_ASSERT(py_comp);
592
593 /* Call user's _finalize() method */
594 PyObject *py_method_result = PyObject_CallMethod(py_comp,
595 "_finalize", NULL);
596
597 if (PyErr_Occurred()) {
d24d5663
PP
598 BT_LOGW("User component's _finalize() method raised an exception: ignoring:");
599 logw_exception();
6945df9a
SM
600 }
601
602 /*
603 * Ignore any exception raised by the _finalize() method because
604 * it won't change anything at this point: the component is
605 * being destroyed anyway.
606 */
607 PyErr_Clear();
608 Py_XDECREF(py_method_result);
609 Py_DECREF(py_comp);
610}
611
7744859c 612static
d24d5663 613void component_class_source_finalize(bt_self_component_source *self_component_source)
6945df9a
SM
614{
615 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
d24d5663 616 component_class_finalize(self_component);
6945df9a
SM
617}
618
7744859c 619static
d24d5663 620void component_class_filter_finalize(bt_self_component_filter *self_component_filter)
6945df9a
SM
621{
622 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
d24d5663 623 component_class_finalize(self_component);
6945df9a
SM
624}
625
7744859c 626static
d24d5663 627void component_class_sink_finalize(bt_self_component_sink *self_component_sink)
6945df9a
SM
628{
629 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
d24d5663 630 component_class_finalize(self_component);
6945df9a
SM
631}
632
f00b8d40 633static
d24d5663 634bt_bool component_class_can_seek_beginning(
f00b8d40
SM
635 bt_self_message_iterator *self_message_iterator)
636{
637 PyObject *py_iter;
638 PyObject *py_result = NULL;
639 bt_bool can_seek_beginning = false;
640
641 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
642 BT_ASSERT(py_iter);
643
85906b6b 644 py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native");
f00b8d40
SM
645
646 BT_ASSERT(!py_result || PyBool_Check(py_result));
647
648 if (py_result) {
649 can_seek_beginning = PyObject_IsTrue(py_result);
650 } else {
651 /*
652 * Once can_seek_beginning can report errors, convert the
653 * exception to a status. For now, log and return false;
654 */
ce95fb26 655 loge_exception_message_iterator(self_message_iterator);
f00b8d40
SM
656 PyErr_Clear();
657 }
658
659 Py_XDECREF(py_result);
660
661 return can_seek_beginning;
662}
663
664static
d24d5663
PP
665bt_component_class_message_iterator_seek_beginning_method_status
666component_class_seek_beginning(bt_self_message_iterator *self_message_iterator)
f00b8d40
SM
667{
668 PyObject *py_iter;
669 PyObject *py_result;
d24d5663 670 bt_component_class_message_iterator_seek_beginning_method_status status;
f00b8d40
SM
671
672 py_iter = bt_self_message_iterator_get_data(self_message_iterator);
673 BT_ASSERT(py_iter);
85906b6b 674 py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native",
d24d5663 675 NULL);
f00b8d40 676 BT_ASSERT(!py_result || py_result == Py_None);
ce4923b0 677 status = py_exc_to_status_message_iterator(self_message_iterator);
f00b8d40 678 Py_XDECREF(py_result);
f00b8d40
SM
679 return status;
680}
681
7744859c 682static
d24d5663 683bt_component_class_port_connected_method_status component_class_port_connected(
6945df9a
SM
684 bt_self_component *self_component,
685 void *self_component_port,
686 swig_type_info *self_component_port_swig_type,
687 bt_port_type self_component_port_type,
688 const void *other_port,
689 swig_type_info *other_port_swig_type)
690{
d24d5663 691 bt_component_class_port_connected_method_status status;
6945df9a
SM
692 PyObject *py_comp = NULL;
693 PyObject *py_self_port_ptr = NULL;
694 PyObject *py_other_port_ptr = NULL;
695 PyObject *py_method_result = NULL;
696
697 py_comp = bt_self_component_get_data(self_component);
698 BT_ASSERT(py_comp);
6945df9a
SM
699 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port),
700 self_component_port_swig_type, 0);
701 if (!py_self_port_ptr) {
702 BT_LOGF_STR("Failed to create a SWIG pointer object.");
d24d5663 703 status = __BT_FUNC_STATUS_MEMORY_ERROR;
6945df9a
SM
704 goto end;
705 }
706
707 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
708 other_port_swig_type, 0);
709 if (!py_other_port_ptr) {
710 BT_LOGF_STR("Failed to create a SWIG pointer object.");
d24d5663 711 status = __BT_FUNC_STATUS_MEMORY_ERROR;
6945df9a
SM
712 goto end; }
713
714 py_method_result = PyObject_CallMethod(py_comp,
85906b6b 715 "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr,
6945df9a 716 self_component_port_type, py_other_port_ptr);
6945df9a 717 BT_ASSERT(!py_method_result || py_method_result == Py_None);
ce95fb26 718 status = py_exc_to_status_component(self_component);
6945df9a
SM
719
720end:
721 Py_XDECREF(py_self_port_ptr);
722 Py_XDECREF(py_other_port_ptr);
723 Py_XDECREF(py_method_result);
6945df9a
SM
724 return status;
725}
726
7744859c 727static
d24d5663
PP
728bt_component_class_port_connected_method_status
729component_class_source_output_port_connected(
6945df9a
SM
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
d24d5663 736 return component_class_port_connected(
6945df9a
SM
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
7744859c 745static
d24d5663
PP
746bt_component_class_port_connected_method_status
747component_class_filter_input_port_connected(
6945df9a
SM
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
d24d5663 754 return component_class_port_connected(
6945df9a
SM
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
7744859c 763static
d24d5663
PP
764bt_component_class_port_connected_method_status
765component_class_filter_output_port_connected(
6945df9a
SM
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
d24d5663 772 return component_class_port_connected(
6945df9a
SM
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
7744859c 781static
d24d5663
PP
782bt_component_class_port_connected_method_status
783component_class_sink_input_port_connected(
6945df9a
SM
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
d24d5663 790 return component_class_port_connected(
6945df9a
SM
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
7744859c 799static
d24d5663
PP
800bt_component_class_sink_graph_is_configured_method_status
801component_class_sink_graph_is_configured(
7744859c 802 bt_self_component_sink *self_component_sink)
6945df9a
SM
803{
804 PyObject *py_comp = NULL;
805 PyObject *py_method_result = NULL;
d24d5663 806 bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
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);
810 py_method_result = PyObject_CallMethod(py_comp,
85906b6b 811 "_bt_graph_is_configured_from_native", NULL);
6945df9a 812 BT_ASSERT(!py_method_result || py_method_result == Py_None);
ce95fb26 813 status = py_exc_to_status_component(self_component);
6945df9a 814 Py_XDECREF(py_method_result);
6945df9a
SM
815 return status;
816}
817
7744859c 818static
d24d5663 819bt_component_class_query_method_status component_class_query(
6945df9a 820 const bt_component_class *component_class,
ce95fb26 821 bt_self_component_class *self_component_class,
6945df9a
SM
822 const bt_query_executor *query_executor,
823 const char *object, const bt_value *params,
f4e38e70 824 bt_logging_level log_level,
6945df9a
SM
825 const bt_value **result)
826{
827 PyObject *py_cls = NULL;
828 PyObject *py_params_ptr = NULL;
829 PyObject *py_query_exec_ptr = NULL;
830 PyObject *py_query_func = NULL;
831 PyObject *py_object = NULL;
832 PyObject *py_results_addr = NULL;
d24d5663 833 bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
834
835 py_cls = lookup_cc_ptr_to_py_cls(component_class);
836 if (!py_cls) {
837 BT_LOGE("Cannot find Python class associated to native component class: "
838 "comp-cls-addr=%p", component_class);
839 goto error;
840 }
841
842 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
843 SWIGTYPE_p_bt_value, 0);
844 if (!py_params_ptr) {
845 BT_LOGE_STR("Failed to create a SWIG pointer object.");
846 goto error;
847 }
848
849 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor),
850 SWIGTYPE_p_bt_query_executor, 0);
851 if (!py_query_exec_ptr) {
852 BT_LOGE_STR("Failed to create a SWIG pointer object.");
853 goto error;
854 }
855
856 py_object = SWIG_FromCharPtr(object);
857 if (!py_object) {
858 BT_LOGE_STR("Failed to create a Python string.");
859 goto error;
860 }
861
862 py_results_addr = PyObject_CallMethod(py_cls,
85906b6b 863 "_bt_query_from_native", "(OOOi)", py_query_exec_ptr,
f4e38e70 864 py_object, py_params_ptr, (int) log_level);
6945df9a 865 if (!py_results_addr) {
85906b6b 866 BT_LOGW("Failed to call Python class's _bt_query_from_native() method: "
6945df9a 867 "py-cls-addr=%p", py_cls);
ce95fb26 868 status = py_exc_to_status_component_class(self_component_class);
6945df9a
SM
869 goto end;
870 }
871
872 /*
873 * The returned object, on success, is an integer object
874 * (PyLong) containing the address of a BT value object (new
875 * reference).
876 */
babe0791 877 *result = PyLong_AsVoidPtr(py_results_addr);
6945df9a
SM
878 BT_ASSERT(!PyErr_Occurred());
879 BT_ASSERT(*result);
880 goto end;
881
882error:
883 PyErr_Clear();
d24d5663 884 status = __BT_FUNC_STATUS_ERROR;
6945df9a
SM
885
886end:
887 Py_XDECREF(py_params_ptr);
888 Py_XDECREF(py_query_exec_ptr);
889 Py_XDECREF(py_query_func);
890 Py_XDECREF(py_object);
891 Py_XDECREF(py_results_addr);
892 return status;
893}
894
7744859c 895static
d24d5663 896bt_component_class_query_method_status component_class_source_query(
6945df9a
SM
897 bt_self_component_class_source *self_component_class_source,
898 const bt_query_executor *query_executor,
899 const char *object, const bt_value *params,
f4e38e70 900 bt_logging_level log_level,
6945df9a
SM
901 const bt_value **result)
902{
903 const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source);
904 const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source);
ce95fb26 905 bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source);
d24d5663 906
ce95fb26 907 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
6945df9a
SM
908}
909
7744859c 910static
d24d5663 911bt_component_class_query_method_status component_class_filter_query(
6945df9a
SM
912 bt_self_component_class_filter *self_component_class_filter,
913 const bt_query_executor *query_executor,
914 const char *object, const bt_value *params,
f4e38e70 915 bt_logging_level log_level,
6945df9a
SM
916 const bt_value **result)
917{
918 const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter);
919 const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter);
ce95fb26 920 bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter);
d24d5663 921
ce95fb26 922 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
6945df9a
SM
923}
924
7744859c 925static
d24d5663 926bt_component_class_query_method_status component_class_sink_query(
6945df9a
SM
927 bt_self_component_class_sink *self_component_class_sink,
928 const bt_query_executor *query_executor,
929 const char *object, const bt_value *params,
f4e38e70 930 bt_logging_level log_level,
6945df9a
SM
931 const bt_value **result)
932{
933 const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink);
934 const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink);
ce95fb26 935 bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink);
d24d5663 936
ce95fb26 937 return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result);
6945df9a
SM
938}
939
7744859c 940static
d24d5663
PP
941bt_component_class_message_iterator_init_method_status
942component_class_message_iterator_init(
6945df9a
SM
943 bt_self_message_iterator *self_message_iterator,
944 bt_self_component *self_component,
945 bt_self_component_port_output *self_component_port_output)
946{
d24d5663 947 bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
948 PyObject *py_comp_cls = NULL;
949 PyObject *py_iter_cls = NULL;
950 PyObject *py_iter_ptr = NULL;
c5f330cd 951 PyObject *py_component_port_output_ptr = NULL;
6945df9a
SM
952 PyObject *py_init_method_result = NULL;
953 PyObject *py_iter = NULL;
954 PyObject *py_comp;
955
956 py_comp = bt_self_component_get_data(self_component);
957
5602ef81 958 /* Find user's Python message iterator class */
6945df9a
SM
959 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
960 if (!py_comp_cls) {
961 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
ce95fb26 962 goto python_error;
6945df9a
SM
963 }
964
965 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
966 if (!py_iter_cls) {
967 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
ce95fb26 968 goto python_error;
6945df9a
SM
969 }
970
971 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator),
972 SWIGTYPE_p_bt_self_message_iterator, 0);
973 if (!py_iter_ptr) {
ce95fb26
SM
974 const char *err = "Failed to create a SWIG pointer object.";
975
976 BT_LOGE_STR(err);
977 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
978 self_message_iterator, err);
6945df9a
SM
979 goto error;
980 }
981
982 /*
5602ef81 983 * Create object with borrowed native message iterator
6945df9a
SM
984 * reference:
985 *
986 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
987 */
988 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
989 "(OO)", py_iter_cls, py_iter_ptr);
990 if (!py_iter) {
991 BT_LOGE("Failed to call Python class's __new__() method: "
992 "py-cls-addr=%p", py_iter_cls);
ce95fb26 993 goto python_error;
6945df9a
SM
994 }
995
996 /*
997 * Initialize object:
998 *
c5f330cd
SM
999 * py_iter.__init__(self_output_port)
1000 *
1001 * through the _init_for_native helper static method.
6945df9a
SM
1002 *
1003 * At this point, py_iter._ptr is set, so this initialization
1004 * function has access to self._component (which gives it the
1005 * user Python component object from which the iterator was
1006 * created).
1007 */
d24d5663
PP
1008 py_component_port_output_ptr = SWIG_NewPointerObj(
1009 SWIG_as_voidptr(self_component_port_output),
c5f330cd
SM
1010 SWIGTYPE_p_bt_self_component_port_output, 0);
1011 if (!py_component_port_output_ptr) {
ce95fb26
SM
1012 const char *err = "Failed to create a SWIG pointer object.";
1013
1014 BT_LOGE_STR(err);
1015 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
1016 self_message_iterator, err);
c5f330cd
SM
1017 goto error;
1018 }
1019
d24d5663 1020 py_init_method_result = PyObject_CallMethod(py_iter,
85906b6b 1021 "_bt_init_from_native", "O", py_component_port_output_ptr);
6945df9a 1022 if (!py_init_method_result) {
ce95fb26
SM
1023 BT_LOGE_STR("User's __init__() method failed:");
1024 goto python_error;
6945df9a
SM
1025 }
1026
1027 /*
1028 * Since the Python code can never instantiate a user-defined
5602ef81
SM
1029 * message iterator class, the native message iterator
1030 * object does NOT belong to a user Python message iterator
6945df9a 1031 * object (borrowed reference). However this Python object is
5602ef81 1032 * owned by this native message iterator object.
6945df9a 1033 *
5602ef81
SM
1034 * In the Python world, the lifetime of the native message
1035 * iterator is managed by a _GenericMessageIterator
6945df9a
SM
1036 * instance:
1037 *
5602ef81
SM
1038 * _GenericMessageIterator instance:
1039 * owns a native bt_message_iterator object (iter)
6945df9a
SM
1040 * owns a _UserMessageIterator instance (py_iter)
1041 * self._ptr is a borrowed reference to the
5602ef81 1042 * native bt_private_connection_private_message_iterator
6945df9a
SM
1043 * object (iter)
1044 */
1045 bt_self_message_iterator_set_data(self_message_iterator, py_iter);
1046 py_iter = NULL;
1047 goto end;
1048
ce95fb26
SM
1049python_error:
1050 /* Handling of errors that cause a Python exception to be set. */
1051 status = py_exc_to_status_message_iterator(self_message_iterator);
1052 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
1053 goto end;
6945df9a 1054
ce95fb26
SM
1055error:
1056 /* Handling of errors that don't cause a Python exception to be set. */
1057 status = __BT_FUNC_STATUS_ERROR;
6945df9a
SM
1058
1059end:
ce95fb26
SM
1060 BT_ASSERT(!PyErr_Occurred());
1061
6945df9a
SM
1062 Py_XDECREF(py_comp_cls);
1063 Py_XDECREF(py_iter_cls);
1064 Py_XDECREF(py_iter_ptr);
c5f330cd 1065 Py_XDECREF(py_component_port_output_ptr);
6945df9a
SM
1066 Py_XDECREF(py_init_method_result);
1067 Py_XDECREF(py_iter);
1068 return status;
1069}
1070
7744859c 1071static
d24d5663
PP
1072bt_component_class_message_iterator_init_method_status
1073component_class_source_message_iterator_init(
6945df9a
SM
1074 bt_self_message_iterator *self_message_iterator,
1075 bt_self_component_source *self_component_source,
1076 bt_self_component_port_output *self_component_port_output)
1077{
1078 bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source);
d24d5663
PP
1079
1080 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
6945df9a
SM
1081}
1082
7744859c 1083static
d24d5663
PP
1084bt_component_class_message_iterator_init_method_status
1085component_class_filter_message_iterator_init(
6945df9a
SM
1086 bt_self_message_iterator *self_message_iterator,
1087 bt_self_component_filter *self_component_filter,
1088 bt_self_component_port_output *self_component_port_output)
1089{
1090 bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter);
d24d5663
PP
1091
1092 return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output);
6945df9a
SM
1093}
1094
7744859c 1095static
d24d5663 1096void component_class_message_iterator_finalize(
6945df9a
SM
1097 bt_self_message_iterator *message_iterator)
1098{
1099 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1100 PyObject *py_method_result = NULL;
1101
1102 BT_ASSERT(py_message_iter);
1103
1104 /* Call user's _finalize() method */
1105 py_method_result = PyObject_CallMethod(py_message_iter,
1106 "_finalize", NULL);
1107
1108 if (PyErr_Occurred()) {
d24d5663
PP
1109 BT_LOGW("User's _finalize() method raised an exception: ignoring:");
1110 logw_exception();
6945df9a
SM
1111 }
1112
1113 /*
1114 * Ignore any exception raised by the _finalize() method because
1115 * it won't change anything at this point: the component is
1116 * being destroyed anyway.
1117 */
1118 PyErr_Clear();
1119 Py_XDECREF(py_method_result);
1120 Py_DECREF(py_message_iter);
1121}
1122
1123/* Valid for both sources and filters. */
1124
7744859c 1125static
d24d5663
PP
1126bt_component_class_message_iterator_next_method_status
1127component_class_message_iterator_next(
7744859c
SM
1128 bt_self_message_iterator *message_iterator,
1129 bt_message_array_const msgs, uint64_t capacity,
1130 uint64_t *count)
6945df9a 1131{
d24d5663 1132 bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK;
6945df9a
SM
1133 PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator);
1134 PyObject *py_method_result = NULL;
1135
1136 BT_ASSERT(py_message_iter);
1137 py_method_result = PyObject_CallMethod(py_message_iter,
85906b6b 1138 "_bt_next_from_native", NULL);
6945df9a 1139 if (!py_method_result) {
ce95fb26 1140 status = py_exc_to_status_message_iterator(message_iterator);
d24d5663 1141 BT_ASSERT(status != __BT_FUNC_STATUS_OK);
6945df9a
SM
1142 goto end;
1143 }
1144
1145 /*
1146 * The returned object, on success, is an integer object
5602ef81 1147 * (PyLong) containing the address of a native message
6945df9a
SM
1148 * object (which is now ours).
1149 */
babe0791 1150 msgs[0] = PyLong_AsVoidPtr(py_method_result);
6945df9a
SM
1151 *count = 1;
1152
1153 /* Clear potential overflow error; should never happen */
1154 BT_ASSERT(!PyErr_Occurred());
1155 goto end;
1156
1157end:
1158 Py_XDECREF(py_method_result);
1159 return status;
1160}
1161
7744859c 1162static
d24d5663
PP
1163bt_component_class_sink_consume_method_status
1164component_class_sink_consume(bt_self_component_sink *self_component_sink)
6945df9a
SM
1165{
1166 bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink);
1167 PyObject *py_comp = bt_self_component_get_data(self_component);
1168 PyObject *py_method_result = NULL;
d24d5663 1169 bt_component_class_sink_consume_method_status status;
6945df9a
SM
1170
1171 BT_ASSERT(py_comp);
1172 py_method_result = PyObject_CallMethod(py_comp,
1173 "_consume", NULL);
ce95fb26 1174 status = py_exc_to_status_component(self_component);
d24d5663 1175 if (!py_method_result && status == __BT_FUNC_STATUS_OK) {
6945df9a
SM
1176 /* Pretty sure this should never happen, but just in case */
1177 BT_LOGE("User's _consume() method failed without raising an exception: "
1178 "status=%d", status);
d24d5663 1179 status = __BT_FUNC_STATUS_ERROR;
6945df9a
SM
1180 }
1181
1182 Py_XDECREF(py_method_result);
1183 return status;
1184}
1185
1186static
d24d5663 1187int component_class_set_help_and_desc(
6945df9a
SM
1188 bt_component_class *component_class,
1189 const char *description, const char *help)
1190{
1191 int ret;
1192
1193 if (description) {
1194 ret = bt_component_class_set_description(component_class, description);
1195 if (ret) {
1196 BT_LOGE("Cannot set component class's description: "
1197 "comp-cls-addr=%p", component_class);
1198 goto end;
1199 }
1200 }
1201
1202 if (help) {
1203 ret = bt_component_class_set_help(component_class, help);
1204 if (ret) {
1205 BT_LOGE("Cannot set component class's help text: "
1206 "comp-cls-addr=%p", component_class);
1207 goto end;
1208 }
1209 }
1210
1211 ret = 0;
1212
1213end:
1214 return ret;
1215}
1216
1217static
d24d5663 1218bt_component_class_source *bt_bt2_component_class_source_create(
6945df9a
SM
1219 PyObject *py_cls, const char *name, const char *description,
1220 const char *help)
1221{
1222 bt_component_class_source *component_class_source;
1223 bt_component_class *component_class;
1224 int ret;
1225
1226 BT_ASSERT(py_cls);
6945df9a 1227 component_class_source = bt_component_class_source_create(name,
d24d5663 1228 component_class_message_iterator_next);
6945df9a
SM
1229 if (!component_class_source) {
1230 BT_LOGE_STR("Cannot create source component class.");
1231 goto end;
1232 }
1233
1234 component_class = bt_component_class_source_as_component_class(component_class_source);
1235
d24d5663 1236 if (component_class_set_help_and_desc(component_class, description, help)) {
6945df9a
SM
1237 goto end;
1238 }
1239
d24d5663 1240 ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init);
6945df9a 1241 BT_ASSERT(ret == 0);
d24d5663 1242 ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize);
f00b8d40
SM
1243 BT_ASSERT(ret == 0);
1244 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source,
d24d5663 1245 component_class_can_seek_beginning);
f00b8d40
SM
1246 BT_ASSERT(ret == 0);
1247 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source,
d24d5663 1248 component_class_seek_beginning);
6945df9a 1249 BT_ASSERT(ret == 0);
6945df9a 1250 ret = bt_component_class_source_set_output_port_connected_method(component_class_source,
d24d5663 1251 component_class_source_output_port_connected);
6945df9a 1252 BT_ASSERT(ret == 0);
d24d5663 1253 ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query);
6945df9a
SM
1254 BT_ASSERT(ret == 0);
1255 ret = bt_component_class_source_set_message_iterator_init_method(
d24d5663 1256 component_class_source, component_class_source_message_iterator_init);
6945df9a
SM
1257 BT_ASSERT(ret == 0);
1258 ret = bt_component_class_source_set_message_iterator_finalize_method(
d24d5663 1259 component_class_source, component_class_message_iterator_finalize);
6945df9a 1260 BT_ASSERT(ret == 0);
6945df9a
SM
1261 register_cc_ptr_to_py_cls(component_class, py_cls);
1262
1263end:
1264 return component_class_source;
1265}
1266
1267static
d24d5663 1268bt_component_class_filter *bt_bt2_component_class_filter_create(
6945df9a
SM
1269 PyObject *py_cls, const char *name, const char *description,
1270 const char *help)
1271{
1272 bt_component_class *component_class;
1273 bt_component_class_filter *component_class_filter;
1274 int ret;
1275
1276 BT_ASSERT(py_cls);
6945df9a 1277 component_class_filter = bt_component_class_filter_create(name,
d24d5663 1278 component_class_message_iterator_next);
6945df9a
SM
1279 if (!component_class_filter) {
1280 BT_LOGE_STR("Cannot create filter component class.");
1281 goto end;
1282 }
1283
1284 component_class = bt_component_class_filter_as_component_class(component_class_filter);
1285
d24d5663 1286 if (component_class_set_help_and_desc(component_class, description, help)) {
6945df9a
SM
1287 goto end;
1288 }
1289
d24d5663 1290 ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init);
6945df9a 1291 BT_ASSERT(ret == 0);
d24d5663 1292 ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize);
6945df9a 1293 BT_ASSERT(ret == 0);
f00b8d40 1294 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter,
d24d5663 1295 component_class_can_seek_beginning);
f00b8d40
SM
1296 BT_ASSERT(ret == 0);
1297 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter,
d24d5663 1298 component_class_seek_beginning);
f00b8d40 1299 BT_ASSERT(ret == 0);
6945df9a 1300 ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter,
d24d5663 1301 component_class_filter_input_port_connected);
6945df9a
SM
1302 BT_ASSERT(ret == 0);
1303 ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter,
d24d5663 1304 component_class_filter_output_port_connected);
6945df9a 1305 BT_ASSERT(ret == 0);
d24d5663 1306 ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query);
6945df9a
SM
1307 BT_ASSERT(ret == 0);
1308 ret = bt_component_class_filter_set_message_iterator_init_method(
d24d5663 1309 component_class_filter, component_class_filter_message_iterator_init);
6945df9a
SM
1310 BT_ASSERT(ret == 0);
1311 ret = bt_component_class_filter_set_message_iterator_finalize_method(
d24d5663 1312 component_class_filter, component_class_message_iterator_finalize);
6945df9a 1313 BT_ASSERT(ret == 0);
6945df9a
SM
1314 register_cc_ptr_to_py_cls(component_class, py_cls);
1315
1316end:
1317 return component_class_filter;
1318}
1319
1320static
d24d5663 1321bt_component_class_sink *bt_bt2_component_class_sink_create(
6945df9a
SM
1322 PyObject *py_cls, const char *name, const char *description,
1323 const char *help)
1324{
1325 bt_component_class_sink *component_class_sink;
1326 bt_component_class *component_class;
1327 int ret;
1328
1329 BT_ASSERT(py_cls);
d24d5663 1330 component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume);
6945df9a
SM
1331
1332 if (!component_class_sink) {
1333 BT_LOGE_STR("Cannot create sink component class.");
1334 goto end;
1335 }
1336
1337 component_class = bt_component_class_sink_as_component_class(component_class_sink);
1338
d24d5663 1339 if (component_class_set_help_and_desc(component_class, description, help)) {
6945df9a
SM
1340 goto end;
1341 }
1342
d24d5663 1343 ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init);
6945df9a 1344 BT_ASSERT(ret == 0);
d24d5663 1345 ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize);
6945df9a 1346 BT_ASSERT(ret == 0);
6945df9a 1347 ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink,
d24d5663 1348 component_class_sink_input_port_connected);
6945df9a
SM
1349 BT_ASSERT(ret == 0);
1350 ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink,
d24d5663 1351 component_class_sink_graph_is_configured);
6945df9a 1352 BT_ASSERT(ret == 0);
d24d5663 1353 ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query);
6945df9a 1354 BT_ASSERT(ret == 0);
6945df9a
SM
1355 register_cc_ptr_to_py_cls(component_class, py_cls);
1356
1357end:
1358 return component_class_sink;
1359}
1360%}
1361
d24d5663 1362struct bt_component_class_source *bt_bt2_component_class_source_create(
6945df9a
SM
1363 PyObject *py_cls, const char *name, const char *description,
1364 const char *help);
d24d5663 1365struct bt_component_class_filter *bt_bt2_component_class_filter_create(
6945df9a
SM
1366 PyObject *py_cls, const char *name, const char *description,
1367 const char *help);
d24d5663 1368struct bt_component_class_sink *bt_bt2_component_class_sink_create(
6945df9a
SM
1369 PyObject *py_cls, const char *name, const char *description,
1370 const char *help);
d24d5663
PP
1371void bt_bt2_cc_init_from_bt2(void);
1372void bt_bt2_cc_exit_handler(void);
This page took 0.09757 seconds and 4 git commands to generate.