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 enum bt_component_class_type bt_component_class_get_type(
53 struct bt_component_class *component_class);
57 * This hash table associates a BT component class object address to a
58 * user-defined Python class (PyObject *). The keys and values are NOT
59 * owned by this hash table. The Python class objects are owned by the
60 * Python module, which should not be unloaded until it is not possible
61 * to create a user Python component anyway.
63 * This hash table is written to when a user-defined Python component
64 * class is created by one of the bt_py3_component_class_*_create()
67 * This function is read from when a user calls bt_component_create()
68 * with a component class pointer created by one of the functions above.
69 * In this case, the original Python class needs to be found to
70 * instantiate it and associate the created Python component object with
71 * a BT component object instance.
74 static GHashTable *bt_cc_ptr_to_py_cls;
76 static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
79 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
83 static PyObject *lookup_cc_ptr_to_py_cls(struct bt_component_class *bt_cc)
85 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
86 (gconstpointer) bt_cc);
91 * Useful Python objects.
94 static PyObject *py_mod_bt2 = NULL;
95 static PyObject *py_mod_bt2_exc_error_type = NULL;
96 static PyObject *py_mod_bt2_exc_unsupported_feature_type = NULL;
97 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
98 static PyObject *py_mod_bt2_exc_stop_type = NULL;
99 static PyObject *py_mod_bt2_values = NULL;
100 static PyObject *py_mod_bt2_values_create_from_ptr_func = NULL;
102 static void bt_py3_cc_init_from_bt2(void)
105 * This is called once the bt2 package is loaded.
107 * Those modules and functions are needed while the package is
108 * used. Loading them here is safe because we know the bt2
109 * package is imported, and we know that the user cannot use the
110 * code here without importing bt2 first.
112 py_mod_bt2 = PyImport_ImportModule("bt2");
114 py_mod_bt2_values = PyImport_ImportModule("bt2.values");
115 assert(py_mod_bt2_values);
116 py_mod_bt2_values_create_from_ptr_func =
117 PyObject_GetAttrString(py_mod_bt2_values,
119 assert(py_mod_bt2_values_create_from_ptr_func);
120 py_mod_bt2_exc_error_type =
121 PyObject_GetAttrString(py_mod_bt2, "Error");
122 assert(py_mod_bt2_exc_error_type);
123 py_mod_bt2_exc_unsupported_feature_type =
124 PyObject_GetAttrString(py_mod_bt2, "UnsupportedFeature");
125 py_mod_bt2_exc_try_again_type =
126 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
127 py_mod_bt2_exc_stop_type =
128 PyObject_GetAttrString(py_mod_bt2, "Stop");
129 assert(py_mod_bt2_exc_stop_type);
132 static void bt_py3_cc_exit_handler(void)
135 * This is an exit handler (set by the bt2 package).
137 * We only give back the references that we took in
138 * bt_py3_cc_init_from_bt2() here. The global variables continue
139 * to exist for the code of this file, but they are now borrowed
140 * references. If this code is executed, it means that somehow
141 * to modules are still loaded, so it should be safe to use them
142 * even without a strong reference.
144 * We cannot do this in the library's destructor because it
145 * gets executed once Python is already finalized.
147 Py_XDECREF(py_mod_bt2_values_create_from_ptr_func);
148 Py_XDECREF(py_mod_bt2_values);
149 Py_XDECREF(py_mod_bt2);
150 Py_XDECREF(py_mod_bt2_exc_error_type);
151 Py_XDECREF(py_mod_bt2_exc_unsupported_feature_type);
152 Py_XDECREF(py_mod_bt2_exc_try_again_type);
153 Py_XDECREF(py_mod_bt2_exc_stop_type);
157 /* Library constructor and destructor */
159 __attribute__((constructor))
160 static void bt_py3_native_comp_class_ctor(void) {
161 /* Initialize component class association hash table */
162 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
163 assert(bt_cc_ptr_to_py_cls);
166 __attribute__((destructor))
167 static void bt_py3_native_comp_class_dtor(void) {
168 /* Destroy component class association hash table */
169 if (bt_cc_ptr_to_py_cls) {
170 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
175 /* Converts a BT value object to a bt2.values object */
177 static PyObject *bt_py3_bt_value_from_ptr(struct bt_value *value)
179 PyObject *py_create_from_ptr_func = NULL;
180 PyObject *py_value = NULL;
181 PyObject *py_ptr = NULL;
183 py_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(value),
184 SWIGTYPE_p_bt_value, 0);
185 py_value = PyObject_CallFunctionObjArgs(
186 py_mod_bt2_values_create_from_ptr_func, py_ptr, NULL);
192 * _create_from_ptr() only wraps an existing reference. `value`
193 * is a borrowed reference here, so increment its reference
194 * count for this new owner.
205 /* self.__init__(*args, **kwargs) */
207 static int bt_py3_call_self_init(PyObject *py_self, PyObject *py_args,
212 PyObject *py_init_method_result = NULL;
213 PyObject *py_init_method = NULL;
214 PyObject *py_init_method_args = NULL;
215 PyObject *py_class = NULL;
217 py_class = PyObject_GetAttrString(py_self, "__class__");
222 py_init_method = PyObject_GetAttrString(py_class, "__init__");
223 if (!py_init_method) {
227 /* Prepend py_self to the arguments (copy them to new tuple) */
228 py_init_method_args = PyTuple_New(PyTuple_Size(py_args) + 1);
229 if (!py_init_method_args) {
234 ret = PyTuple_SetItem(py_init_method_args, 0, py_self);
240 for (i = 0; i < PyTuple_Size(py_args); i++) {
241 PyObject *obj = PyTuple_GetItem(py_args, i);
247 ret = PyTuple_SetItem(py_init_method_args, i + 1, obj);
254 py_init_method_result = PyObject_Call(py_init_method,
255 py_init_method_args, py_kwargs);
256 if (!py_init_method_result) {
266 Py_XDECREF(py_init_method_args);
267 Py_XDECREF(py_init_method_result);
268 Py_XDECREF(py_init_method);
269 Py_XDECREF(py_class);
274 /* Component class proxy methods (delegate to the attached Python object) */
276 static enum bt_component_status bt_py3_cc_init(
277 struct bt_component *component, struct bt_value *params,
278 void *init_method_data)
280 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
284 * This function is either called from
285 * _UserComponentType.__call__() (when init_method_data contains
286 * the PyObject * currently creating this component) or from
287 * other code (init_method_data is NULL).
289 if (init_method_data) {
291 * Called from _UserComponentType.__call__().
293 * The `params` argument is completely ignored here,
294 * because if the user creating the component object
295 * from the component class object wants parameters,
296 * they'll be in the keyword arguments anyway.
298 PyObject *py_comp = init_method_data;
299 PyObject *py_comp_ptr = NULL;
300 PyObject *py_init_args = NULL;
301 PyObject *py_init_kwargs = NULL;
304 * No need to take a Python reference on py_comp here:
305 * we store it as a _borrowed_ reference. When all the
306 * Python references are dropped, the object's __del__()
307 * method is called. This method calls this side
308 * (bt_py3_component_on_del()) to swap the ownership:
309 * this BT component becomes the only owner of the
312 bt_component_set_private_data(component, py_comp);
315 * Set this BT component pointer as py_comp._ptr, and
316 * take a reference on it (on success, at the end).
318 * This reference is put in the Python component
319 * object's __del__() method.
321 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component),
322 SWIGTYPE_p_bt_component, 0);
325 goto with_init_method_data_error;
328 ret = PyObject_SetAttrString(py_comp, "_ptr", py_comp_ptr);
331 goto with_init_method_data_error;
335 * _UserComponentType.__call__() puts the arguments and
336 * keyword arguments to use for the py_comp.__init__()
337 * call in py_comp._init_args and py_comp._init_kwargs.
339 * We need to get them and remove them from py_comp before
340 * calling py_comp.__init__().
342 py_init_args = PyObject_GetAttrString(py_comp, "_init_args");
345 goto with_init_method_data_error;
348 py_init_kwargs = PyObject_GetAttrString(py_comp, "_init_kwargs");
349 if (!py_init_kwargs) {
351 goto with_init_method_data_error;
354 ret = PyObject_DelAttrString(py_comp, "_init_args");
357 goto with_init_method_data_error;
360 ret = PyObject_DelAttrString(py_comp, "_init_kwargs");
363 goto with_init_method_data_error;
366 /* Ready to call py_comp.__init__() */
367 ret = bt_py3_call_self_init(py_comp, py_init_args,
371 * We do not clear any raised exception here
372 * so that the Python caller (the one who is
373 * instantiating the class) gets the exception
374 * from the __init__() method.
376 goto with_init_method_data_error;
380 * py_comp.__init__() went well: get the native BT
381 * component object reference for the Python object.
384 goto with_init_method_data_end;
386 with_init_method_data_error:
387 status = BT_COMPONENT_STATUS_ERROR;
390 * Reset py_comp._ptr to None to signal the error to
391 * _UserComponentType.__call__(). For exceptions raised
392 * from py_comp.__init__(), this is not important,
393 * because __call__() will also raise before even
394 * checking py_comp._ptr.
396 if (PyObject_HasAttrString(py_comp, "_ptr")) {
397 PyObject *py_err_type, *py_err_value, *py_err_traceback;
398 PyErr_Fetch(&py_err_type, &py_err_value,
401 PyObject_SetAttrString(py_comp, "_ptr", Py_None);
402 PyErr_Restore(py_err_type, py_err_value,
407 * Important: remove py_comp from our the BT component's
408 * private data. Since we're failing,
409 * bt_py3_cc_destroy() is about to be called: it must
410 * not consider py_comp. py_comp's __del__() method
411 * will be called (because the instance exists), but
412 * since py_comp._ptr is None, it won't do anything.
414 bt_component_set_private_data(component, NULL);
416 with_init_method_data_end:
417 Py_XDECREF(py_comp_ptr);
418 Py_XDECREF(py_init_args);
419 Py_XDECREF(py_init_kwargs);
422 * Not called from _UserComponentType.__call__().
424 * In this case we need to instantiate the user-defined
425 * Python class. To do this we call the user-defined
426 * class manually with this already-created BT component
427 * object pointer as the __comp_ptr keyword argument.
429 * We keep a reference, the only existing one, to the
430 * created Python object. bt_py3_component_on_del() will
431 * never be called by the object's __del__() method
432 * because _UserComponentType.__call__() marks the
433 * object as owned by the native BT component object.
435 PyObject *py_cls = NULL;
436 PyObject *py_comp = NULL;
437 PyObject *py_params = NULL;
438 PyObject *py_comp_ptr = NULL;
439 PyObject *py_cls_ctor_args = NULL;
440 PyObject *py_cls_ctor_kwargs = NULL;
443 * Get the user-defined Python class which created this
444 * component's class in the first place (borrowed
447 py_cls = lookup_cc_ptr_to_py_cls(bt_component_get_class(component));
449 goto without_init_method_data_error;
452 /* BT value object -> Python bt2.values object */
453 py_params = bt_py3_bt_value_from_ptr(params);
455 goto without_init_method_data_error;
458 /* Component pointer -> SWIG pointer Python object */
459 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component),
460 SWIGTYPE_p_bt_component, 0);
462 goto without_init_method_data_error;
466 * Do the equivalent of this:
468 * py_comp = py_cls(__comp_ptr=py_comp_ptr, params=py_params)
470 * _UserComponentType.__call__() calls the Python
471 * component object's __init__() function itself in this
474 py_cls_ctor_args = PyTuple_New(0);
475 if (!py_cls_ctor_args) {
476 goto without_init_method_data_error;
479 py_cls_ctor_kwargs = PyDict_New();
480 if (!py_cls_ctor_kwargs) {
481 goto without_init_method_data_error;
484 ret = PyDict_SetItemString(py_cls_ctor_kwargs, "__comp_ptr",
487 goto without_init_method_data_error;
490 ret = PyDict_SetItemString(py_cls_ctor_kwargs, "params",
493 goto without_init_method_data_error;
496 py_comp = PyObject_Call(py_cls, py_cls_ctor_args,
499 goto without_init_method_data_error;
503 * Our user Python component object is now fully created
504 * and initialized by the user. Since we just created
505 * it, this BT component is its only (persistent) owner.
507 bt_component_set_private_data(component, py_comp);
509 goto without_init_method_data_end;
511 without_init_method_data_error:
512 status = BT_COMPONENT_STATUS_ERROR;
515 * Clear any exception: we're returning a bad status
516 * anyway. If this call originated from Python (creation
517 * from a plugin's component class, for example), then
518 * the user gets an appropriate creation error.
522 without_init_method_data_end:
524 Py_XDECREF(py_params);
525 Py_XDECREF(py_comp_ptr);
526 Py_XDECREF(py_cls_ctor_args);
527 Py_XDECREF(py_cls_ctor_kwargs);
533 static void bt_py3_cc_destroy(struct bt_component *component)
535 PyObject *py_comp = bt_component_get_private_data(component);
536 PyObject *py_destroy_method_result = NULL;
540 * If we're here, this BT component is the only owner of
541 * its private user Python object. It is safe to
542 * decrement its reference count, and thus destroy it.
544 * We call its _destroy() method before doing so to notify
547 py_destroy_method_result = PyObject_CallMethod(py_comp,
551 * Ignore any exception raised by the _destroy() method
552 * because it won't change anything at this point: the
553 * component is being destroyed anyway.
556 Py_XDECREF(py_destroy_method_result);
561 static enum bt_notification_iterator_status bt_py3_exc_to_notif_iter_status(void)
563 enum bt_notification_iterator_status status =
564 BT_NOTIFICATION_ITERATOR_STATUS_OK;
565 PyObject *exc = PyErr_Occurred();
571 if (PyErr_GivenExceptionMatches(exc,
572 py_mod_bt2_exc_unsupported_feature_type)) {
573 status = BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED;
574 } else if (PyErr_GivenExceptionMatches(exc,
575 py_mod_bt2_exc_stop_type)) {
576 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
578 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
586 static enum bt_notification_iterator_status bt_py3_cc_notification_iterator_init(
587 struct bt_component *component,
588 struct bt_notification_iterator *iter, void *init_method_data)
590 enum bt_notification_iterator_status status =
591 BT_NOTIFICATION_ITERATOR_STATUS_OK;
592 PyObject *py_comp_cls = NULL;
593 PyObject *py_iter_cls = NULL;
594 PyObject *py_iter_ptr = NULL;
595 PyObject *py_init_method_result = NULL;
596 PyObject *py_iter = NULL;
597 PyObject *py_comp = bt_component_get_private_data(component);
599 /* Find user's Python notification iterator class */
600 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
605 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
611 * Create object with borrowed BT notification iterator
614 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
616 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(iter),
617 SWIGTYPE_p_bt_notification_iterator, 0);
622 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
623 "(OO)", py_iter_cls, py_iter_ptr);
633 * At this point, py_iter._ptr is set, so this initialization
634 * function has access to the py_iter.component property (which
635 * gives it the user Python component object from which the
636 * iterator was created).
638 py_init_method_result = PyObject_CallMethod(py_iter, "__init__", NULL);
639 if (!py_init_method_result) {
644 * Since the Python code can never instantiate a user-defined
645 * notification iterator class, the BT notification iterator
646 * object does NOT belong to a user Python notification iterator
647 * object (borrowed reference). However this Python object is
648 * owned by this BT notification iterator object.
650 * In the Python world, the lifetime of the BT notification
651 * iterator is managed by a _GenericNotificationIterator
654 * _GenericNotificationIterator instance:
655 * owns a native bt_notification_iterator object (iter)
656 * owns a UserNotificationIterator instance (py_iter)
657 * self._ptr is a borrowed reference to the
658 * native bt_notification_iterator object (iter)
660 bt_notification_iterator_set_private_data(iter, py_iter);
665 status = bt_py3_exc_to_notif_iter_status();
666 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
668 * Looks like there wasn't any exception from the Python
669 * side, but we're still in an error state here.
671 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
675 * Clear any exception: we're returning a bad status anyway. If
676 * this call originated from Python, then the user gets an
677 * appropriate creation error.
682 Py_XDECREF(py_comp_cls);
683 Py_XDECREF(py_iter_cls);
684 Py_XDECREF(py_iter_ptr);
685 Py_XDECREF(py_init_method_result);
690 static void bt_py3_cc_notification_iterator_destroy(
691 struct bt_notification_iterator *iter)
693 PyObject *py_notif_iter =
694 bt_notification_iterator_get_private_data(iter);
695 PyObject *py_destroy_method_result = NULL;
697 assert(py_notif_iter);
698 py_destroy_method_result = PyObject_CallMethod(py_notif_iter,
702 * Ignore any exception raised by the _destroy() method because
703 * it won't change anything at this point. The notification
704 * iterator is being destroyed anyway.
707 Py_XDECREF(py_destroy_method_result);
708 Py_DECREF(py_notif_iter);
711 static struct bt_notification *bt_py3_cc_notification_iterator_get(
712 struct bt_notification_iterator *iter)
714 PyObject *py_notif_iter =
715 bt_notification_iterator_get_private_data(iter);
716 PyObject *py_get_method_result = NULL;
717 struct bt_notification *notif = NULL;
719 assert(py_notif_iter);
720 py_get_method_result = PyObject_CallMethod(py_notif_iter,
721 "_get_from_bt", NULL);
722 if (!py_get_method_result) {
727 * The returned object, on success, is an integer object
728 * (PyLong) containing the address of a BT notification
729 * object (which is not ours).
731 notif = (struct bt_notification *) PyLong_AsUnsignedLongLong(
732 py_get_method_result);
734 /* Clear potential overflow error; should never happen */
735 if (PyErr_Occurred()) {
749 * Clear any exception: we're returning NULL anyway. If this
750 * call originated from Python, then the user gets an
756 Py_XDECREF(py_get_method_result);
760 static enum bt_notification_iterator_status bt_py3_cc_notification_iterator_next(
761 struct bt_notification_iterator *iter)
763 enum bt_notification_iterator_status status;
764 PyObject *py_notif_iter =
765 bt_notification_iterator_get_private_data(iter);
766 PyObject *py_next_method_result = NULL;
768 assert(py_notif_iter);
769 py_next_method_result = PyObject_CallMethod(py_notif_iter,
771 status = bt_py3_exc_to_notif_iter_status();
772 if (!py_next_method_result
773 && status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
774 /* Pretty sure this should never happen, but just in case */
775 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
778 Py_XDECREF(py_next_method_result);
782 static enum bt_notification_iterator_status bt_py3_cc_notification_iterator_seek_time(
783 struct bt_notification_iterator *iter, int64_t time)
785 enum bt_notification_iterator_status status;
786 PyObject *py_notif_iter =
787 bt_notification_iterator_get_private_data(iter);
788 PyObject *py_seek_to_time_method_result = NULL;
790 assert(py_notif_iter);
791 py_seek_to_time_method_result = PyObject_CallMethod(py_notif_iter,
792 "_seek_to_time", "(L)", time);
793 status = bt_py3_exc_to_notif_iter_status();
794 if (!py_seek_to_time_method_result
795 && status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
796 /* Pretty sure this should never happen, but just in case */
797 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
800 Py_XDECREF(py_seek_to_time_method_result);
804 static enum bt_component_status bt_py3_exc_to_component_status(void)
806 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
807 PyObject *exc = PyErr_Occurred();
813 if (PyErr_GivenExceptionMatches(exc,
814 py_mod_bt2_exc_unsupported_feature_type)) {
815 status = BT_COMPONENT_STATUS_UNSUPPORTED;
816 } else if (PyErr_GivenExceptionMatches(exc,
817 py_mod_bt2_exc_try_again_type)) {
818 status = BT_COMPONENT_STATUS_AGAIN;
819 } else if (PyErr_GivenExceptionMatches(exc,
820 py_mod_bt2_exc_stop_type)) {
821 status = BT_COMPONENT_STATUS_END;
823 status = BT_COMPONENT_STATUS_ERROR;
831 static enum bt_component_status bt_py3_cc_sink_consume(
832 struct bt_component *component)
834 PyObject *py_comp = bt_component_get_private_data(component);
835 PyObject *py_consume_method_result = NULL;
836 enum bt_component_status status;
838 py_consume_method_result = PyObject_CallMethod(py_comp,
840 status = bt_py3_exc_to_component_status();
841 if (!py_consume_method_result && status == BT_COMPONENT_STATUS_OK) {
842 /* Pretty sure this should never happen, but just in case */
843 status = BT_COMPONENT_STATUS_ERROR;
846 Py_XDECREF(py_consume_method_result);
850 static enum bt_component_status bt_py3_cc_sink_filter_add_iterator(
851 struct bt_component *component,
852 struct bt_notification_iterator *iter)
854 PyObject *py_comp = bt_component_get_private_data(component);
855 PyObject *py_add_iterator_method_result = NULL;
856 PyObject *py_notif_iter_ptr = NULL;
857 enum bt_component_status status;
859 py_notif_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(iter),
860 SWIGTYPE_p_bt_notification_iterator, 0);
861 if (!py_notif_iter_ptr) {
863 status = BT_COMPONENT_STATUS_ERROR;
868 py_add_iterator_method_result = PyObject_CallMethod(py_comp,
869 "_add_iterator_from_bt", "(O)", py_notif_iter_ptr);
870 status = bt_py3_exc_to_component_status();
871 if (!py_add_iterator_method_result
872 && status == BT_COMPONENT_STATUS_OK) {
873 /* Pretty sure this should never happen, but just in case */
874 status = BT_COMPONENT_STATUS_ERROR;
878 Py_XDECREF(py_notif_iter_ptr);
879 Py_XDECREF(py_add_iterator_method_result);
884 /* Component class creation functions (called from Python module) */
886 static int bt_py3_cc_set_optional_attrs_methods(struct bt_component_class *cc,
887 const char *description)
892 ret = bt_component_class_set_description(cc, description);
898 ret = bt_component_class_set_init_method(cc, bt_py3_cc_init);
903 ret = bt_component_class_set_destroy_method(cc, bt_py3_cc_destroy);
912 static int bt_py3_cc_set_optional_iter_methods(struct bt_component_class *cc,
914 int (*set_notif_iter_init_method)(struct bt_component_class *, bt_component_class_notification_iterator_init_method),
915 int (*set_notif_iter_destroy_method)(struct bt_component_class *, bt_component_class_notification_iterator_destroy_method),
916 int (*set_notif_iter_seek_time_method)(struct bt_component_class *, bt_component_class_notification_iterator_seek_time_method))
920 ret = set_notif_iter_init_method(
921 cc, bt_py3_cc_notification_iterator_init);
926 ret = set_notif_iter_destroy_method(
927 cc, bt_py3_cc_notification_iterator_destroy);
933 ret = set_notif_iter_seek_time_method(
934 cc, bt_py3_cc_notification_iterator_seek_time);
944 static struct bt_component_class *bt_py3_component_class_source_create(
945 PyObject *py_cls, const char *name, const char *description,
948 struct bt_component_class *cc;
952 cc = bt_component_class_source_create(name,
953 bt_py3_cc_notification_iterator_get,
954 bt_py3_cc_notification_iterator_next);
959 ret = bt_py3_cc_set_optional_attrs_methods(cc, description);
965 ret = bt_py3_cc_set_optional_iter_methods(cc, has_seek_time,
966 bt_component_class_source_set_notification_iterator_init_method,
967 bt_component_class_source_set_notification_iterator_destroy_method,
968 bt_component_class_source_set_notification_iterator_seek_time_method);
974 register_cc_ptr_to_py_cls(cc, py_cls);
975 bt_component_class_freeze(cc);
981 static struct bt_component_class *bt_py3_component_class_filter_create(
982 PyObject *py_cls, const char *name, const char *description,
985 struct bt_component_class *cc;
989 cc = bt_component_class_filter_create(name,
990 bt_py3_cc_notification_iterator_get,
991 bt_py3_cc_notification_iterator_next);
996 ret = bt_py3_cc_set_optional_attrs_methods(cc, description);
1002 ret = bt_py3_cc_set_optional_iter_methods(cc, has_seek_time,
1003 bt_component_class_filter_set_notification_iterator_init_method,
1004 bt_component_class_filter_set_notification_iterator_destroy_method,
1005 bt_component_class_filter_set_notification_iterator_seek_time_method);
1011 ret = bt_component_class_filter_set_add_iterator_method(cc,
1012 bt_py3_cc_sink_filter_add_iterator);
1018 register_cc_ptr_to_py_cls(cc, py_cls);
1019 bt_component_class_freeze(cc);
1025 static struct bt_component_class *bt_py3_component_class_sink_create(
1026 PyObject *py_cls, const char *name, const char *description)
1028 struct bt_component_class *cc;
1032 cc = bt_component_class_sink_create(name, bt_py3_cc_sink_consume);
1037 ret = bt_py3_cc_set_optional_attrs_methods(cc, description);
1043 ret = bt_component_class_sink_set_add_iterator_method(cc,
1044 bt_py3_cc_sink_filter_add_iterator);
1050 register_cc_ptr_to_py_cls(cc, py_cls);
1051 bt_component_class_freeze(cc);
1058 /* Component creation function (called from Python module) */
1060 static void bt_py3_component_create(
1061 struct bt_component_class *comp_class, PyObject *py_self,
1064 struct bt_component *component =
1065 bt_component_create_with_init_method_data(comp_class,
1066 name, NULL, py_self);
1069 * Our component initialization function, bt_py3_cc_init(), sets
1070 * a new reference to the created component in the user's Python
1071 * object (py_self._ptr). This is where the reference is kept.
1072 * We don't need the returned component in this case (if any,
1073 * because it might be NULL if the creation failed, most
1074 * probably because py_self.__init__() raised something).
1080 /* Component delete notification */
1082 static void bt_py3_component_on_del(PyObject *py_comp)
1085 * The Python component's __del__() function calls this to
1086 * indicate that there's no more reference on it from the
1089 * Since the BT component can continue to live once the Python
1090 * component object is deleted, we increment the Python
1091 * component's reference count so that it now only belong to the
1092 * BT component. We will decrement this reference count once
1093 * the BT component is destroyed in bt_py3_cc_destroy().
1100 %exception bt_py3_component_create {
1102 if (PyErr_Occurred()) {
1103 /* Exception is already set by bt_py3_component_create() */
1108 struct bt_component_class *bt_py3_component_class_source_create(
1109 PyObject *py_cls, const char *name, const char *description,
1110 bool has_seek_time);
1111 struct bt_component_class *bt_py3_component_class_filter_create(
1112 PyObject *py_cls, const char *name, const char *description,
1113 bool has_seek_time);
1114 struct bt_component_class *bt_py3_component_class_sink_create(
1115 PyObject *py_cls, const char *name, const char *description);
1116 void bt_py3_component_create(
1117 struct bt_component_class *comp_class, PyObject *py_self,
1119 void bt_py3_component_on_del(PyObject *py_comp);
1120 void bt_py3_cc_init_from_bt2(void);
1121 void bt_py3_cc_exit_handler(void);