| 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | %include <babeltrace2/graph/component-class-const.h> |
| 26 | %include <babeltrace2/graph/component-class-source-const.h> |
| 27 | %include <babeltrace2/graph/component-class-source.h> |
| 28 | %include <babeltrace2/graph/component-class-filter-const.h> |
| 29 | %include <babeltrace2/graph/component-class-filter.h> |
| 30 | %include <babeltrace2/graph/component-class-sink-const.h> |
| 31 | %include <babeltrace2/graph/component-class-sink.h> |
| 32 | %include <babeltrace2/graph/self-component-class-source.h> |
| 33 | %include <babeltrace2/graph/self-component-class-filter.h> |
| 34 | %include <babeltrace2/graph/self-component-class-sink.h> |
| 35 | |
| 36 | %{ |
| 37 | /* |
| 38 | * This hash table associates a BT component class object address to a |
| 39 | * user-defined Python class (PyObject *). The keys and values are NOT |
| 40 | * owned by this hash table. The Python class objects are owned by the |
| 41 | * Python module, which should not be unloaded until it is not possible |
| 42 | * to create a user Python component anyway. |
| 43 | * |
| 44 | * This hash table is written to when a user-defined Python component |
| 45 | * class is created by one of the bt_py3_component_class_*_create() |
| 46 | * functions. |
| 47 | * |
| 48 | * This function is read from when a user calls bt_component_create() |
| 49 | * with a component class pointer created by one of the functions above. |
| 50 | * In this case, the original Python class needs to be found to |
| 51 | * instantiate it and associate the created Python component object with |
| 52 | * a BT component object instance. |
| 53 | */ |
| 54 | |
| 55 | static GHashTable *bt_cc_ptr_to_py_cls; |
| 56 | |
| 57 | static |
| 58 | void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc, |
| 59 | PyObject *py_cls) |
| 60 | { |
| 61 | if (!bt_cc_ptr_to_py_cls) { |
| 62 | /* |
| 63 | * Lazy-initializing this GHashTable because GLib |
| 64 | * might not be initialized yet and it needs to be |
| 65 | * before we call g_hash_table_new() |
| 66 | */ |
| 67 | BT_LOGD_STR("Creating native component class to Python component class hash table."); |
| 68 | bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 69 | BT_ASSERT(bt_cc_ptr_to_py_cls); |
| 70 | } |
| 71 | |
| 72 | g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc, |
| 73 | (gpointer) py_cls); |
| 74 | } |
| 75 | |
| 76 | static |
| 77 | PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc) |
| 78 | { |
| 79 | if (!bt_cc_ptr_to_py_cls) { |
| 80 | BT_LOGW("Cannot look up Python component class because hash table is NULL: " |
| 81 | "comp-cls-addr=%p", bt_cc); |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls, |
| 86 | (gconstpointer) bt_cc); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /* |
| 91 | * Useful Python objects. |
| 92 | */ |
| 93 | |
| 94 | static PyObject *py_mod_bt2 = NULL; |
| 95 | static PyObject *py_mod_bt2_exc_error_type = NULL; |
| 96 | static PyObject *py_mod_bt2_exc_try_again_type = NULL; |
| 97 | static PyObject *py_mod_bt2_exc_stop_type = NULL; |
| 98 | static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL; |
| 99 | static PyObject *py_mod_bt2_exc_invalid_query_object_type = NULL; |
| 100 | static PyObject *py_mod_bt2_exc_invalid_query_params_type = NULL; |
| 101 | |
| 102 | static |
| 103 | void bt_py3_cc_init_from_bt2(void) |
| 104 | { |
| 105 | /* |
| 106 | * This is called once the bt2 package is loaded. |
| 107 | * |
| 108 | * Those modules and functions are needed while the package is |
| 109 | * used. Loading them here is safe because we know the bt2 |
| 110 | * package is imported, and we know that the user cannot use the |
| 111 | * code here without importing bt2 first. |
| 112 | */ |
| 113 | py_mod_bt2 = PyImport_ImportModule("bt2"); |
| 114 | BT_ASSERT(py_mod_bt2); |
| 115 | py_mod_bt2_exc_error_type = |
| 116 | PyObject_GetAttrString(py_mod_bt2, "Error"); |
| 117 | BT_ASSERT(py_mod_bt2_exc_error_type); |
| 118 | py_mod_bt2_exc_try_again_type = |
| 119 | PyObject_GetAttrString(py_mod_bt2, "TryAgain"); |
| 120 | BT_ASSERT(py_mod_bt2_exc_try_again_type); |
| 121 | py_mod_bt2_exc_stop_type = |
| 122 | PyObject_GetAttrString(py_mod_bt2, "Stop"); |
| 123 | BT_ASSERT(py_mod_bt2_exc_stop_type); |
| 124 | py_mod_bt2_exc_invalid_query_object_type = |
| 125 | PyObject_GetAttrString(py_mod_bt2, "InvalidQueryObject"); |
| 126 | BT_ASSERT(py_mod_bt2_exc_invalid_query_object_type); |
| 127 | py_mod_bt2_exc_invalid_query_params_type = |
| 128 | PyObject_GetAttrString(py_mod_bt2, "InvalidQueryParams"); |
| 129 | BT_ASSERT(py_mod_bt2_exc_invalid_query_params_type); |
| 130 | } |
| 131 | |
| 132 | static |
| 133 | void bt_py3_cc_exit_handler(void) |
| 134 | { |
| 135 | /* |
| 136 | * This is an exit handler (set by the bt2 package). |
| 137 | * |
| 138 | * We only give back the references that we took in |
| 139 | * bt_py3_cc_init_from_bt2() here. The global variables continue |
| 140 | * to exist for the code of this file, but they are now borrowed |
| 141 | * references. If this code is executed, it means that somehow |
| 142 | * the modules are still loaded, so it should be safe to use |
| 143 | * them even without a strong reference. |
| 144 | * |
| 145 | * We cannot do this in the library's destructor because it |
| 146 | * gets executed once Python is already finalized. |
| 147 | */ |
| 148 | Py_XDECREF(py_mod_bt2); |
| 149 | Py_XDECREF(py_mod_bt2_exc_error_type); |
| 150 | Py_XDECREF(py_mod_bt2_exc_try_again_type); |
| 151 | Py_XDECREF(py_mod_bt2_exc_stop_type); |
| 152 | Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type); |
| 153 | Py_XDECREF(py_mod_bt2_exc_invalid_query_object_type); |
| 154 | Py_XDECREF(py_mod_bt2_exc_invalid_query_params_type); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /* Library destructor */ |
| 159 | |
| 160 | __attribute__((destructor)) |
| 161 | static |
| 162 | void bt_py3_native_comp_class_dtor(void) { |
| 163 | /* Destroy component class association hash table */ |
| 164 | if (bt_cc_ptr_to_py_cls) { |
| 165 | BT_LOGD_STR("Destroying native component class to Python component class hash table."); |
| 166 | g_hash_table_destroy(bt_cc_ptr_to_py_cls); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static |
| 171 | void bt2_py_loge_exception(void) |
| 172 | { |
| 173 | GString *gstr; |
| 174 | |
| 175 | BT_ASSERT(PyErr_Occurred() != NULL); |
| 176 | gstr = bt_py_common_format_exception(BT_LOG_OUTPUT_LEVEL); |
| 177 | if (!gstr) { |
| 178 | /* bt_py_common_format_exception() logs errors */ |
| 179 | goto end; |
| 180 | } |
| 181 | |
| 182 | BT_LOGE_STR(gstr->str); |
| 183 | |
| 184 | end: |
| 185 | if (gstr) { |
| 186 | g_string_free(gstr, TRUE); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static |
| 191 | bt_self_component_status bt_py3_exc_to_self_component_status(void) |
| 192 | { |
| 193 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
| 194 | PyObject *exc = PyErr_Occurred(); |
| 195 | |
| 196 | if (!exc) { |
| 197 | goto end; |
| 198 | } |
| 199 | |
| 200 | if (PyErr_GivenExceptionMatches(exc, |
| 201 | py_mod_bt2_exc_try_again_type)) { |
| 202 | status = BT_SELF_COMPONENT_STATUS_AGAIN; |
| 203 | } else if (PyErr_GivenExceptionMatches(exc, |
| 204 | py_mod_bt2_exc_stop_type)) { |
| 205 | status = BT_SELF_COMPONENT_STATUS_END; |
| 206 | } else { |
| 207 | bt2_py_loge_exception(); |
| 208 | status = BT_SELF_COMPONENT_STATUS_ERROR; |
| 209 | } |
| 210 | |
| 211 | end: |
| 212 | PyErr_Clear(); |
| 213 | return status; |
| 214 | } |
| 215 | |
| 216 | /* Component class proxy methods (delegate to the attached Python object) */ |
| 217 | |
| 218 | static |
| 219 | bt_self_message_iterator_status bt_py3_exc_to_self_message_iterator_status(void) |
| 220 | { |
| 221 | enum bt_self_message_iterator_status status = |
| 222 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 223 | PyObject *exc = PyErr_Occurred(); |
| 224 | |
| 225 | if (!exc) { |
| 226 | goto end; |
| 227 | } |
| 228 | |
| 229 | if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_stop_type)) { |
| 230 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_END; |
| 231 | } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_try_again_type)) { |
| 232 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN; |
| 233 | } else { |
| 234 | bt2_py_loge_exception(); |
| 235 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
| 236 | } |
| 237 | |
| 238 | end: |
| 239 | PyErr_Clear(); |
| 240 | return status; |
| 241 | } |
| 242 | |
| 243 | static |
| 244 | enum bt_query_status bt_py3_exc_to_query_status(void) |
| 245 | { |
| 246 | enum bt_query_status status = BT_QUERY_STATUS_OK; |
| 247 | PyObject *exc = PyErr_Occurred(); |
| 248 | |
| 249 | if (!exc) { |
| 250 | goto end; |
| 251 | } |
| 252 | |
| 253 | if (PyErr_GivenExceptionMatches(exc, |
| 254 | py_mod_bt2_exc_invalid_query_object_type)) { |
| 255 | status = BT_QUERY_STATUS_INVALID_OBJECT; |
| 256 | } else if (PyErr_GivenExceptionMatches(exc, |
| 257 | py_mod_bt2_exc_invalid_query_params_type)) { |
| 258 | status = BT_QUERY_STATUS_INVALID_PARAMS; |
| 259 | } else if (PyErr_GivenExceptionMatches(exc, |
| 260 | py_mod_bt2_exc_try_again_type)) { |
| 261 | status = BT_QUERY_STATUS_AGAIN; |
| 262 | } else { |
| 263 | bt2_py_loge_exception(); |
| 264 | status = BT_QUERY_STATUS_ERROR; |
| 265 | } |
| 266 | |
| 267 | end: |
| 268 | PyErr_Clear(); |
| 269 | return status; |
| 270 | } |
| 271 | |
| 272 | static |
| 273 | bt_self_component_status bt_py3_component_class_init( |
| 274 | bt_self_component *self_component, |
| 275 | void *self_component_v, |
| 276 | swig_type_info *self_comp_cls_type_swig_type, |
| 277 | const bt_value *params, |
| 278 | void *init_method_data) |
| 279 | { |
| 280 | const bt_component *component = bt_self_component_as_component(self_component); |
| 281 | const bt_component_class *component_class = bt_component_borrow_class_const(component); |
| 282 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
| 283 | PyObject *py_cls = NULL; |
| 284 | PyObject *py_comp = NULL; |
| 285 | PyObject *py_params_ptr = NULL; |
| 286 | PyObject *py_comp_ptr = NULL; |
| 287 | |
| 288 | (void) init_method_data; |
| 289 | |
| 290 | BT_ASSERT(self_component); |
| 291 | BT_ASSERT(self_component_v); |
| 292 | BT_ASSERT(self_comp_cls_type_swig_type); |
| 293 | |
| 294 | /* |
| 295 | * Get the user-defined Python class which created this |
| 296 | * component's class in the first place (borrowed |
| 297 | * reference). |
| 298 | */ |
| 299 | py_cls = lookup_cc_ptr_to_py_cls(component_class); |
| 300 | if (!py_cls) { |
| 301 | BT_LOGE("Cannot find Python class associated to native component class: " |
| 302 | "comp-cls-addr=%p", component_class); |
| 303 | goto error; |
| 304 | } |
| 305 | |
| 306 | /* Parameters pointer -> SWIG pointer Python object */ |
| 307 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), |
| 308 | SWIGTYPE_p_bt_value, 0); |
| 309 | if (!py_params_ptr) { |
| 310 | BT_LOGE_STR("Failed to create a SWIG pointer object."); |
| 311 | goto error; |
| 312 | } |
| 313 | |
| 314 | py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v), |
| 315 | self_comp_cls_type_swig_type, 0); |
| 316 | if (!py_comp_ptr) { |
| 317 | BT_LOGE_STR("Failed to create a SWIG pointer object."); |
| 318 | goto error; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Do the equivalent of this: |
| 323 | * |
| 324 | * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr) |
| 325 | * |
| 326 | * _UserComponentType._init_from_native() calls the Python |
| 327 | * component object's __init__() function. |
| 328 | */ |
| 329 | py_comp = PyObject_CallMethod(py_cls, |
| 330 | "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr); |
| 331 | if (!py_comp) { |
| 332 | bt2_py_loge_exception(); |
| 333 | BT_LOGE("Failed to call Python class's _init_from_native() method: " |
| 334 | "py-cls-addr=%p", py_cls); |
| 335 | |
| 336 | goto error; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Our user Python component object is now fully created and |
| 341 | * initialized by the user. Since we just created it, this |
| 342 | * native component is its only (persistent) owner. |
| 343 | */ |
| 344 | bt_self_component_set_data(self_component, py_comp); |
| 345 | py_comp = NULL; |
| 346 | goto end; |
| 347 | |
| 348 | error: |
| 349 | status = BT_SELF_COMPONENT_STATUS_ERROR; |
| 350 | |
| 351 | /* |
| 352 | * Clear any exception: we're returning a bad status anyway. If |
| 353 | * this call originated from Python (creation from a plugin's |
| 354 | * component class, for example), then the user gets an |
| 355 | * appropriate creation error. |
| 356 | */ |
| 357 | PyErr_Clear(); |
| 358 | |
| 359 | end: |
| 360 | Py_XDECREF(py_comp); |
| 361 | Py_XDECREF(py_params_ptr); |
| 362 | Py_XDECREF(py_comp_ptr); |
| 363 | return status; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Method of bt_component_class_source to initialize a bt_self_component_source |
| 368 | * of that class. |
| 369 | */ |
| 370 | |
| 371 | static |
| 372 | bt_self_component_status bt_py3_component_class_source_init( |
| 373 | bt_self_component_source *self_component_source, |
| 374 | const bt_value *params, void *init_method_data) |
| 375 | { |
| 376 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); |
| 377 | return bt_py3_component_class_init( |
| 378 | self_component, |
| 379 | self_component_source, |
| 380 | SWIGTYPE_p_bt_self_component_source, |
| 381 | params, init_method_data); |
| 382 | } |
| 383 | |
| 384 | static |
| 385 | bt_self_component_status bt_py3_component_class_filter_init( |
| 386 | bt_self_component_filter *self_component_filter, |
| 387 | const bt_value *params, void *init_method_data) |
| 388 | { |
| 389 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); |
| 390 | return bt_py3_component_class_init( |
| 391 | self_component, |
| 392 | self_component_filter, |
| 393 | SWIGTYPE_p_bt_self_component_filter, |
| 394 | params, init_method_data); |
| 395 | } |
| 396 | |
| 397 | static |
| 398 | bt_self_component_status bt_py3_component_class_sink_init( |
| 399 | bt_self_component_sink *self_component_sink, |
| 400 | const bt_value *params, void *init_method_data) |
| 401 | { |
| 402 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
| 403 | return bt_py3_component_class_init( |
| 404 | self_component, |
| 405 | self_component_sink, |
| 406 | SWIGTYPE_p_bt_self_component_sink, |
| 407 | params, init_method_data); |
| 408 | } |
| 409 | |
| 410 | static |
| 411 | void bt_py3_component_class_finalize(bt_self_component *self_component) |
| 412 | { |
| 413 | PyObject *py_comp = bt_self_component_get_data(self_component); |
| 414 | BT_ASSERT(py_comp); |
| 415 | |
| 416 | /* Call user's _finalize() method */ |
| 417 | PyObject *py_method_result = PyObject_CallMethod(py_comp, |
| 418 | "_finalize", NULL); |
| 419 | |
| 420 | if (PyErr_Occurred()) { |
| 421 | BT_LOGW("User's _finalize() method raised an exception: ignoring."); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Ignore any exception raised by the _finalize() method because |
| 426 | * it won't change anything at this point: the component is |
| 427 | * being destroyed anyway. |
| 428 | */ |
| 429 | PyErr_Clear(); |
| 430 | Py_XDECREF(py_method_result); |
| 431 | Py_DECREF(py_comp); |
| 432 | } |
| 433 | |
| 434 | static |
| 435 | void bt_py3_component_class_source_finalize(bt_self_component_source *self_component_source) |
| 436 | { |
| 437 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); |
| 438 | bt_py3_component_class_finalize(self_component); |
| 439 | } |
| 440 | |
| 441 | static |
| 442 | void bt_py3_component_class_filter_finalize(bt_self_component_filter *self_component_filter) |
| 443 | { |
| 444 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); |
| 445 | bt_py3_component_class_finalize(self_component); |
| 446 | } |
| 447 | |
| 448 | static |
| 449 | void bt_py3_component_class_sink_finalize(bt_self_component_sink *self_component_sink) |
| 450 | { |
| 451 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
| 452 | bt_py3_component_class_finalize(self_component); |
| 453 | } |
| 454 | |
| 455 | static |
| 456 | bt_bool bt_py3_component_class_can_seek_beginning( |
| 457 | bt_self_message_iterator *self_message_iterator) |
| 458 | { |
| 459 | PyObject *py_iter; |
| 460 | PyObject *py_result = NULL; |
| 461 | bt_bool can_seek_beginning = false; |
| 462 | |
| 463 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); |
| 464 | BT_ASSERT(py_iter); |
| 465 | |
| 466 | py_result = PyObject_GetAttrString(py_iter, "_can_seek_beginning_from_native"); |
| 467 | |
| 468 | BT_ASSERT(!py_result || PyBool_Check(py_result)); |
| 469 | |
| 470 | if (py_result) { |
| 471 | can_seek_beginning = PyObject_IsTrue(py_result); |
| 472 | } else { |
| 473 | /* |
| 474 | * Once can_seek_beginning can report errors, convert the |
| 475 | * exception to a status. For now, log and return false; |
| 476 | */ |
| 477 | bt2_py_loge_exception(); |
| 478 | PyErr_Clear(); |
| 479 | } |
| 480 | |
| 481 | Py_XDECREF(py_result); |
| 482 | |
| 483 | return can_seek_beginning; |
| 484 | } |
| 485 | |
| 486 | static |
| 487 | bt_self_message_iterator_status bt_py3_component_class_seek_beginning( |
| 488 | bt_self_message_iterator *self_message_iterator) |
| 489 | { |
| 490 | PyObject *py_iter; |
| 491 | PyObject *py_result; |
| 492 | bt_self_message_iterator_status status; |
| 493 | |
| 494 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); |
| 495 | BT_ASSERT(py_iter); |
| 496 | |
| 497 | py_result = PyObject_CallMethod(py_iter, "_seek_beginning_from_native", NULL); |
| 498 | |
| 499 | BT_ASSERT(!py_result || py_result == Py_None); |
| 500 | status = bt_py3_exc_to_self_message_iterator_status(); |
| 501 | |
| 502 | Py_XDECREF(py_result); |
| 503 | |
| 504 | return status; |
| 505 | } |
| 506 | |
| 507 | static |
| 508 | bt_self_component_status bt_py3_component_class_port_connected( |
| 509 | bt_self_component *self_component, |
| 510 | void *self_component_port, |
| 511 | swig_type_info *self_component_port_swig_type, |
| 512 | bt_port_type self_component_port_type, |
| 513 | const void *other_port, |
| 514 | swig_type_info *other_port_swig_type) |
| 515 | { |
| 516 | bt_self_component_status status; |
| 517 | PyObject *py_comp = NULL; |
| 518 | PyObject *py_self_port_ptr = NULL; |
| 519 | PyObject *py_other_port_ptr = NULL; |
| 520 | PyObject *py_method_result = NULL; |
| 521 | |
| 522 | py_comp = bt_self_component_get_data(self_component); |
| 523 | BT_ASSERT(py_comp); |
| 524 | |
| 525 | py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port), |
| 526 | self_component_port_swig_type, 0); |
| 527 | if (!py_self_port_ptr) { |
| 528 | BT_LOGF_STR("Failed to create a SWIG pointer object."); |
| 529 | status = BT_SELF_COMPONENT_STATUS_NOMEM; |
| 530 | goto end; |
| 531 | } |
| 532 | |
| 533 | py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port), |
| 534 | other_port_swig_type, 0); |
| 535 | if (!py_other_port_ptr) { |
| 536 | BT_LOGF_STR("Failed to create a SWIG pointer object."); |
| 537 | status = BT_SELF_COMPONENT_STATUS_NOMEM; |
| 538 | goto end; } |
| 539 | |
| 540 | py_method_result = PyObject_CallMethod(py_comp, |
| 541 | "_port_connected_from_native", "(OiO)", py_self_port_ptr, |
| 542 | self_component_port_type, py_other_port_ptr); |
| 543 | |
| 544 | BT_ASSERT(!py_method_result || py_method_result == Py_None); |
| 545 | |
| 546 | status = bt_py3_exc_to_self_component_status(); |
| 547 | |
| 548 | end: |
| 549 | Py_XDECREF(py_self_port_ptr); |
| 550 | Py_XDECREF(py_other_port_ptr); |
| 551 | Py_XDECREF(py_method_result); |
| 552 | |
| 553 | return status; |
| 554 | } |
| 555 | |
| 556 | static |
| 557 | bt_self_component_status bt_py3_component_class_source_output_port_connected( |
| 558 | bt_self_component_source *self_component_source, |
| 559 | bt_self_component_port_output *self_component_port_output, |
| 560 | const bt_port_input *other_port_input) |
| 561 | { |
| 562 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); |
| 563 | |
| 564 | return bt_py3_component_class_port_connected( |
| 565 | self_component, |
| 566 | self_component_port_output, |
| 567 | SWIGTYPE_p_bt_self_component_port_output, |
| 568 | BT_PORT_TYPE_OUTPUT, |
| 569 | other_port_input, |
| 570 | SWIGTYPE_p_bt_port_input); |
| 571 | } |
| 572 | |
| 573 | static |
| 574 | bt_self_component_status bt_py3_component_class_filter_input_port_connected( |
| 575 | bt_self_component_filter *self_component_filter, |
| 576 | bt_self_component_port_input *self_component_port_input, |
| 577 | const bt_port_output *other_port_output) |
| 578 | { |
| 579 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); |
| 580 | |
| 581 | return bt_py3_component_class_port_connected( |
| 582 | self_component, |
| 583 | self_component_port_input, |
| 584 | SWIGTYPE_p_bt_self_component_port_input, |
| 585 | BT_PORT_TYPE_INPUT, |
| 586 | other_port_output, |
| 587 | SWIGTYPE_p_bt_port_output); |
| 588 | } |
| 589 | |
| 590 | static |
| 591 | bt_self_component_status bt_py3_component_class_filter_output_port_connected( |
| 592 | bt_self_component_filter *self_component_filter, |
| 593 | bt_self_component_port_output *self_component_port_output, |
| 594 | const bt_port_input *other_port_input) |
| 595 | { |
| 596 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); |
| 597 | |
| 598 | return bt_py3_component_class_port_connected( |
| 599 | self_component, |
| 600 | self_component_port_output, |
| 601 | SWIGTYPE_p_bt_self_component_port_output, |
| 602 | BT_PORT_TYPE_OUTPUT, |
| 603 | other_port_input, |
| 604 | SWIGTYPE_p_bt_port_input); |
| 605 | } |
| 606 | |
| 607 | static |
| 608 | bt_self_component_status bt_py3_component_class_sink_input_port_connected( |
| 609 | bt_self_component_sink *self_component_sink, |
| 610 | bt_self_component_port_input *self_component_port_input, |
| 611 | const bt_port_output *other_port_output) |
| 612 | { |
| 613 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
| 614 | |
| 615 | return bt_py3_component_class_port_connected( |
| 616 | self_component, |
| 617 | self_component_port_input, |
| 618 | SWIGTYPE_p_bt_self_component_port_input, |
| 619 | BT_PORT_TYPE_INPUT, |
| 620 | other_port_output, |
| 621 | SWIGTYPE_p_bt_port_output); |
| 622 | } |
| 623 | |
| 624 | static |
| 625 | bt_self_component_status bt_py3_component_class_sink_graph_is_configured( |
| 626 | bt_self_component_sink *self_component_sink) |
| 627 | { |
| 628 | PyObject *py_comp = NULL; |
| 629 | PyObject *py_method_result = NULL; |
| 630 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
| 631 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
| 632 | |
| 633 | py_comp = bt_self_component_get_data(self_component); |
| 634 | py_method_result = PyObject_CallMethod(py_comp, |
| 635 | "_graph_is_configured_from_native", NULL); |
| 636 | |
| 637 | BT_ASSERT(!py_method_result || py_method_result == Py_None); |
| 638 | |
| 639 | status = bt_py3_exc_to_self_component_status(); |
| 640 | |
| 641 | Py_XDECREF(py_method_result); |
| 642 | |
| 643 | return status; |
| 644 | } |
| 645 | |
| 646 | static |
| 647 | bt_query_status bt_py3_component_class_query( |
| 648 | const bt_component_class *component_class, |
| 649 | const bt_query_executor *query_executor, |
| 650 | const char *object, const bt_value *params, |
| 651 | bt_logging_level log_level, |
| 652 | const bt_value **result) |
| 653 | { |
| 654 | PyObject *py_cls = NULL; |
| 655 | PyObject *py_params_ptr = NULL; |
| 656 | PyObject *py_query_exec_ptr = NULL; |
| 657 | PyObject *py_query_func = NULL; |
| 658 | PyObject *py_object = NULL; |
| 659 | PyObject *py_results_addr = NULL; |
| 660 | bt_query_status status = BT_QUERY_STATUS_OK; |
| 661 | |
| 662 | py_cls = lookup_cc_ptr_to_py_cls(component_class); |
| 663 | if (!py_cls) { |
| 664 | BT_LOGE("Cannot find Python class associated to native component class: " |
| 665 | "comp-cls-addr=%p", component_class); |
| 666 | goto error; |
| 667 | } |
| 668 | |
| 669 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), |
| 670 | SWIGTYPE_p_bt_value, 0); |
| 671 | if (!py_params_ptr) { |
| 672 | BT_LOGE_STR("Failed to create a SWIG pointer object."); |
| 673 | goto error; |
| 674 | } |
| 675 | |
| 676 | py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor), |
| 677 | SWIGTYPE_p_bt_query_executor, 0); |
| 678 | if (!py_query_exec_ptr) { |
| 679 | BT_LOGE_STR("Failed to create a SWIG pointer object."); |
| 680 | goto error; |
| 681 | } |
| 682 | |
| 683 | py_object = SWIG_FromCharPtr(object); |
| 684 | if (!py_object) { |
| 685 | BT_LOGE_STR("Failed to create a Python string."); |
| 686 | goto error; |
| 687 | } |
| 688 | |
| 689 | py_results_addr = PyObject_CallMethod(py_cls, |
| 690 | "_query_from_native", "(OOOi)", py_query_exec_ptr, |
| 691 | py_object, py_params_ptr, (int) log_level); |
| 692 | |
| 693 | if (!py_results_addr) { |
| 694 | BT_LOGE("Failed to call Python class's _query_from_native() method: " |
| 695 | "py-cls-addr=%p", py_cls); |
| 696 | status = bt_py3_exc_to_query_status(); |
| 697 | goto end; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * The returned object, on success, is an integer object |
| 702 | * (PyLong) containing the address of a BT value object (new |
| 703 | * reference). |
| 704 | */ |
| 705 | *result = PyLong_AsVoidPtr(py_results_addr); |
| 706 | BT_ASSERT(!PyErr_Occurred()); |
| 707 | BT_ASSERT(*result); |
| 708 | goto end; |
| 709 | |
| 710 | error: |
| 711 | PyErr_Clear(); |
| 712 | status = BT_QUERY_STATUS_ERROR; |
| 713 | |
| 714 | end: |
| 715 | Py_XDECREF(py_params_ptr); |
| 716 | Py_XDECREF(py_query_exec_ptr); |
| 717 | Py_XDECREF(py_query_func); |
| 718 | Py_XDECREF(py_object); |
| 719 | Py_XDECREF(py_results_addr); |
| 720 | return status; |
| 721 | } |
| 722 | |
| 723 | static |
| 724 | bt_query_status bt_py3_component_class_source_query( |
| 725 | bt_self_component_class_source *self_component_class_source, |
| 726 | const bt_query_executor *query_executor, |
| 727 | const char *object, const bt_value *params, |
| 728 | bt_logging_level log_level, |
| 729 | const bt_value **result) |
| 730 | { |
| 731 | const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); |
| 732 | const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); |
| 733 | return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result); |
| 734 | } |
| 735 | |
| 736 | static |
| 737 | bt_query_status bt_py3_component_class_filter_query( |
| 738 | bt_self_component_class_filter *self_component_class_filter, |
| 739 | const bt_query_executor *query_executor, |
| 740 | const char *object, const bt_value *params, |
| 741 | bt_logging_level log_level, |
| 742 | const bt_value **result) |
| 743 | { |
| 744 | const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); |
| 745 | const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); |
| 746 | return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result); |
| 747 | } |
| 748 | |
| 749 | static |
| 750 | bt_query_status bt_py3_component_class_sink_query( |
| 751 | bt_self_component_class_sink *self_component_class_sink, |
| 752 | const bt_query_executor *query_executor, |
| 753 | const char *object, const bt_value *params, |
| 754 | bt_logging_level log_level, |
| 755 | const bt_value **result) |
| 756 | { |
| 757 | const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); |
| 758 | const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); |
| 759 | return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result); |
| 760 | } |
| 761 | |
| 762 | static |
| 763 | bt_self_message_iterator_status bt_py3_component_class_message_iterator_init( |
| 764 | bt_self_message_iterator *self_message_iterator, |
| 765 | bt_self_component *self_component, |
| 766 | bt_self_component_port_output *self_component_port_output) |
| 767 | { |
| 768 | bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 769 | PyObject *py_comp_cls = NULL; |
| 770 | PyObject *py_iter_cls = NULL; |
| 771 | PyObject *py_iter_ptr = NULL; |
| 772 | PyObject *py_component_port_output_ptr = NULL; |
| 773 | PyObject *py_init_method_result = NULL; |
| 774 | PyObject *py_iter = NULL; |
| 775 | PyObject *py_comp; |
| 776 | |
| 777 | py_comp = bt_self_component_get_data(self_component); |
| 778 | |
| 779 | /* Find user's Python message iterator class */ |
| 780 | py_comp_cls = PyObject_GetAttrString(py_comp, "__class__"); |
| 781 | if (!py_comp_cls) { |
| 782 | BT_LOGE_STR("Cannot get Python object's `__class__` attribute."); |
| 783 | goto error; |
| 784 | } |
| 785 | |
| 786 | py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls"); |
| 787 | if (!py_iter_cls) { |
| 788 | BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute."); |
| 789 | goto error; |
| 790 | } |
| 791 | |
| 792 | py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator), |
| 793 | SWIGTYPE_p_bt_self_message_iterator, 0); |
| 794 | if (!py_iter_ptr) { |
| 795 | BT_LOGE_STR("Failed to create a SWIG pointer object."); |
| 796 | goto error; |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * Create object with borrowed native message iterator |
| 801 | * reference: |
| 802 | * |
| 803 | * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr) |
| 804 | */ |
| 805 | py_iter = PyObject_CallMethod(py_iter_cls, "__new__", |
| 806 | "(OO)", py_iter_cls, py_iter_ptr); |
| 807 | if (!py_iter) { |
| 808 | BT_LOGE("Failed to call Python class's __new__() method: " |
| 809 | "py-cls-addr=%p", py_iter_cls); |
| 810 | bt2_py_loge_exception(); |
| 811 | goto error; |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * Initialize object: |
| 816 | * |
| 817 | * py_iter.__init__(self_output_port) |
| 818 | * |
| 819 | * through the _init_for_native helper static method. |
| 820 | * |
| 821 | * At this point, py_iter._ptr is set, so this initialization |
| 822 | * function has access to self._component (which gives it the |
| 823 | * user Python component object from which the iterator was |
| 824 | * created). |
| 825 | */ |
| 826 | py_component_port_output_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port_output), |
| 827 | SWIGTYPE_p_bt_self_component_port_output, 0); |
| 828 | if (!py_component_port_output_ptr) { |
| 829 | BT_LOGE_STR("Failed to create a SWIG pointer object."); |
| 830 | goto error; |
| 831 | } |
| 832 | |
| 833 | py_init_method_result = PyObject_CallMethod(py_iter, "_init_from_native", "O", py_component_port_output_ptr); |
| 834 | if (!py_init_method_result) { |
| 835 | BT_LOGE_STR("User's __init__() method failed."); |
| 836 | bt2_py_loge_exception(); |
| 837 | goto error; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Since the Python code can never instantiate a user-defined |
| 842 | * message iterator class, the native message iterator |
| 843 | * object does NOT belong to a user Python message iterator |
| 844 | * object (borrowed reference). However this Python object is |
| 845 | * owned by this native message iterator object. |
| 846 | * |
| 847 | * In the Python world, the lifetime of the native message |
| 848 | * iterator is managed by a _GenericMessageIterator |
| 849 | * instance: |
| 850 | * |
| 851 | * _GenericMessageIterator instance: |
| 852 | * owns a native bt_message_iterator object (iter) |
| 853 | * owns a _UserMessageIterator instance (py_iter) |
| 854 | * self._ptr is a borrowed reference to the |
| 855 | * native bt_private_connection_private_message_iterator |
| 856 | * object (iter) |
| 857 | */ |
| 858 | bt_self_message_iterator_set_data(self_message_iterator, py_iter); |
| 859 | py_iter = NULL; |
| 860 | goto end; |
| 861 | |
| 862 | error: |
| 863 | status = bt_py3_exc_to_self_message_iterator_status(); |
| 864 | if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
| 865 | /* |
| 866 | * Looks like there wasn't any exception from the Python |
| 867 | * side, but we're still in an error state here. |
| 868 | */ |
| 869 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * Clear any exception: we're returning a bad status anyway. If |
| 874 | * this call originated from Python, then the user gets an |
| 875 | * appropriate creation error. |
| 876 | */ |
| 877 | PyErr_Clear(); |
| 878 | |
| 879 | end: |
| 880 | Py_XDECREF(py_comp_cls); |
| 881 | Py_XDECREF(py_iter_cls); |
| 882 | Py_XDECREF(py_iter_ptr); |
| 883 | Py_XDECREF(py_component_port_output_ptr); |
| 884 | Py_XDECREF(py_init_method_result); |
| 885 | Py_XDECREF(py_iter); |
| 886 | return status; |
| 887 | } |
| 888 | |
| 889 | static |
| 890 | bt_self_message_iterator_status bt_py3_component_class_source_message_iterator_init( |
| 891 | bt_self_message_iterator *self_message_iterator, |
| 892 | bt_self_component_source *self_component_source, |
| 893 | bt_self_component_port_output *self_component_port_output) |
| 894 | { |
| 895 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); |
| 896 | return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); |
| 897 | } |
| 898 | |
| 899 | static |
| 900 | bt_self_message_iterator_status bt_py3_component_class_filter_message_iterator_init( |
| 901 | bt_self_message_iterator *self_message_iterator, |
| 902 | bt_self_component_filter *self_component_filter, |
| 903 | bt_self_component_port_output *self_component_port_output) |
| 904 | { |
| 905 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); |
| 906 | return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); |
| 907 | } |
| 908 | |
| 909 | static |
| 910 | void bt_py3_component_class_message_iterator_finalize( |
| 911 | bt_self_message_iterator *message_iterator) |
| 912 | { |
| 913 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); |
| 914 | PyObject *py_method_result = NULL; |
| 915 | |
| 916 | BT_ASSERT(py_message_iter); |
| 917 | |
| 918 | /* Call user's _finalize() method */ |
| 919 | py_method_result = PyObject_CallMethod(py_message_iter, |
| 920 | "_finalize", NULL); |
| 921 | |
| 922 | if (PyErr_Occurred()) { |
| 923 | BT_LOGW("User's _finalize() method raised an exception: ignoring."); |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Ignore any exception raised by the _finalize() method because |
| 928 | * it won't change anything at this point: the component is |
| 929 | * being destroyed anyway. |
| 930 | */ |
| 931 | PyErr_Clear(); |
| 932 | Py_XDECREF(py_method_result); |
| 933 | Py_DECREF(py_message_iter); |
| 934 | } |
| 935 | |
| 936 | /* Valid for both sources and filters. */ |
| 937 | |
| 938 | static |
| 939 | bt_self_message_iterator_status bt_py3_component_class_message_iterator_next( |
| 940 | bt_self_message_iterator *message_iterator, |
| 941 | bt_message_array_const msgs, uint64_t capacity, |
| 942 | uint64_t *count) |
| 943 | { |
| 944 | bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 945 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); |
| 946 | PyObject *py_method_result = NULL; |
| 947 | |
| 948 | BT_ASSERT(py_message_iter); |
| 949 | py_method_result = PyObject_CallMethod(py_message_iter, |
| 950 | "_next_from_native", NULL); |
| 951 | if (!py_method_result) { |
| 952 | status = bt_py3_exc_to_self_message_iterator_status(); |
| 953 | BT_ASSERT(status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK); |
| 954 | goto end; |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | * The returned object, on success, is an integer object |
| 959 | * (PyLong) containing the address of a native message |
| 960 | * object (which is now ours). |
| 961 | */ |
| 962 | msgs[0] = PyLong_AsVoidPtr(py_method_result); |
| 963 | *count = 1; |
| 964 | |
| 965 | /* Clear potential overflow error; should never happen */ |
| 966 | BT_ASSERT(!PyErr_Occurred()); |
| 967 | goto end; |
| 968 | |
| 969 | end: |
| 970 | Py_XDECREF(py_method_result); |
| 971 | return status; |
| 972 | } |
| 973 | |
| 974 | static |
| 975 | bt_self_component_status bt_py3_component_class_sink_consume( |
| 976 | bt_self_component_sink *self_component_sink) |
| 977 | { |
| 978 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
| 979 | PyObject *py_comp = bt_self_component_get_data(self_component); |
| 980 | PyObject *py_method_result = NULL; |
| 981 | bt_self_component_status status; |
| 982 | |
| 983 | BT_ASSERT(py_comp); |
| 984 | py_method_result = PyObject_CallMethod(py_comp, |
| 985 | "_consume", NULL); |
| 986 | |
| 987 | status = bt_py3_exc_to_self_component_status(); |
| 988 | if (!py_method_result && status == BT_SELF_COMPONENT_STATUS_OK) { |
| 989 | /* Pretty sure this should never happen, but just in case */ |
| 990 | BT_LOGE("User's _consume() method failed without raising an exception: " |
| 991 | "status=%d", status); |
| 992 | status = BT_SELF_COMPONENT_STATUS_ERROR; |
| 993 | } |
| 994 | |
| 995 | Py_XDECREF(py_method_result); |
| 996 | return status; |
| 997 | } |
| 998 | |
| 999 | static |
| 1000 | int bt_py3_component_class_set_help_and_desc( |
| 1001 | bt_component_class *component_class, |
| 1002 | const char *description, const char *help) |
| 1003 | { |
| 1004 | int ret; |
| 1005 | |
| 1006 | if (description) { |
| 1007 | ret = bt_component_class_set_description(component_class, description); |
| 1008 | if (ret) { |
| 1009 | BT_LOGE("Cannot set component class's description: " |
| 1010 | "comp-cls-addr=%p", component_class); |
| 1011 | goto end; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | if (help) { |
| 1016 | ret = bt_component_class_set_help(component_class, help); |
| 1017 | if (ret) { |
| 1018 | BT_LOGE("Cannot set component class's help text: " |
| 1019 | "comp-cls-addr=%p", component_class); |
| 1020 | goto end; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | ret = 0; |
| 1025 | |
| 1026 | end: |
| 1027 | return ret; |
| 1028 | } |
| 1029 | |
| 1030 | static |
| 1031 | bt_component_class_source *bt_py3_component_class_source_create( |
| 1032 | PyObject *py_cls, const char *name, const char *description, |
| 1033 | const char *help) |
| 1034 | { |
| 1035 | bt_component_class_source *component_class_source; |
| 1036 | bt_component_class *component_class; |
| 1037 | int ret; |
| 1038 | |
| 1039 | BT_ASSERT(py_cls); |
| 1040 | |
| 1041 | component_class_source = bt_component_class_source_create(name, |
| 1042 | bt_py3_component_class_message_iterator_next); |
| 1043 | if (!component_class_source) { |
| 1044 | BT_LOGE_STR("Cannot create source component class."); |
| 1045 | goto end; |
| 1046 | } |
| 1047 | |
| 1048 | component_class = bt_component_class_source_as_component_class(component_class_source); |
| 1049 | |
| 1050 | if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) { |
| 1051 | goto end; |
| 1052 | } |
| 1053 | |
| 1054 | ret = bt_component_class_source_set_init_method(component_class_source, bt_py3_component_class_source_init); |
| 1055 | BT_ASSERT(ret == 0); |
| 1056 | ret = bt_component_class_source_set_finalize_method(component_class_source, bt_py3_component_class_source_finalize); |
| 1057 | BT_ASSERT(ret == 0); |
| 1058 | ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source, |
| 1059 | bt_py3_component_class_can_seek_beginning); |
| 1060 | BT_ASSERT(ret == 0); |
| 1061 | ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source, |
| 1062 | bt_py3_component_class_seek_beginning); |
| 1063 | BT_ASSERT(ret == 0); |
| 1064 | ret = bt_component_class_source_set_output_port_connected_method(component_class_source, |
| 1065 | bt_py3_component_class_source_output_port_connected); |
| 1066 | BT_ASSERT(ret == 0); |
| 1067 | ret = bt_component_class_source_set_query_method(component_class_source, bt_py3_component_class_source_query); |
| 1068 | BT_ASSERT(ret == 0); |
| 1069 | ret = bt_component_class_source_set_message_iterator_init_method( |
| 1070 | component_class_source, bt_py3_component_class_source_message_iterator_init); |
| 1071 | BT_ASSERT(ret == 0); |
| 1072 | ret = bt_component_class_source_set_message_iterator_finalize_method( |
| 1073 | component_class_source, bt_py3_component_class_message_iterator_finalize); |
| 1074 | BT_ASSERT(ret == 0); |
| 1075 | |
| 1076 | register_cc_ptr_to_py_cls(component_class, py_cls); |
| 1077 | |
| 1078 | end: |
| 1079 | return component_class_source; |
| 1080 | } |
| 1081 | |
| 1082 | static |
| 1083 | bt_component_class_filter *bt_py3_component_class_filter_create( |
| 1084 | PyObject *py_cls, const char *name, const char *description, |
| 1085 | const char *help) |
| 1086 | { |
| 1087 | bt_component_class *component_class; |
| 1088 | bt_component_class_filter *component_class_filter; |
| 1089 | int ret; |
| 1090 | |
| 1091 | BT_ASSERT(py_cls); |
| 1092 | |
| 1093 | component_class_filter = bt_component_class_filter_create(name, |
| 1094 | bt_py3_component_class_message_iterator_next); |
| 1095 | if (!component_class_filter) { |
| 1096 | BT_LOGE_STR("Cannot create filter component class."); |
| 1097 | goto end; |
| 1098 | } |
| 1099 | |
| 1100 | component_class = bt_component_class_filter_as_component_class(component_class_filter); |
| 1101 | |
| 1102 | if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) { |
| 1103 | goto end; |
| 1104 | } |
| 1105 | |
| 1106 | ret = bt_component_class_filter_set_init_method(component_class_filter, bt_py3_component_class_filter_init); |
| 1107 | BT_ASSERT(ret == 0); |
| 1108 | ret = bt_component_class_filter_set_finalize_method (component_class_filter, bt_py3_component_class_filter_finalize); |
| 1109 | BT_ASSERT(ret == 0); |
| 1110 | ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter, |
| 1111 | bt_py3_component_class_can_seek_beginning); |
| 1112 | BT_ASSERT(ret == 0); |
| 1113 | ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter, |
| 1114 | bt_py3_component_class_seek_beginning); |
| 1115 | BT_ASSERT(ret == 0); |
| 1116 | ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter, |
| 1117 | bt_py3_component_class_filter_input_port_connected); |
| 1118 | BT_ASSERT(ret == 0); |
| 1119 | ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter, |
| 1120 | bt_py3_component_class_filter_output_port_connected); |
| 1121 | BT_ASSERT(ret == 0); |
| 1122 | ret = bt_component_class_filter_set_query_method(component_class_filter, bt_py3_component_class_filter_query); |
| 1123 | BT_ASSERT(ret == 0); |
| 1124 | ret = bt_component_class_filter_set_message_iterator_init_method( |
| 1125 | component_class_filter, bt_py3_component_class_filter_message_iterator_init); |
| 1126 | BT_ASSERT(ret == 0); |
| 1127 | ret = bt_component_class_filter_set_message_iterator_finalize_method( |
| 1128 | component_class_filter, bt_py3_component_class_message_iterator_finalize); |
| 1129 | BT_ASSERT(ret == 0); |
| 1130 | |
| 1131 | register_cc_ptr_to_py_cls(component_class, py_cls); |
| 1132 | |
| 1133 | end: |
| 1134 | return component_class_filter; |
| 1135 | } |
| 1136 | |
| 1137 | static |
| 1138 | bt_component_class_sink *bt_py3_component_class_sink_create( |
| 1139 | PyObject *py_cls, const char *name, const char *description, |
| 1140 | const char *help) |
| 1141 | { |
| 1142 | bt_component_class_sink *component_class_sink; |
| 1143 | bt_component_class *component_class; |
| 1144 | int ret; |
| 1145 | |
| 1146 | BT_ASSERT(py_cls); |
| 1147 | |
| 1148 | component_class_sink = bt_component_class_sink_create(name, bt_py3_component_class_sink_consume); |
| 1149 | |
| 1150 | if (!component_class_sink) { |
| 1151 | BT_LOGE_STR("Cannot create sink component class."); |
| 1152 | goto end; |
| 1153 | } |
| 1154 | |
| 1155 | component_class = bt_component_class_sink_as_component_class(component_class_sink); |
| 1156 | |
| 1157 | if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) { |
| 1158 | goto end; |
| 1159 | } |
| 1160 | |
| 1161 | ret = bt_component_class_sink_set_init_method(component_class_sink, bt_py3_component_class_sink_init); |
| 1162 | BT_ASSERT(ret == 0); |
| 1163 | ret = bt_component_class_sink_set_finalize_method(component_class_sink, bt_py3_component_class_sink_finalize); |
| 1164 | BT_ASSERT(ret == 0); |
| 1165 | ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink, |
| 1166 | bt_py3_component_class_sink_input_port_connected); |
| 1167 | BT_ASSERT(ret == 0); |
| 1168 | ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink, |
| 1169 | bt_py3_component_class_sink_graph_is_configured); |
| 1170 | BT_ASSERT(ret == 0); |
| 1171 | ret = bt_component_class_sink_set_query_method(component_class_sink, bt_py3_component_class_sink_query); |
| 1172 | BT_ASSERT(ret == 0); |
| 1173 | |
| 1174 | register_cc_ptr_to_py_cls(component_class, py_cls); |
| 1175 | |
| 1176 | end: |
| 1177 | return component_class_sink; |
| 1178 | } |
| 1179 | %} |
| 1180 | |
| 1181 | struct bt_component_class_source *bt_py3_component_class_source_create( |
| 1182 | PyObject *py_cls, const char *name, const char *description, |
| 1183 | const char *help); |
| 1184 | struct bt_component_class_filter *bt_py3_component_class_filter_create( |
| 1185 | PyObject *py_cls, const char *name, const char *description, |
| 1186 | const char *help); |
| 1187 | struct bt_component_class_sink *bt_py3_component_class_sink_create( |
| 1188 | PyObject *py_cls, const char *name, const char *description, |
| 1189 | const char *help); |
| 1190 | void bt_py3_cc_init_from_bt2(void); |
| 1191 | void bt_py3_cc_exit_handler(void); |