2 * The MIT License (MIT)
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 #include <babeltrace/component/component-class.h>
27 #include <babeltrace/component/component-class-source.h>
28 #include <babeltrace/component/component-class-sink.h>
29 #include <babeltrace/component/component-class-filter.h>
30 #include <babeltrace/component/notification/iterator.h>
31 #include <babeltrace/component/notification/notification.h>
37 struct bt_component_class;
40 enum bt_component_class_type {
41 BT_COMPONENT_CLASS_TYPE_UNKNOWN = -1,
42 BT_COMPONENT_CLASS_TYPE_SOURCE = 0,
43 BT_COMPONENT_CLASS_TYPE_SINK = 1,
44 BT_COMPONENT_CLASS_TYPE_FILTER = 2,
47 /* General functions */
48 const char *bt_component_class_get_name(
49 struct bt_component_class *component_class);
50 const char *bt_component_class_get_description(
51 struct bt_component_class *component_class);
52 const char *bt_component_class_get_help(
53 struct bt_component_class *component_class);
54 enum bt_component_class_type bt_component_class_get_type(
55 struct bt_component_class *component_class);
59 * This hash table associates a BT component class object address to a
60 * user-defined Python class (PyObject *). The keys and values are NOT
61 * owned by this hash table. The Python class objects are owned by the
62 * Python module, which should not be unloaded until it is not possible
63 * to create a user Python component anyway.
65 * This hash table is written to when a user-defined Python component
66 * class is created by one of the bt_py3_component_class_*_create()
69 * This function is read from when a user calls bt_component_create()
70 * with a component class pointer created by one of the functions above.
71 * In this case, the original Python class needs to be found to
72 * instantiate it and associate the created Python component object with
73 * a BT component object instance.
76 static GHashTable *bt_cc_ptr_to_py_cls;
78 static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
81 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
85 static PyObject *lookup_cc_ptr_to_py_cls(struct bt_component_class *bt_cc)
87 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
88 (gconstpointer) bt_cc);
93 * Useful Python objects.
96 static PyObject *py_mod_bt2 = NULL;
97 static PyObject *py_mod_bt2_exc_error_type = NULL;
98 static PyObject *py_mod_bt2_exc_unsupported_feature_type = NULL;
99 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
100 static PyObject *py_mod_bt2_exc_stop_type = NULL;
101 static PyObject *py_mod_bt2_values = NULL;
102 static PyObject *py_mod_bt2_values_create_from_ptr_func = NULL;
104 static void bt_py3_cc_init_from_bt2(void)
107 * This is called once the bt2 package is loaded.
109 * Those modules and functions are needed while the package is
110 * used. Loading them here is safe because we know the bt2
111 * package is imported, and we know that the user cannot use the
112 * code here without importing bt2 first.
114 py_mod_bt2 = PyImport_ImportModule("bt2");
116 py_mod_bt2_values = PyImport_ImportModule("bt2.values");
117 assert(py_mod_bt2_values);
118 py_mod_bt2_values_create_from_ptr_func =
119 PyObject_GetAttrString(py_mod_bt2_values,
121 assert(py_mod_bt2_values_create_from_ptr_func);
122 py_mod_bt2_exc_error_type =
123 PyObject_GetAttrString(py_mod_bt2, "Error");
124 assert(py_mod_bt2_exc_error_type);
125 py_mod_bt2_exc_unsupported_feature_type =
126 PyObject_GetAttrString(py_mod_bt2, "UnsupportedFeature");
127 py_mod_bt2_exc_try_again_type =
128 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
129 py_mod_bt2_exc_stop_type =
130 PyObject_GetAttrString(py_mod_bt2, "Stop");
131 assert(py_mod_bt2_exc_stop_type);
134 static void bt_py3_cc_exit_handler(void)
137 * This is an exit handler (set by the bt2 package).
139 * We only give back the references that we took in
140 * bt_py3_cc_init_from_bt2() here. The global variables continue
141 * to exist for the code of this file, but they are now borrowed
142 * references. If this code is executed, it means that somehow
143 * to modules are still loaded, so it should be safe to use them
144 * even without a strong reference.
146 * We cannot do this in the library's destructor because it
147 * gets executed once Python is already finalized.
149 Py_XDECREF(py_mod_bt2_values_create_from_ptr_func);
150 Py_XDECREF(py_mod_bt2_values);
151 Py_XDECREF(py_mod_bt2);
152 Py_XDECREF(py_mod_bt2_exc_error_type);
153 Py_XDECREF(py_mod_bt2_exc_unsupported_feature_type);
154 Py_XDECREF(py_mod_bt2_exc_try_again_type);
155 Py_XDECREF(py_mod_bt2_exc_stop_type);
159 /* Library constructor and destructor */
161 __attribute__((constructor))
162 static void bt_py3_native_comp_class_ctor(void) {
163 /* Initialize component class association hash table */
164 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
165 assert(bt_cc_ptr_to_py_cls);
168 __attribute__((destructor))
169 static void bt_py3_native_comp_class_dtor(void) {
170 /* Destroy component class association hash table */
171 if (bt_cc_ptr_to_py_cls) {
172 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
177 /* Converts a BT value object to a bt2.values object */
179 static PyObject *bt_py3_bt_value_from_ptr(struct bt_value *value)
181 PyObject *py_create_from_ptr_func = NULL;
182 PyObject *py_value = NULL;
183 PyObject *py_ptr = NULL;
185 py_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(value),
186 SWIGTYPE_p_bt_value, 0);
187 py_value = PyObject_CallFunctionObjArgs(
188 py_mod_bt2_values_create_from_ptr_func, py_ptr, NULL);
194 * _create_from_ptr() only wraps an existing reference. `value`
195 * is a borrowed reference here, so increment its reference
196 * count for this new owner.
207 /* self.__init__(*args, **kwargs) */
209 static int bt_py3_call_self_init(PyObject *py_self, PyObject *py_args,
214 PyObject *py_init_method_result = NULL;
215 PyObject *py_init_method = NULL;
216 PyObject *py_init_method_args = NULL;
217 PyObject *py_class = NULL;
219 py_class = PyObject_GetAttrString(py_self, "__class__");
224 py_init_method = PyObject_GetAttrString(py_class, "__init__");
225 if (!py_init_method) {
229 /* Prepend py_self to the arguments (copy them to new tuple) */
230 py_init_method_args = PyTuple_New(PyTuple_Size(py_args) + 1);
231 if (!py_init_method_args) {
236 ret = PyTuple_SetItem(py_init_method_args, 0, py_self);
242 for (i = 0; i < PyTuple_Size(py_args); i++) {
243 PyObject *obj = PyTuple_GetItem(py_args, i);
249 ret = PyTuple_SetItem(py_init_method_args, i + 1, obj);
256 py_init_method_result = PyObject_Call(py_init_method,
257 py_init_method_args, py_kwargs);
258 if (!py_init_method_result) {
268 Py_XDECREF(py_init_method_args);
269 Py_XDECREF(py_init_method_result);
270 Py_XDECREF(py_init_method);
271 Py_XDECREF(py_class);
276 /* Component class proxy methods (delegate to the attached Python object) */
278 static enum bt_component_status bt_py3_cc_init(
279 struct bt_component *component, struct bt_value *params,
280 void *init_method_data)
282 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
286 * This function is either called from
287 * _UserComponentType.__call__() (when init_method_data contains
288 * the PyObject * currently creating this component) or from
289 * other code (init_method_data is NULL).
291 if (init_method_data) {
293 * Called from _UserComponentType.__call__().
295 * The `params` argument is completely ignored here,
296 * because if the user creating the component object
297 * from the component class object wants parameters,
298 * they'll be in the keyword arguments anyway.
300 PyObject *py_comp = init_method_data;
301 PyObject *py_comp_ptr = NULL;
302 PyObject *py_init_args = NULL;
303 PyObject *py_init_kwargs = NULL;
306 * No need to take a Python reference on py_comp here:
307 * we store it as a _borrowed_ reference. When all the
308 * Python references are dropped, the object's __del__()
309 * method is called. This method calls this side
310 * (bt_py3_component_on_del()) to swap the ownership:
311 * this BT component becomes the only owner of the
314 bt_component_set_private_data(component, py_comp);
317 * Set this BT component pointer as py_comp._ptr, and
318 * take a reference on it (on success, at the end).
320 * This reference is put in the Python component
321 * object's __del__() method.
323 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component),
324 SWIGTYPE_p_bt_component, 0);
327 goto with_init_method_data_error;
330 ret = PyObject_SetAttrString(py_comp, "_ptr", py_comp_ptr);
333 goto with_init_method_data_error;
337 * _UserComponentType.__call__() puts the arguments and
338 * keyword arguments to use for the py_comp.__init__()
339 * call in py_comp._init_args and py_comp._init_kwargs.
341 * We need to get them and remove them from py_comp before
342 * calling py_comp.__init__().
344 py_init_args = PyObject_GetAttrString(py_comp, "_init_args");
347 goto with_init_method_data_error;
350 py_init_kwargs = PyObject_GetAttrString(py_comp, "_init_kwargs");
351 if (!py_init_kwargs) {
353 goto with_init_method_data_error;
356 ret = PyObject_DelAttrString(py_comp, "_init_args");
359 goto with_init_method_data_error;
362 ret = PyObject_DelAttrString(py_comp, "_init_kwargs");
365 goto with_init_method_data_error;
368 /* Ready to call py_comp.__init__() */
369 ret = bt_py3_call_self_init(py_comp, py_init_args,
373 * We do not clear any raised exception here
374 * so that the Python caller (the one who is
375 * instantiating the class) gets the exception
376 * from the __init__() method.
378 goto with_init_method_data_error;
382 * py_comp.__init__() went well: get the native BT
383 * component object reference for the Python object.
386 goto with_init_method_data_end;
388 with_init_method_data_error:
389 status = BT_COMPONENT_STATUS_ERROR;
392 * Reset py_comp._ptr to None to signal the error to
393 * _UserComponentType.__call__(). For exceptions raised
394 * from py_comp.__init__(), this is not important,
395 * because __call__() will also raise before even
396 * checking py_comp._ptr.
398 if (PyObject_HasAttrString(py_comp, "_ptr")) {
399 PyObject *py_err_type, *py_err_value, *py_err_traceback;
400 PyErr_Fetch(&py_err_type, &py_err_value,
403 PyObject_SetAttrString(py_comp, "_ptr", Py_None);
404 PyErr_Restore(py_err_type, py_err_value,
409 * Important: remove py_comp from our the BT component's
410 * private data. Since we're failing,
411 * bt_py3_cc_destroy() is about to be called: it must
412 * not consider py_comp. py_comp's __del__() method
413 * will be called (because the instance exists), but
414 * since py_comp._ptr is None, it won't do anything.
416 bt_component_set_private_data(component, NULL);
418 with_init_method_data_end:
419 Py_XDECREF(py_comp_ptr);
420 Py_XDECREF(py_init_args);
421 Py_XDECREF(py_init_kwargs);
424 * Not called from _UserComponentType.__call__().
426 * In this case we need to instantiate the user-defined
427 * Python class. To do this we call the user-defined
428 * class manually with this already-created BT component
429 * object pointer as the __comp_ptr keyword argument.
431 * We keep a reference, the only existing one, to the
432 * created Python object. bt_py3_component_on_del() will
433 * never be called by the object's __del__() method
434 * because _UserComponentType.__call__() marks the
435 * object as owned by the native BT component object.
437 PyObject *py_cls = NULL;
438 PyObject *py_comp = NULL;
439 PyObject *py_params = NULL;
440 PyObject *py_comp_ptr = NULL;
441 PyObject *py_cls_ctor_args = NULL;
442 PyObject *py_cls_ctor_kwargs = NULL;
445 * Get the user-defined Python class which created this
446 * component's class in the first place (borrowed
449 py_cls = lookup_cc_ptr_to_py_cls(bt_component_get_class(component));
451 goto without_init_method_data_error;
454 /* BT value object -> Python bt2.values object */
455 py_params = bt_py3_bt_value_from_ptr(params);
457 goto without_init_method_data_error;
460 /* Component pointer -> SWIG pointer Python object */
461 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component),
462 SWIGTYPE_p_bt_component, 0);
464 goto without_init_method_data_error;
468 * Do the equivalent of this:
470 * py_comp = py_cls(__comp_ptr=py_comp_ptr, params=py_params)
472 * _UserComponentType.__call__() calls the Python
473 * component object's __init__() function itself in this
476 py_cls_ctor_args = PyTuple_New(0);
477 if (!py_cls_ctor_args) {
478 goto without_init_method_data_error;
481 py_cls_ctor_kwargs = PyDict_New();
482 if (!py_cls_ctor_kwargs) {
483 goto without_init_method_data_error;
486 ret = PyDict_SetItemString(py_cls_ctor_kwargs, "__comp_ptr",
489 goto without_init_method_data_error;
492 ret = PyDict_SetItemString(py_cls_ctor_kwargs, "params",
495 goto without_init_method_data_error;
498 py_comp = PyObject_Call(py_cls, py_cls_ctor_args,
501 goto without_init_method_data_error;
505 * Our user Python component object is now fully created
506 * and initialized by the user. Since we just created
507 * it, this BT component is its only (persistent) owner.
509 bt_component_set_private_data(component, py_comp);
511 goto without_init_method_data_end;
513 without_init_method_data_error:
514 status = BT_COMPONENT_STATUS_ERROR;
517 * Clear any exception: we're returning a bad status
518 * anyway. If this call originated from Python (creation
519 * from a plugin's component class, for example), then
520 * the user gets an appropriate creation error.
524 without_init_method_data_end:
526 Py_XDECREF(py_params);
527 Py_XDECREF(py_comp_ptr);
528 Py_XDECREF(py_cls_ctor_args);
529 Py_XDECREF(py_cls_ctor_kwargs);
535 static void bt_py3_cc_destroy(struct bt_component *component)
537 PyObject *py_comp = bt_component_get_private_data(component);
538 PyObject *py_destroy_method_result = NULL;
542 * If we're here, this BT component is the only owner of
543 * its private user Python object. It is safe to
544 * decrement its reference count, and thus destroy it.
546 * We call its _destroy() method before doing so to notify
549 py_destroy_method_result = PyObject_CallMethod(py_comp,
553 * Ignore any exception raised by the _destroy() method
554 * because it won't change anything at this point: the
555 * component is being destroyed anyway.
558 Py_XDECREF(py_destroy_method_result);
563 static enum bt_notification_iterator_status bt_py3_exc_to_notif_iter_status(void)
565 enum bt_notification_iterator_status status =
566 BT_NOTIFICATION_ITERATOR_STATUS_OK;
567 PyObject *exc = PyErr_Occurred();
573 if (PyErr_GivenExceptionMatches(exc,
574 py_mod_bt2_exc_unsupported_feature_type)) {
575 status = BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED;
576 } else if (PyErr_GivenExceptionMatches(exc,
577 py_mod_bt2_exc_stop_type)) {
578 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
580 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
588 static enum bt_notification_iterator_status bt_py3_cc_notification_iterator_init(
589 struct bt_component *component,
590 struct bt_notification_iterator *iter, void *init_method_data)
592 enum bt_notification_iterator_status status =
593 BT_NOTIFICATION_ITERATOR_STATUS_OK;
594 PyObject *py_comp_cls = NULL;
595 PyObject *py_iter_cls = NULL;
596 PyObject *py_iter_ptr = NULL;
597 PyObject *py_init_method_result = NULL;
598 PyObject *py_iter = NULL;
599 PyObject *py_comp = bt_component_get_private_data(component);
601 /* Find user's Python notification iterator class */
602 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
607 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
613 * Create object with borrowed BT notification iterator
616 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
618 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(iter),
619 SWIGTYPE_p_bt_notification_iterator, 0);
624 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
625 "(OO)", py_iter_cls, py_iter_ptr);
635 * At this point, py_iter._ptr is set, so this initialization
636 * function has access to the py_iter.component property (which
637 * gives it the user Python component object from which the
638 * iterator was created).
640 py_init_method_result = PyObject_CallMethod(py_iter, "__init__", NULL);
641 if (!py_init_method_result) {
646 * Since the Python code can never instantiate a user-defined
647 * notification iterator class, the BT notification iterator
648 * object does NOT belong to a user Python notification iterator
649 * object (borrowed reference). However this Python object is
650 * owned by this BT notification iterator object.
652 * In the Python world, the lifetime of the BT notification
653 * iterator is managed by a _GenericNotificationIterator
656 * _GenericNotificationIterator instance:
657 * owns a native bt_notification_iterator object (iter)
658 * owns a UserNotificationIterator instance (py_iter)
659 * self._ptr is a borrowed reference to the
660 * native bt_notification_iterator object (iter)
662 bt_notification_iterator_set_private_data(iter, py_iter);
667 status = bt_py3_exc_to_notif_iter_status();
668 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
670 * Looks like there wasn't any exception from the Python
671 * side, but we're still in an error state here.
673 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
677 * Clear any exception: we're returning a bad status anyway. If
678 * this call originated from Python, then the user gets an
679 * appropriate creation error.
684 Py_XDECREF(py_comp_cls);
685 Py_XDECREF(py_iter_cls);
686 Py_XDECREF(py_iter_ptr);
687 Py_XDECREF(py_init_method_result);
692 static void bt_py3_cc_notification_iterator_destroy(
693 struct bt_notification_iterator *iter)
695 PyObject *py_notif_iter =
696 bt_notification_iterator_get_private_data(iter);
697 PyObject *py_destroy_method_result = NULL;
699 assert(py_notif_iter);
700 py_destroy_method_result = PyObject_CallMethod(py_notif_iter,
704 * Ignore any exception raised by the _destroy() method because
705 * it won't change anything at this point. The notification
706 * iterator is being destroyed anyway.
709 Py_XDECREF(py_destroy_method_result);
710 Py_DECREF(py_notif_iter);
713 static struct bt_notification *bt_py3_cc_notification_iterator_get(
714 struct bt_notification_iterator *iter)
716 PyObject *py_notif_iter =
717 bt_notification_iterator_get_private_data(iter);
718 PyObject *py_get_method_result = NULL;
719 struct bt_notification *notif = NULL;
721 assert(py_notif_iter);
722 py_get_method_result = PyObject_CallMethod(py_notif_iter,
723 "_get_from_bt", NULL);
724 if (!py_get_method_result) {
729 * The returned object, on success, is an integer object
730 * (PyLong) containing the address of a BT notification
731 * object (which is not ours).
733 notif = (struct bt_notification *) PyLong_AsUnsignedLongLong(
734 py_get_method_result);
736 /* Clear potential overflow error; should never happen */
737 if (PyErr_Occurred()) {
751 * Clear any exception: we're returning NULL anyway. If this
752 * call originated from Python, then the user gets an
758 Py_XDECREF(py_get_method_result);
762 static enum bt_notification_iterator_status bt_py3_cc_notification_iterator_next(
763 struct bt_notification_iterator *iter)
765 enum bt_notification_iterator_status status;
766 PyObject *py_notif_iter =
767 bt_notification_iterator_get_private_data(iter);
768 PyObject *py_next_method_result = NULL;
770 assert(py_notif_iter);
771 py_next_method_result = PyObject_CallMethod(py_notif_iter,
773 status = bt_py3_exc_to_notif_iter_status();
774 if (!py_next_method_result
775 && status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
776 /* Pretty sure this should never happen, but just in case */
777 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
780 Py_XDECREF(py_next_method_result);
784 static enum bt_notification_iterator_status bt_py3_cc_notification_iterator_seek_time(
785 struct bt_notification_iterator *iter, int64_t time)
787 enum bt_notification_iterator_status status;
788 PyObject *py_notif_iter =
789 bt_notification_iterator_get_private_data(iter);
790 PyObject *py_seek_to_time_method_result = NULL;
792 assert(py_notif_iter);
793 py_seek_to_time_method_result = PyObject_CallMethod(py_notif_iter,
794 "_seek_to_time", "(L)", time);
795 status = bt_py3_exc_to_notif_iter_status();
796 if (!py_seek_to_time_method_result
797 && status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
798 /* Pretty sure this should never happen, but just in case */
799 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
802 Py_XDECREF(py_seek_to_time_method_result);
806 static enum bt_component_status bt_py3_exc_to_component_status(void)
808 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
809 PyObject *exc = PyErr_Occurred();
815 if (PyErr_GivenExceptionMatches(exc,
816 py_mod_bt2_exc_unsupported_feature_type)) {
817 status = BT_COMPONENT_STATUS_UNSUPPORTED;
818 } else if (PyErr_GivenExceptionMatches(exc,
819 py_mod_bt2_exc_try_again_type)) {
820 status = BT_COMPONENT_STATUS_AGAIN;
821 } else if (PyErr_GivenExceptionMatches(exc,
822 py_mod_bt2_exc_stop_type)) {
823 status = BT_COMPONENT_STATUS_END;
825 status = BT_COMPONENT_STATUS_ERROR;
833 static enum bt_component_status bt_py3_cc_sink_consume(
834 struct bt_component *component)
836 PyObject *py_comp = bt_component_get_private_data(component);
837 PyObject *py_consume_method_result = NULL;
838 enum bt_component_status status;
840 py_consume_method_result = PyObject_CallMethod(py_comp,
842 status = bt_py3_exc_to_component_status();
843 if (!py_consume_method_result && status == BT_COMPONENT_STATUS_OK) {
844 /* Pretty sure this should never happen, but just in case */
845 status = BT_COMPONENT_STATUS_ERROR;
848 Py_XDECREF(py_consume_method_result);
852 static enum bt_component_status bt_py3_cc_sink_filter_add_iterator(
853 struct bt_component *component,
854 struct bt_notification_iterator *iter)
856 PyObject *py_comp = bt_component_get_private_data(component);
857 PyObject *py_add_iterator_method_result = NULL;
858 PyObject *py_notif_iter_ptr = NULL;
859 enum bt_component_status status;
861 py_notif_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(iter),
862 SWIGTYPE_p_bt_notification_iterator, 0);
863 if (!py_notif_iter_ptr) {
865 status = BT_COMPONENT_STATUS_ERROR;
870 py_add_iterator_method_result = PyObject_CallMethod(py_comp,
871 "_add_iterator_from_bt", "(O)", py_notif_iter_ptr);
872 status = bt_py3_exc_to_component_status();
873 if (!py_add_iterator_method_result
874 && status == BT_COMPONENT_STATUS_OK) {
875 /* Pretty sure this should never happen, but just in case */
876 status = BT_COMPONENT_STATUS_ERROR;
880 Py_XDECREF(py_notif_iter_ptr);
881 Py_XDECREF(py_add_iterator_method_result);
886 /* Component class creation functions (called from Python module) */
888 static int bt_py3_cc_set_optional_attrs_methods(struct bt_component_class *cc,
889 const char *description, const char *help)
894 ret = bt_component_class_set_description(cc, description);
901 ret = bt_component_class_set_help(cc, help);
907 ret = bt_component_class_set_init_method(cc, bt_py3_cc_init);
912 ret = bt_component_class_set_destroy_method(cc, bt_py3_cc_destroy);
921 static int bt_py3_cc_set_optional_iter_methods(struct bt_component_class *cc,
923 int (*set_notif_iter_init_method)(struct bt_component_class *, bt_component_class_notification_iterator_init_method),
924 int (*set_notif_iter_destroy_method)(struct bt_component_class *, bt_component_class_notification_iterator_destroy_method),
925 int (*set_notif_iter_seek_time_method)(struct bt_component_class *, bt_component_class_notification_iterator_seek_time_method))
929 ret = set_notif_iter_init_method(
930 cc, bt_py3_cc_notification_iterator_init);
935 ret = set_notif_iter_destroy_method(
936 cc, bt_py3_cc_notification_iterator_destroy);
942 ret = set_notif_iter_seek_time_method(
943 cc, bt_py3_cc_notification_iterator_seek_time);
953 static struct bt_component_class *bt_py3_component_class_source_create(
954 PyObject *py_cls, const char *name, const char *description,
955 const char *help, bool has_seek_time)
957 struct bt_component_class *cc;
961 cc = bt_component_class_source_create(name,
962 bt_py3_cc_notification_iterator_get,
963 bt_py3_cc_notification_iterator_next);
968 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
974 ret = bt_py3_cc_set_optional_iter_methods(cc, has_seek_time,
975 bt_component_class_source_set_notification_iterator_init_method,
976 bt_component_class_source_set_notification_iterator_destroy_method,
977 bt_component_class_source_set_notification_iterator_seek_time_method);
983 register_cc_ptr_to_py_cls(cc, py_cls);
984 bt_component_class_freeze(cc);
990 static struct bt_component_class *bt_py3_component_class_filter_create(
991 PyObject *py_cls, const char *name, const char *description,
992 const char *help, bool has_seek_time)
994 struct bt_component_class *cc;
998 cc = bt_component_class_filter_create(name,
999 bt_py3_cc_notification_iterator_get,
1000 bt_py3_cc_notification_iterator_next);
1005 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
1011 ret = bt_py3_cc_set_optional_iter_methods(cc, has_seek_time,
1012 bt_component_class_filter_set_notification_iterator_init_method,
1013 bt_component_class_filter_set_notification_iterator_destroy_method,
1014 bt_component_class_filter_set_notification_iterator_seek_time_method);
1020 ret = bt_component_class_filter_set_add_iterator_method(cc,
1021 bt_py3_cc_sink_filter_add_iterator);
1027 register_cc_ptr_to_py_cls(cc, py_cls);
1028 bt_component_class_freeze(cc);
1034 static struct bt_component_class *bt_py3_component_class_sink_create(
1035 PyObject *py_cls, const char *name, const char *description,
1038 struct bt_component_class *cc;
1042 cc = bt_component_class_sink_create(name, bt_py3_cc_sink_consume);
1047 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
1053 ret = bt_component_class_sink_set_add_iterator_method(cc,
1054 bt_py3_cc_sink_filter_add_iterator);
1060 register_cc_ptr_to_py_cls(cc, py_cls);
1061 bt_component_class_freeze(cc);
1068 /* Component creation function (called from Python module) */
1070 static void bt_py3_component_create(
1071 struct bt_component_class *comp_class, PyObject *py_self,
1074 struct bt_component *component =
1075 bt_component_create_with_init_method_data(comp_class,
1076 name, NULL, py_self);
1079 * Our component initialization function, bt_py3_cc_init(), sets
1080 * a new reference to the created component in the user's Python
1081 * object (py_self._ptr). This is where the reference is kept.
1082 * We don't need the returned component in this case (if any,
1083 * because it might be NULL if the creation failed, most
1084 * probably because py_self.__init__() raised something).
1090 /* Component delete notification */
1092 static void bt_py3_component_on_del(PyObject *py_comp)
1095 * The Python component's __del__() function calls this to
1096 * indicate that there's no more reference on it from the
1099 * Since the BT component can continue to live once the Python
1100 * component object is deleted, we increment the Python
1101 * component's reference count so that it now only belong to the
1102 * BT component. We will decrement this reference count once
1103 * the BT component is destroyed in bt_py3_cc_destroy().
1110 %exception bt_py3_component_create {
1112 if (PyErr_Occurred()) {
1113 /* Exception is already set by bt_py3_component_create() */
1118 struct bt_component_class *bt_py3_component_class_source_create(
1119 PyObject *py_cls, const char *name, const char *description,
1120 const char *help, bool has_seek_time);
1121 struct bt_component_class *bt_py3_component_class_filter_create(
1122 PyObject *py_cls, const char *name, const char *description,
1123 const char *help, bool has_seek_time);
1124 struct bt_component_class *bt_py3_component_class_sink_create(
1125 PyObject *py_cls, const char *name, const char *description,
1127 void bt_py3_component_create(
1128 struct bt_component_class *comp_class, PyObject *py_self,
1130 void bt_py3_component_on_del(PyObject *py_comp);
1131 void bt_py3_cc_init_from_bt2(void);
1132 void bt_py3_cc_exit_handler(void);