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