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