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