Commit | Line | Data |
---|---|---|
6fa6bfa1 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 | ||
fb25b9e3 | 25 | %include <babeltrace2/graph/component-class.h> |
e4b56bb9 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> | |
6fa6bfa1 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 | |
fb25b9e3 | 46 | * class is created by one of the bt_bt2_component_class_*_create() |
6fa6bfa1 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 | ||
efb177a7 SM |
58 | static |
59 | void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc, | |
6fa6bfa1 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 | ||
efb177a7 SM |
77 | static |
78 | PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc) | |
6fa6bfa1 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; | |
97 | static PyObject *py_mod_bt2_exc_try_again_type = NULL; | |
98 | static PyObject *py_mod_bt2_exc_stop_type = NULL; | |
fa4c33e3 | 99 | static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL; |
fb25b9e3 PP |
100 | static PyObject *py_mod_bt2_exc_invalid_object_type = NULL; |
101 | static PyObject *py_mod_bt2_exc_invalid_params_type = NULL; | |
102 | static PyObject *py_mod_bt2_exc_unsupported_type = NULL; | |
6fa6bfa1 | 103 | |
efb177a7 | 104 | static |
fb25b9e3 | 105 | void bt_bt2_cc_init_from_bt2(void) |
6fa6bfa1 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 = | |
118 | PyObject_GetAttrString(py_mod_bt2, "Error"); | |
119 | BT_ASSERT(py_mod_bt2_exc_error_type); | |
120 | py_mod_bt2_exc_try_again_type = | |
121 | PyObject_GetAttrString(py_mod_bt2, "TryAgain"); | |
122 | BT_ASSERT(py_mod_bt2_exc_try_again_type); | |
123 | py_mod_bt2_exc_stop_type = | |
124 | PyObject_GetAttrString(py_mod_bt2, "Stop"); | |
125 | BT_ASSERT(py_mod_bt2_exc_stop_type); | |
fb25b9e3 PP |
126 | py_mod_bt2_exc_invalid_object_type = |
127 | PyObject_GetAttrString(py_mod_bt2, "InvalidObject"); | |
128 | BT_ASSERT(py_mod_bt2_exc_invalid_object_type); | |
129 | py_mod_bt2_exc_invalid_params_type = | |
130 | PyObject_GetAttrString(py_mod_bt2, "InvalidParams"); | |
131 | BT_ASSERT(py_mod_bt2_exc_invalid_params_type); | |
132 | py_mod_bt2_exc_unsupported_type = | |
133 | PyObject_GetAttrString(py_mod_bt2, "Unsupported"); | |
134 | BT_ASSERT(py_mod_bt2_exc_unsupported_type); | |
6fa6bfa1 SM |
135 | } |
136 | ||
efb177a7 | 137 | static |
fb25b9e3 | 138 | void bt_bt2_cc_exit_handler(void) |
6fa6bfa1 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 | |
fb25b9e3 | 144 | * bt_bt2_cc_init_from_bt2() here. The global variables continue |
6fa6bfa1 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); | |
fa4c33e3 | 157 | Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type); |
fb25b9e3 PP |
158 | Py_XDECREF(py_mod_bt2_exc_invalid_object_type); |
159 | Py_XDECREF(py_mod_bt2_exc_invalid_params_type); | |
6fa6bfa1 SM |
160 | } |
161 | ||
162 | ||
163 | /* Library destructor */ | |
164 | ||
165 | __attribute__((destructor)) | |
efb177a7 | 166 | static |
fb25b9e3 | 167 | void native_comp_class_dtor(void) { |
6fa6bfa1 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 | ||
3b2be708 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 | /* | |
197 | * If the raised exception is a bt2.Error, restore the wrapped error. | |
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 | /* | |
205 | * We never raise a bt2.Error with a cause: it should be the | |
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() | |
269 | * except bt2.Error as e1: | |
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 | * | |
276 | * TypeError -> ValueError -> bt2.Error | |
277 | * | |
278 | * Where the TypeError is the current exception (obtained from PyErr_Fetch). | |
279 | * | |
280 | * The bt2.Error contains a `struct bt_error *` that used to be the current | |
281 | * thread's error, at the moment the exception was raised. | |
282 | * | |
283 | * This function gets to the bt2.Error and restores the wrapped | |
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 | ||
fb25b9e3 | 324 | static inline |
f8d71b59 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, | |
3b2be708 SM |
329 | bt_self_message_iterator *self_message_iterator, |
330 | const char *module_name) | |
6fa6bfa1 | 331 | { |
2d7aa9b6 | 332 | GString *gstr; |
6fa6bfa1 | 333 | |
8e01f2d9 | 334 | BT_ASSERT(PyErr_Occurred()); |
f33c35fb | 335 | gstr = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL); |
2d7aa9b6 | 336 | if (!gstr) { |
f33c35fb | 337 | /* bt_py_common_format_current_exception() logs errors */ |
6fa6bfa1 SM |
338 | goto end; |
339 | } | |
340 | ||
fb25b9e3 | 341 | BT_LOG_WRITE(log_level, BT_LOG_TAG, "%s", gstr->str); |
6fa6bfa1 | 342 | |
f8d71b59 | 343 | if (append_error) { |
3b2be708 SM |
344 | restore_bt_error_and_append_current_exception_chain( |
345 | self_component_class, self_component, | |
346 | self_message_iterator, module_name); | |
347 | ||
f8d71b59 SM |
348 | } |
349 | ||
6fa6bfa1 | 350 | end: |
2d7aa9b6 PP |
351 | if (gstr) { |
352 | g_string_free(gstr, TRUE); | |
6fa6bfa1 | 353 | } |
6fa6bfa1 SM |
354 | } |
355 | ||
fb25b9e3 | 356 | static inline |
3b2be708 | 357 | void loge_exception(const char *module_name) |
6fa6bfa1 | 358 | { |
3b2be708 SM |
359 | log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL, |
360 | NULL, module_name); | |
f8d71b59 SM |
361 | } |
362 | ||
363 | static | |
364 | void loge_exception_message_iterator( | |
365 | bt_self_message_iterator *self_message_iterator) | |
366 | { | |
3b2be708 SM |
367 | log_exception_and_maybe_append_error(BT_LOG_ERROR, true, NULL, NULL, |
368 | self_message_iterator, NULL); | |
6fa6bfa1 SM |
369 | } |
370 | ||
fb25b9e3 PP |
371 | static inline |
372 | void logw_exception(void) | |
6fa6bfa1 | 373 | { |
3b2be708 SM |
374 | log_exception_and_maybe_append_error(BT_LOG_WARNING, false, NULL, NULL, |
375 | NULL, NULL); | |
6fa6bfa1 SM |
376 | } |
377 | ||
fb25b9e3 | 378 | static inline |
f8d71b59 SM |
379 | int py_exc_to_status(bt_self_component_class *self_component_class, |
380 | bt_self_component *self_component, | |
3b2be708 SM |
381 | bt_self_message_iterator *self_message_iterator, |
382 | const char *module_name) | |
6fa6bfa1 | 383 | { |
fb25b9e3 | 384 | int status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
385 | PyObject *exc = PyErr_Occurred(); |
386 | ||
387 | if (!exc) { | |
388 | goto end; | |
389 | } | |
390 | ||
391 | if (PyErr_GivenExceptionMatches(exc, | |
fb25b9e3 PP |
392 | py_mod_bt2_exc_try_again_type)) { |
393 | status = __BT_FUNC_STATUS_AGAIN; | |
6fa6bfa1 | 394 | } else if (PyErr_GivenExceptionMatches(exc, |
fb25b9e3 PP |
395 | py_mod_bt2_exc_stop_type)) { |
396 | status = __BT_FUNC_STATUS_END; | |
6fa6bfa1 | 397 | } else if (PyErr_GivenExceptionMatches(exc, |
fb25b9e3 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; | |
403 | } else if (PyErr_GivenExceptionMatches(exc, | |
404 | py_mod_bt2_exc_unsupported_type)) { | |
405 | status = __BT_FUNC_STATUS_UNSUPPORTED; | |
6fa6bfa1 | 406 | } else { |
fb25b9e3 | 407 | /* Unknown exception: convert to general error */ |
f8d71b59 SM |
408 | log_exception_and_maybe_append_error(BT_LOG_WARNING, true, |
409 | self_component_class, self_component, | |
3b2be708 | 410 | self_message_iterator, module_name); |
fb25b9e3 | 411 | status = __BT_FUNC_STATUS_ERROR; |
6fa6bfa1 SM |
412 | } |
413 | ||
414 | end: | |
415 | PyErr_Clear(); | |
416 | return status; | |
417 | } | |
418 | ||
f8d71b59 SM |
419 | static |
420 | int py_exc_to_status_component_class(bt_self_component_class *self_component_class) | |
421 | { | |
3b2be708 | 422 | return py_exc_to_status(self_component_class, NULL, NULL, NULL); |
f8d71b59 SM |
423 | } |
424 | ||
425 | static | |
426 | int py_exc_to_status_component(bt_self_component *self_component) | |
427 | { | |
3b2be708 | 428 | return py_exc_to_status(NULL, self_component, NULL, NULL); |
f8d71b59 SM |
429 | } |
430 | ||
431 | static | |
432 | int py_exc_to_status_message_iterator( | |
433 | bt_self_message_iterator *self_message_iterator) | |
434 | { | |
3b2be708 | 435 | return py_exc_to_status(NULL, NULL, self_message_iterator, NULL); |
f8d71b59 SM |
436 | } |
437 | ||
fb25b9e3 PP |
438 | /* Component class proxy methods (delegate to the attached Python object) */ |
439 | ||
efb177a7 | 440 | static |
fb25b9e3 | 441 | bt_component_class_init_method_status component_class_init( |
6fa6bfa1 SM |
442 | bt_self_component *self_component, |
443 | void *self_component_v, | |
444 | swig_type_info *self_comp_cls_type_swig_type, | |
445 | const bt_value *params, | |
446 | void *init_method_data) | |
447 | { | |
448 | const bt_component *component = bt_self_component_as_component(self_component); | |
449 | const bt_component_class *component_class = bt_component_borrow_class_const(component); | |
fb25b9e3 | 450 | bt_component_class_init_method_status status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
451 | PyObject *py_cls = NULL; |
452 | PyObject *py_comp = NULL; | |
453 | PyObject *py_params_ptr = NULL; | |
454 | PyObject *py_comp_ptr = NULL; | |
455 | ||
456 | (void) init_method_data; | |
457 | ||
458 | BT_ASSERT(self_component); | |
459 | BT_ASSERT(self_component_v); | |
460 | BT_ASSERT(self_comp_cls_type_swig_type); | |
461 | ||
462 | /* | |
463 | * Get the user-defined Python class which created this | |
464 | * component's class in the first place (borrowed | |
465 | * reference). | |
466 | */ | |
467 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
468 | if (!py_cls) { | |
469 | BT_LOGE("Cannot find Python class associated to native component class: " | |
470 | "comp-cls-addr=%p", component_class); | |
471 | goto error; | |
472 | } | |
473 | ||
474 | /* Parameters pointer -> SWIG pointer Python object */ | |
475 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
476 | SWIGTYPE_p_bt_value, 0); | |
477 | if (!py_params_ptr) { | |
478 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
479 | goto error; | |
480 | } | |
481 | ||
482 | py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v), | |
483 | self_comp_cls_type_swig_type, 0); | |
484 | if (!py_comp_ptr) { | |
485 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
486 | goto error; | |
487 | } | |
488 | ||
489 | /* | |
490 | * Do the equivalent of this: | |
491 | * | |
deec48a6 | 492 | * py_comp = py_cls._bt_init_from_native(py_comp_ptr, py_params_ptr) |
6fa6bfa1 | 493 | * |
deec48a6 | 494 | * _UserComponentType._bt_init_from_native() calls the Python |
6fa6bfa1 SM |
495 | * component object's __init__() function. |
496 | */ | |
497 | py_comp = PyObject_CallMethod(py_cls, | |
deec48a6 | 498 | "_bt_init_from_native", "(OO)", py_comp_ptr, py_params_ptr); |
6fa6bfa1 | 499 | if (!py_comp) { |
deec48a6 | 500 | BT_LOGW("Failed to call Python class's _bt_init_from_native() method: " |
6fa6bfa1 | 501 | "py-cls-addr=%p", py_cls); |
f8d71b59 SM |
502 | status = py_exc_to_status_component(self_component); |
503 | goto end; | |
6fa6bfa1 SM |
504 | } |
505 | ||
506 | /* | |
507 | * Our user Python component object is now fully created and | |
508 | * initialized by the user. Since we just created it, this | |
509 | * native component is its only (persistent) owner. | |
510 | */ | |
511 | bt_self_component_set_data(self_component, py_comp); | |
512 | py_comp = NULL; | |
513 | goto end; | |
514 | ||
515 | error: | |
fb25b9e3 | 516 | status = __BT_FUNC_STATUS_ERROR; |
6fa6bfa1 SM |
517 | |
518 | /* | |
519 | * Clear any exception: we're returning a bad status anyway. If | |
520 | * this call originated from Python (creation from a plugin's | |
521 | * component class, for example), then the user gets an | |
522 | * appropriate creation error. | |
523 | */ | |
524 | PyErr_Clear(); | |
525 | ||
526 | end: | |
527 | Py_XDECREF(py_comp); | |
528 | Py_XDECREF(py_params_ptr); | |
529 | Py_XDECREF(py_comp_ptr); | |
530 | return status; | |
531 | } | |
532 | ||
533 | /* | |
534 | * Method of bt_component_class_source to initialize a bt_self_component_source | |
535 | * of that class. | |
536 | */ | |
537 | ||
efb177a7 | 538 | static |
fb25b9e3 | 539 | bt_component_class_init_method_status component_class_source_init( |
efb177a7 | 540 | bt_self_component_source *self_component_source, |
6fa6bfa1 SM |
541 | const bt_value *params, void *init_method_data) |
542 | { | |
543 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
fb25b9e3 | 544 | return component_class_init( |
6fa6bfa1 SM |
545 | self_component, |
546 | self_component_source, | |
547 | SWIGTYPE_p_bt_self_component_source, | |
548 | params, init_method_data); | |
549 | } | |
550 | ||
efb177a7 | 551 | static |
fb25b9e3 | 552 | bt_component_class_init_method_status component_class_filter_init( |
efb177a7 | 553 | bt_self_component_filter *self_component_filter, |
6fa6bfa1 SM |
554 | const bt_value *params, void *init_method_data) |
555 | { | |
556 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
fb25b9e3 | 557 | return component_class_init( |
6fa6bfa1 SM |
558 | self_component, |
559 | self_component_filter, | |
560 | SWIGTYPE_p_bt_self_component_filter, | |
561 | params, init_method_data); | |
562 | } | |
563 | ||
efb177a7 | 564 | static |
fb25b9e3 | 565 | bt_component_class_init_method_status component_class_sink_init( |
efb177a7 | 566 | bt_self_component_sink *self_component_sink, |
6fa6bfa1 SM |
567 | const bt_value *params, void *init_method_data) |
568 | { | |
569 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
fb25b9e3 | 570 | return component_class_init( |
6fa6bfa1 SM |
571 | self_component, |
572 | self_component_sink, | |
573 | SWIGTYPE_p_bt_self_component_sink, | |
574 | params, init_method_data); | |
575 | } | |
576 | ||
efb177a7 | 577 | static |
fb25b9e3 | 578 | void component_class_finalize(bt_self_component *self_component) |
6fa6bfa1 SM |
579 | { |
580 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
581 | BT_ASSERT(py_comp); | |
582 | ||
583 | /* Call user's _finalize() method */ | |
584 | PyObject *py_method_result = PyObject_CallMethod(py_comp, | |
585 | "_finalize", NULL); | |
586 | ||
587 | if (PyErr_Occurred()) { | |
fb25b9e3 PP |
588 | BT_LOGW("User component's _finalize() method raised an exception: ignoring:"); |
589 | logw_exception(); | |
6fa6bfa1 SM |
590 | } |
591 | ||
592 | /* | |
593 | * Ignore any exception raised by the _finalize() method because | |
594 | * it won't change anything at this point: the component is | |
595 | * being destroyed anyway. | |
596 | */ | |
597 | PyErr_Clear(); | |
598 | Py_XDECREF(py_method_result); | |
599 | Py_DECREF(py_comp); | |
600 | } | |
601 | ||
efb177a7 | 602 | static |
fb25b9e3 | 603 | void component_class_source_finalize(bt_self_component_source *self_component_source) |
6fa6bfa1 SM |
604 | { |
605 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
fb25b9e3 | 606 | component_class_finalize(self_component); |
6fa6bfa1 SM |
607 | } |
608 | ||
efb177a7 | 609 | static |
fb25b9e3 | 610 | void component_class_filter_finalize(bt_self_component_filter *self_component_filter) |
6fa6bfa1 SM |
611 | { |
612 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
fb25b9e3 | 613 | component_class_finalize(self_component); |
6fa6bfa1 SM |
614 | } |
615 | ||
efb177a7 | 616 | static |
fb25b9e3 | 617 | void component_class_sink_finalize(bt_self_component_sink *self_component_sink) |
6fa6bfa1 SM |
618 | { |
619 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
fb25b9e3 | 620 | component_class_finalize(self_component); |
6fa6bfa1 SM |
621 | } |
622 | ||
39ddfa44 | 623 | static |
fb25b9e3 | 624 | bt_bool component_class_can_seek_beginning( |
39ddfa44 SM |
625 | bt_self_message_iterator *self_message_iterator) |
626 | { | |
627 | PyObject *py_iter; | |
628 | PyObject *py_result = NULL; | |
629 | bt_bool can_seek_beginning = false; | |
630 | ||
631 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); | |
632 | BT_ASSERT(py_iter); | |
633 | ||
deec48a6 | 634 | py_result = PyObject_GetAttrString(py_iter, "_bt_can_seek_beginning_from_native"); |
39ddfa44 SM |
635 | |
636 | BT_ASSERT(!py_result || PyBool_Check(py_result)); | |
637 | ||
638 | if (py_result) { | |
639 | can_seek_beginning = PyObject_IsTrue(py_result); | |
640 | } else { | |
641 | /* | |
642 | * Once can_seek_beginning can report errors, convert the | |
643 | * exception to a status. For now, log and return false; | |
644 | */ | |
f8d71b59 | 645 | loge_exception_message_iterator(self_message_iterator); |
39ddfa44 SM |
646 | PyErr_Clear(); |
647 | } | |
648 | ||
649 | Py_XDECREF(py_result); | |
650 | ||
651 | return can_seek_beginning; | |
652 | } | |
653 | ||
654 | static | |
fb25b9e3 PP |
655 | bt_component_class_message_iterator_seek_beginning_method_status |
656 | component_class_seek_beginning(bt_self_message_iterator *self_message_iterator) | |
39ddfa44 SM |
657 | { |
658 | PyObject *py_iter; | |
659 | PyObject *py_result; | |
fb25b9e3 | 660 | bt_component_class_message_iterator_seek_beginning_method_status status; |
39ddfa44 SM |
661 | |
662 | py_iter = bt_self_message_iterator_get_data(self_message_iterator); | |
663 | BT_ASSERT(py_iter); | |
deec48a6 | 664 | py_result = PyObject_CallMethod(py_iter, "_bt_seek_beginning_from_native", |
fb25b9e3 | 665 | NULL); |
39ddfa44 | 666 | BT_ASSERT(!py_result || py_result == Py_None); |
3b2be708 | 667 | status = py_exc_to_status_message_iterator(self_message_iterator); |
39ddfa44 | 668 | Py_XDECREF(py_result); |
39ddfa44 SM |
669 | return status; |
670 | } | |
671 | ||
efb177a7 | 672 | static |
fb25b9e3 | 673 | bt_component_class_port_connected_method_status component_class_port_connected( |
6fa6bfa1 SM |
674 | bt_self_component *self_component, |
675 | void *self_component_port, | |
676 | swig_type_info *self_component_port_swig_type, | |
677 | bt_port_type self_component_port_type, | |
678 | const void *other_port, | |
679 | swig_type_info *other_port_swig_type) | |
680 | { | |
fb25b9e3 | 681 | bt_component_class_port_connected_method_status status; |
6fa6bfa1 SM |
682 | PyObject *py_comp = NULL; |
683 | PyObject *py_self_port_ptr = NULL; | |
684 | PyObject *py_other_port_ptr = NULL; | |
685 | PyObject *py_method_result = NULL; | |
686 | ||
687 | py_comp = bt_self_component_get_data(self_component); | |
688 | BT_ASSERT(py_comp); | |
6fa6bfa1 SM |
689 | py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port), |
690 | self_component_port_swig_type, 0); | |
691 | if (!py_self_port_ptr) { | |
692 | BT_LOGF_STR("Failed to create a SWIG pointer object."); | |
fb25b9e3 | 693 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
6fa6bfa1 SM |
694 | goto end; |
695 | } | |
696 | ||
697 | py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port), | |
698 | other_port_swig_type, 0); | |
699 | if (!py_other_port_ptr) { | |
700 | BT_LOGF_STR("Failed to create a SWIG pointer object."); | |
fb25b9e3 | 701 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
6fa6bfa1 SM |
702 | goto end; } |
703 | ||
704 | py_method_result = PyObject_CallMethod(py_comp, | |
deec48a6 | 705 | "_bt_port_connected_from_native", "(OiO)", py_self_port_ptr, |
6fa6bfa1 | 706 | self_component_port_type, py_other_port_ptr); |
6fa6bfa1 | 707 | BT_ASSERT(!py_method_result || py_method_result == Py_None); |
f8d71b59 | 708 | status = py_exc_to_status_component(self_component); |
6fa6bfa1 SM |
709 | |
710 | end: | |
711 | Py_XDECREF(py_self_port_ptr); | |
712 | Py_XDECREF(py_other_port_ptr); | |
713 | Py_XDECREF(py_method_result); | |
6fa6bfa1 SM |
714 | return status; |
715 | } | |
716 | ||
efb177a7 | 717 | static |
fb25b9e3 PP |
718 | bt_component_class_port_connected_method_status |
719 | component_class_source_output_port_connected( | |
6fa6bfa1 SM |
720 | bt_self_component_source *self_component_source, |
721 | bt_self_component_port_output *self_component_port_output, | |
722 | const bt_port_input *other_port_input) | |
723 | { | |
724 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
725 | ||
fb25b9e3 | 726 | return component_class_port_connected( |
6fa6bfa1 SM |
727 | self_component, |
728 | self_component_port_output, | |
729 | SWIGTYPE_p_bt_self_component_port_output, | |
730 | BT_PORT_TYPE_OUTPUT, | |
731 | other_port_input, | |
732 | SWIGTYPE_p_bt_port_input); | |
733 | } | |
734 | ||
efb177a7 | 735 | static |
fb25b9e3 PP |
736 | bt_component_class_port_connected_method_status |
737 | component_class_filter_input_port_connected( | |
6fa6bfa1 SM |
738 | bt_self_component_filter *self_component_filter, |
739 | bt_self_component_port_input *self_component_port_input, | |
740 | const bt_port_output *other_port_output) | |
741 | { | |
742 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
743 | ||
fb25b9e3 | 744 | return component_class_port_connected( |
6fa6bfa1 SM |
745 | self_component, |
746 | self_component_port_input, | |
747 | SWIGTYPE_p_bt_self_component_port_input, | |
748 | BT_PORT_TYPE_INPUT, | |
749 | other_port_output, | |
750 | SWIGTYPE_p_bt_port_output); | |
751 | } | |
752 | ||
efb177a7 | 753 | static |
fb25b9e3 PP |
754 | bt_component_class_port_connected_method_status |
755 | component_class_filter_output_port_connected( | |
6fa6bfa1 SM |
756 | bt_self_component_filter *self_component_filter, |
757 | bt_self_component_port_output *self_component_port_output, | |
758 | const bt_port_input *other_port_input) | |
759 | { | |
760 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
761 | ||
fb25b9e3 | 762 | return component_class_port_connected( |
6fa6bfa1 SM |
763 | self_component, |
764 | self_component_port_output, | |
765 | SWIGTYPE_p_bt_self_component_port_output, | |
766 | BT_PORT_TYPE_OUTPUT, | |
767 | other_port_input, | |
768 | SWIGTYPE_p_bt_port_input); | |
769 | } | |
770 | ||
efb177a7 | 771 | static |
fb25b9e3 PP |
772 | bt_component_class_port_connected_method_status |
773 | component_class_sink_input_port_connected( | |
6fa6bfa1 SM |
774 | bt_self_component_sink *self_component_sink, |
775 | bt_self_component_port_input *self_component_port_input, | |
776 | const bt_port_output *other_port_output) | |
777 | { | |
778 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
779 | ||
fb25b9e3 | 780 | return component_class_port_connected( |
6fa6bfa1 SM |
781 | self_component, |
782 | self_component_port_input, | |
783 | SWIGTYPE_p_bt_self_component_port_input, | |
784 | BT_PORT_TYPE_INPUT, | |
785 | other_port_output, | |
786 | SWIGTYPE_p_bt_port_output); | |
787 | } | |
788 | ||
efb177a7 | 789 | static |
fb25b9e3 PP |
790 | bt_component_class_sink_graph_is_configured_method_status |
791 | component_class_sink_graph_is_configured( | |
efb177a7 | 792 | bt_self_component_sink *self_component_sink) |
6fa6bfa1 SM |
793 | { |
794 | PyObject *py_comp = NULL; | |
795 | PyObject *py_method_result = NULL; | |
fb25b9e3 | 796 | bt_component_class_sink_graph_is_configured_method_status status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
797 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); |
798 | ||
799 | py_comp = bt_self_component_get_data(self_component); | |
800 | py_method_result = PyObject_CallMethod(py_comp, | |
deec48a6 | 801 | "_bt_graph_is_configured_from_native", NULL); |
6fa6bfa1 | 802 | BT_ASSERT(!py_method_result || py_method_result == Py_None); |
f8d71b59 | 803 | status = py_exc_to_status_component(self_component); |
6fa6bfa1 | 804 | Py_XDECREF(py_method_result); |
6fa6bfa1 SM |
805 | return status; |
806 | } | |
807 | ||
efb177a7 | 808 | static |
fb25b9e3 | 809 | bt_component_class_query_method_status component_class_query( |
6fa6bfa1 | 810 | const bt_component_class *component_class, |
f8d71b59 | 811 | bt_self_component_class *self_component_class, |
6fa6bfa1 SM |
812 | const bt_query_executor *query_executor, |
813 | const char *object, const bt_value *params, | |
83da519a | 814 | bt_logging_level log_level, |
6fa6bfa1 SM |
815 | const bt_value **result) |
816 | { | |
817 | PyObject *py_cls = NULL; | |
818 | PyObject *py_params_ptr = NULL; | |
819 | PyObject *py_query_exec_ptr = NULL; | |
820 | PyObject *py_query_func = NULL; | |
821 | PyObject *py_object = NULL; | |
822 | PyObject *py_results_addr = NULL; | |
fb25b9e3 | 823 | bt_component_class_query_method_status status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
824 | |
825 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
826 | if (!py_cls) { | |
827 | BT_LOGE("Cannot find Python class associated to native component class: " | |
828 | "comp-cls-addr=%p", component_class); | |
829 | goto error; | |
830 | } | |
831 | ||
832 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
833 | SWIGTYPE_p_bt_value, 0); | |
834 | if (!py_params_ptr) { | |
835 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
836 | goto error; | |
837 | } | |
838 | ||
839 | py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor), | |
840 | SWIGTYPE_p_bt_query_executor, 0); | |
841 | if (!py_query_exec_ptr) { | |
842 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
843 | goto error; | |
844 | } | |
845 | ||
846 | py_object = SWIG_FromCharPtr(object); | |
847 | if (!py_object) { | |
848 | BT_LOGE_STR("Failed to create a Python string."); | |
849 | goto error; | |
850 | } | |
851 | ||
852 | py_results_addr = PyObject_CallMethod(py_cls, | |
deec48a6 | 853 | "_bt_query_from_native", "(OOOi)", py_query_exec_ptr, |
83da519a | 854 | py_object, py_params_ptr, (int) log_level); |
6fa6bfa1 | 855 | if (!py_results_addr) { |
deec48a6 | 856 | BT_LOGW("Failed to call Python class's _bt_query_from_native() method: " |
6fa6bfa1 | 857 | "py-cls-addr=%p", py_cls); |
f8d71b59 | 858 | status = py_exc_to_status_component_class(self_component_class); |
6fa6bfa1 SM |
859 | goto end; |
860 | } | |
861 | ||
862 | /* | |
863 | * The returned object, on success, is an integer object | |
864 | * (PyLong) containing the address of a BT value object (new | |
865 | * reference). | |
866 | */ | |
c240a5d8 | 867 | *result = PyLong_AsVoidPtr(py_results_addr); |
6fa6bfa1 SM |
868 | BT_ASSERT(!PyErr_Occurred()); |
869 | BT_ASSERT(*result); | |
870 | goto end; | |
871 | ||
872 | error: | |
873 | PyErr_Clear(); | |
fb25b9e3 | 874 | status = __BT_FUNC_STATUS_ERROR; |
6fa6bfa1 SM |
875 | |
876 | end: | |
877 | Py_XDECREF(py_params_ptr); | |
878 | Py_XDECREF(py_query_exec_ptr); | |
879 | Py_XDECREF(py_query_func); | |
880 | Py_XDECREF(py_object); | |
881 | Py_XDECREF(py_results_addr); | |
882 | return status; | |
883 | } | |
884 | ||
efb177a7 | 885 | static |
fb25b9e3 | 886 | bt_component_class_query_method_status component_class_source_query( |
6fa6bfa1 SM |
887 | bt_self_component_class_source *self_component_class_source, |
888 | const bt_query_executor *query_executor, | |
889 | const char *object, const bt_value *params, | |
83da519a | 890 | bt_logging_level log_level, |
6fa6bfa1 SM |
891 | const bt_value **result) |
892 | { | |
893 | const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); | |
894 | const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); | |
f8d71b59 | 895 | bt_self_component_class *self_component_class = bt_self_component_class_source_as_self_component_class(self_component_class_source); |
fb25b9e3 | 896 | |
f8d71b59 | 897 | return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result); |
6fa6bfa1 SM |
898 | } |
899 | ||
efb177a7 | 900 | static |
fb25b9e3 | 901 | bt_component_class_query_method_status component_class_filter_query( |
6fa6bfa1 SM |
902 | bt_self_component_class_filter *self_component_class_filter, |
903 | const bt_query_executor *query_executor, | |
904 | const char *object, const bt_value *params, | |
83da519a | 905 | bt_logging_level log_level, |
6fa6bfa1 SM |
906 | const bt_value **result) |
907 | { | |
908 | const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); | |
909 | const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); | |
f8d71b59 | 910 | bt_self_component_class *self_component_class = bt_self_component_class_filter_as_self_component_class(self_component_class_filter); |
fb25b9e3 | 911 | |
f8d71b59 | 912 | return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result); |
6fa6bfa1 SM |
913 | } |
914 | ||
efb177a7 | 915 | static |
fb25b9e3 | 916 | bt_component_class_query_method_status component_class_sink_query( |
6fa6bfa1 SM |
917 | bt_self_component_class_sink *self_component_class_sink, |
918 | const bt_query_executor *query_executor, | |
919 | const char *object, const bt_value *params, | |
83da519a | 920 | bt_logging_level log_level, |
6fa6bfa1 SM |
921 | const bt_value **result) |
922 | { | |
923 | const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); | |
924 | const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); | |
f8d71b59 | 925 | bt_self_component_class *self_component_class = bt_self_component_class_sink_as_self_component_class(self_component_class_sink); |
fb25b9e3 | 926 | |
f8d71b59 | 927 | return component_class_query(component_class, self_component_class, query_executor, object, params, log_level, result); |
6fa6bfa1 SM |
928 | } |
929 | ||
efb177a7 | 930 | static |
fb25b9e3 PP |
931 | bt_component_class_message_iterator_init_method_status |
932 | component_class_message_iterator_init( | |
6fa6bfa1 SM |
933 | bt_self_message_iterator *self_message_iterator, |
934 | bt_self_component *self_component, | |
935 | bt_self_component_port_output *self_component_port_output) | |
936 | { | |
fb25b9e3 | 937 | bt_component_class_message_iterator_init_method_status status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
938 | PyObject *py_comp_cls = NULL; |
939 | PyObject *py_iter_cls = NULL; | |
940 | PyObject *py_iter_ptr = NULL; | |
a4dcfa96 | 941 | PyObject *py_component_port_output_ptr = NULL; |
6fa6bfa1 SM |
942 | PyObject *py_init_method_result = NULL; |
943 | PyObject *py_iter = NULL; | |
944 | PyObject *py_comp; | |
945 | ||
946 | py_comp = bt_self_component_get_data(self_component); | |
947 | ||
fa4c33e3 | 948 | /* Find user's Python message iterator class */ |
6fa6bfa1 SM |
949 | py_comp_cls = PyObject_GetAttrString(py_comp, "__class__"); |
950 | if (!py_comp_cls) { | |
951 | BT_LOGE_STR("Cannot get Python object's `__class__` attribute."); | |
f8d71b59 | 952 | goto python_error; |
6fa6bfa1 SM |
953 | } |
954 | ||
955 | py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls"); | |
956 | if (!py_iter_cls) { | |
957 | BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute."); | |
f8d71b59 | 958 | goto python_error; |
6fa6bfa1 SM |
959 | } |
960 | ||
961 | py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator), | |
962 | SWIGTYPE_p_bt_self_message_iterator, 0); | |
963 | if (!py_iter_ptr) { | |
f8d71b59 SM |
964 | const char *err = "Failed to create a SWIG pointer object."; |
965 | ||
966 | BT_LOGE_STR(err); | |
967 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( | |
968 | self_message_iterator, err); | |
6fa6bfa1 SM |
969 | goto error; |
970 | } | |
971 | ||
972 | /* | |
fa4c33e3 | 973 | * Create object with borrowed native message iterator |
6fa6bfa1 SM |
974 | * reference: |
975 | * | |
976 | * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr) | |
977 | */ | |
978 | py_iter = PyObject_CallMethod(py_iter_cls, "__new__", | |
979 | "(OO)", py_iter_cls, py_iter_ptr); | |
980 | if (!py_iter) { | |
981 | BT_LOGE("Failed to call Python class's __new__() method: " | |
982 | "py-cls-addr=%p", py_iter_cls); | |
f8d71b59 | 983 | goto python_error; |
6fa6bfa1 SM |
984 | } |
985 | ||
986 | /* | |
987 | * Initialize object: | |
988 | * | |
a4dcfa96 SM |
989 | * py_iter.__init__(self_output_port) |
990 | * | |
991 | * through the _init_for_native helper static method. | |
6fa6bfa1 SM |
992 | * |
993 | * At this point, py_iter._ptr is set, so this initialization | |
994 | * function has access to self._component (which gives it the | |
995 | * user Python component object from which the iterator was | |
996 | * created). | |
997 | */ | |
fb25b9e3 PP |
998 | py_component_port_output_ptr = SWIG_NewPointerObj( |
999 | SWIG_as_voidptr(self_component_port_output), | |
a4dcfa96 SM |
1000 | SWIGTYPE_p_bt_self_component_port_output, 0); |
1001 | if (!py_component_port_output_ptr) { | |
f8d71b59 SM |
1002 | const char *err = "Failed to create a SWIG pointer object."; |
1003 | ||
1004 | BT_LOGE_STR(err); | |
1005 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( | |
1006 | self_message_iterator, err); | |
a4dcfa96 SM |
1007 | goto error; |
1008 | } | |
1009 | ||
fb25b9e3 | 1010 | py_init_method_result = PyObject_CallMethod(py_iter, |
deec48a6 | 1011 | "_bt_init_from_native", "O", py_component_port_output_ptr); |
6fa6bfa1 | 1012 | if (!py_init_method_result) { |
f8d71b59 SM |
1013 | BT_LOGE_STR("User's __init__() method failed:"); |
1014 | goto python_error; | |
6fa6bfa1 SM |
1015 | } |
1016 | ||
1017 | /* | |
1018 | * Since the Python code can never instantiate a user-defined | |
fa4c33e3 SM |
1019 | * message iterator class, the native message iterator |
1020 | * object does NOT belong to a user Python message iterator | |
6fa6bfa1 | 1021 | * object (borrowed reference). However this Python object is |
fa4c33e3 | 1022 | * owned by this native message iterator object. |
6fa6bfa1 | 1023 | * |
fa4c33e3 SM |
1024 | * In the Python world, the lifetime of the native message |
1025 | * iterator is managed by a _GenericMessageIterator | |
6fa6bfa1 SM |
1026 | * instance: |
1027 | * | |
fa4c33e3 SM |
1028 | * _GenericMessageIterator instance: |
1029 | * owns a native bt_message_iterator object (iter) | |
6fa6bfa1 SM |
1030 | * owns a _UserMessageIterator instance (py_iter) |
1031 | * self._ptr is a borrowed reference to the | |
fa4c33e3 | 1032 | * native bt_private_connection_private_message_iterator |
6fa6bfa1 SM |
1033 | * object (iter) |
1034 | */ | |
1035 | bt_self_message_iterator_set_data(self_message_iterator, py_iter); | |
1036 | py_iter = NULL; | |
1037 | goto end; | |
1038 | ||
f8d71b59 SM |
1039 | python_error: |
1040 | /* Handling of errors that cause a Python exception to be set. */ | |
1041 | status = py_exc_to_status_message_iterator(self_message_iterator); | |
1042 | BT_ASSERT(status != __BT_FUNC_STATUS_OK); | |
1043 | goto end; | |
6fa6bfa1 | 1044 | |
f8d71b59 SM |
1045 | error: |
1046 | /* Handling of errors that don't cause a Python exception to be set. */ | |
1047 | status = __BT_FUNC_STATUS_ERROR; | |
6fa6bfa1 SM |
1048 | |
1049 | end: | |
f8d71b59 SM |
1050 | BT_ASSERT(!PyErr_Occurred()); |
1051 | ||
6fa6bfa1 SM |
1052 | Py_XDECREF(py_comp_cls); |
1053 | Py_XDECREF(py_iter_cls); | |
1054 | Py_XDECREF(py_iter_ptr); | |
a4dcfa96 | 1055 | Py_XDECREF(py_component_port_output_ptr); |
6fa6bfa1 SM |
1056 | Py_XDECREF(py_init_method_result); |
1057 | Py_XDECREF(py_iter); | |
1058 | return status; | |
1059 | } | |
1060 | ||
efb177a7 | 1061 | static |
fb25b9e3 PP |
1062 | bt_component_class_message_iterator_init_method_status |
1063 | component_class_source_message_iterator_init( | |
6fa6bfa1 SM |
1064 | bt_self_message_iterator *self_message_iterator, |
1065 | bt_self_component_source *self_component_source, | |
1066 | bt_self_component_port_output *self_component_port_output) | |
1067 | { | |
1068 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
fb25b9e3 PP |
1069 | |
1070 | return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); | |
6fa6bfa1 SM |
1071 | } |
1072 | ||
efb177a7 | 1073 | static |
fb25b9e3 PP |
1074 | bt_component_class_message_iterator_init_method_status |
1075 | component_class_filter_message_iterator_init( | |
6fa6bfa1 SM |
1076 | bt_self_message_iterator *self_message_iterator, |
1077 | bt_self_component_filter *self_component_filter, | |
1078 | bt_self_component_port_output *self_component_port_output) | |
1079 | { | |
1080 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
fb25b9e3 PP |
1081 | |
1082 | return component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); | |
6fa6bfa1 SM |
1083 | } |
1084 | ||
efb177a7 | 1085 | static |
fb25b9e3 | 1086 | void component_class_message_iterator_finalize( |
6fa6bfa1 SM |
1087 | bt_self_message_iterator *message_iterator) |
1088 | { | |
1089 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); | |
1090 | PyObject *py_method_result = NULL; | |
1091 | ||
1092 | BT_ASSERT(py_message_iter); | |
1093 | ||
1094 | /* Call user's _finalize() method */ | |
1095 | py_method_result = PyObject_CallMethod(py_message_iter, | |
1096 | "_finalize", NULL); | |
1097 | ||
1098 | if (PyErr_Occurred()) { | |
fb25b9e3 PP |
1099 | BT_LOGW("User's _finalize() method raised an exception: ignoring:"); |
1100 | logw_exception(); | |
6fa6bfa1 SM |
1101 | } |
1102 | ||
1103 | /* | |
1104 | * Ignore any exception raised by the _finalize() method because | |
1105 | * it won't change anything at this point: the component is | |
1106 | * being destroyed anyway. | |
1107 | */ | |
1108 | PyErr_Clear(); | |
1109 | Py_XDECREF(py_method_result); | |
1110 | Py_DECREF(py_message_iter); | |
1111 | } | |
1112 | ||
1113 | /* Valid for both sources and filters. */ | |
1114 | ||
efb177a7 | 1115 | static |
fb25b9e3 PP |
1116 | bt_component_class_message_iterator_next_method_status |
1117 | component_class_message_iterator_next( | |
efb177a7 SM |
1118 | bt_self_message_iterator *message_iterator, |
1119 | bt_message_array_const msgs, uint64_t capacity, | |
1120 | uint64_t *count) | |
6fa6bfa1 | 1121 | { |
fb25b9e3 | 1122 | bt_component_class_message_iterator_next_method_status status = __BT_FUNC_STATUS_OK; |
6fa6bfa1 SM |
1123 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); |
1124 | PyObject *py_method_result = NULL; | |
1125 | ||
1126 | BT_ASSERT(py_message_iter); | |
1127 | py_method_result = PyObject_CallMethod(py_message_iter, | |
deec48a6 | 1128 | "_bt_next_from_native", NULL); |
6fa6bfa1 | 1129 | if (!py_method_result) { |
f8d71b59 | 1130 | status = py_exc_to_status_message_iterator(message_iterator); |
fb25b9e3 | 1131 | BT_ASSERT(status != __BT_FUNC_STATUS_OK); |
6fa6bfa1 SM |
1132 | goto end; |
1133 | } | |
1134 | ||
1135 | /* | |
1136 | * The returned object, on success, is an integer object | |
fa4c33e3 | 1137 | * (PyLong) containing the address of a native message |
6fa6bfa1 SM |
1138 | * object (which is now ours). |
1139 | */ | |
c240a5d8 | 1140 | msgs[0] = PyLong_AsVoidPtr(py_method_result); |
6fa6bfa1 SM |
1141 | *count = 1; |
1142 | ||
1143 | /* Clear potential overflow error; should never happen */ | |
1144 | BT_ASSERT(!PyErr_Occurred()); | |
1145 | goto end; | |
1146 | ||
1147 | end: | |
1148 | Py_XDECREF(py_method_result); | |
1149 | return status; | |
1150 | } | |
1151 | ||
efb177a7 | 1152 | static |
fb25b9e3 PP |
1153 | bt_component_class_sink_consume_method_status |
1154 | component_class_sink_consume(bt_self_component_sink *self_component_sink) | |
6fa6bfa1 SM |
1155 | { |
1156 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
1157 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
1158 | PyObject *py_method_result = NULL; | |
fb25b9e3 | 1159 | bt_component_class_sink_consume_method_status status; |
6fa6bfa1 SM |
1160 | |
1161 | BT_ASSERT(py_comp); | |
1162 | py_method_result = PyObject_CallMethod(py_comp, | |
1163 | "_consume", NULL); | |
f8d71b59 | 1164 | status = py_exc_to_status_component(self_component); |
fb25b9e3 | 1165 | if (!py_method_result && status == __BT_FUNC_STATUS_OK) { |
6fa6bfa1 SM |
1166 | /* Pretty sure this should never happen, but just in case */ |
1167 | BT_LOGE("User's _consume() method failed without raising an exception: " | |
1168 | "status=%d", status); | |
fb25b9e3 | 1169 | status = __BT_FUNC_STATUS_ERROR; |
6fa6bfa1 SM |
1170 | } |
1171 | ||
1172 | Py_XDECREF(py_method_result); | |
1173 | return status; | |
1174 | } | |
1175 | ||
1176 | static | |
fb25b9e3 | 1177 | int component_class_set_help_and_desc( |
6fa6bfa1 SM |
1178 | bt_component_class *component_class, |
1179 | const char *description, const char *help) | |
1180 | { | |
1181 | int ret; | |
1182 | ||
1183 | if (description) { | |
1184 | ret = bt_component_class_set_description(component_class, description); | |
1185 | if (ret) { | |
1186 | BT_LOGE("Cannot set component class's description: " | |
1187 | "comp-cls-addr=%p", component_class); | |
1188 | goto end; | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | if (help) { | |
1193 | ret = bt_component_class_set_help(component_class, help); | |
1194 | if (ret) { | |
1195 | BT_LOGE("Cannot set component class's help text: " | |
1196 | "comp-cls-addr=%p", component_class); | |
1197 | goto end; | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | ret = 0; | |
1202 | ||
1203 | end: | |
1204 | return ret; | |
1205 | } | |
1206 | ||
1207 | static | |
fb25b9e3 | 1208 | bt_component_class_source *bt_bt2_component_class_source_create( |
6fa6bfa1 SM |
1209 | PyObject *py_cls, const char *name, const char *description, |
1210 | const char *help) | |
1211 | { | |
1212 | bt_component_class_source *component_class_source; | |
1213 | bt_component_class *component_class; | |
1214 | int ret; | |
1215 | ||
1216 | BT_ASSERT(py_cls); | |
6fa6bfa1 | 1217 | component_class_source = bt_component_class_source_create(name, |
fb25b9e3 | 1218 | component_class_message_iterator_next); |
6fa6bfa1 SM |
1219 | if (!component_class_source) { |
1220 | BT_LOGE_STR("Cannot create source component class."); | |
1221 | goto end; | |
1222 | } | |
1223 | ||
1224 | component_class = bt_component_class_source_as_component_class(component_class_source); | |
1225 | ||
fb25b9e3 | 1226 | if (component_class_set_help_and_desc(component_class, description, help)) { |
6fa6bfa1 SM |
1227 | goto end; |
1228 | } | |
1229 | ||
fb25b9e3 | 1230 | ret = bt_component_class_source_set_init_method(component_class_source, component_class_source_init); |
6fa6bfa1 | 1231 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1232 | ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize); |
39ddfa44 SM |
1233 | BT_ASSERT(ret == 0); |
1234 | ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(component_class_source, | |
fb25b9e3 | 1235 | component_class_can_seek_beginning); |
39ddfa44 SM |
1236 | BT_ASSERT(ret == 0); |
1237 | ret = bt_component_class_source_set_message_iterator_seek_beginning_method(component_class_source, | |
fb25b9e3 | 1238 | component_class_seek_beginning); |
6fa6bfa1 | 1239 | BT_ASSERT(ret == 0); |
6fa6bfa1 | 1240 | ret = bt_component_class_source_set_output_port_connected_method(component_class_source, |
fb25b9e3 | 1241 | component_class_source_output_port_connected); |
6fa6bfa1 | 1242 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1243 | ret = bt_component_class_source_set_query_method(component_class_source, component_class_source_query); |
6fa6bfa1 SM |
1244 | BT_ASSERT(ret == 0); |
1245 | ret = bt_component_class_source_set_message_iterator_init_method( | |
fb25b9e3 | 1246 | component_class_source, component_class_source_message_iterator_init); |
6fa6bfa1 SM |
1247 | BT_ASSERT(ret == 0); |
1248 | ret = bt_component_class_source_set_message_iterator_finalize_method( | |
fb25b9e3 | 1249 | component_class_source, component_class_message_iterator_finalize); |
6fa6bfa1 | 1250 | BT_ASSERT(ret == 0); |
6fa6bfa1 SM |
1251 | register_cc_ptr_to_py_cls(component_class, py_cls); |
1252 | ||
1253 | end: | |
1254 | return component_class_source; | |
1255 | } | |
1256 | ||
1257 | static | |
fb25b9e3 | 1258 | bt_component_class_filter *bt_bt2_component_class_filter_create( |
6fa6bfa1 SM |
1259 | PyObject *py_cls, const char *name, const char *description, |
1260 | const char *help) | |
1261 | { | |
1262 | bt_component_class *component_class; | |
1263 | bt_component_class_filter *component_class_filter; | |
1264 | int ret; | |
1265 | ||
1266 | BT_ASSERT(py_cls); | |
6fa6bfa1 | 1267 | component_class_filter = bt_component_class_filter_create(name, |
fb25b9e3 | 1268 | component_class_message_iterator_next); |
6fa6bfa1 SM |
1269 | if (!component_class_filter) { |
1270 | BT_LOGE_STR("Cannot create filter component class."); | |
1271 | goto end; | |
1272 | } | |
1273 | ||
1274 | component_class = bt_component_class_filter_as_component_class(component_class_filter); | |
1275 | ||
fb25b9e3 | 1276 | if (component_class_set_help_and_desc(component_class, description, help)) { |
6fa6bfa1 SM |
1277 | goto end; |
1278 | } | |
1279 | ||
fb25b9e3 | 1280 | ret = bt_component_class_filter_set_init_method(component_class_filter, component_class_filter_init); |
6fa6bfa1 | 1281 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1282 | ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize); |
6fa6bfa1 | 1283 | BT_ASSERT(ret == 0); |
39ddfa44 | 1284 | ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(component_class_filter, |
fb25b9e3 | 1285 | component_class_can_seek_beginning); |
39ddfa44 SM |
1286 | BT_ASSERT(ret == 0); |
1287 | ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(component_class_filter, | |
fb25b9e3 | 1288 | component_class_seek_beginning); |
39ddfa44 | 1289 | BT_ASSERT(ret == 0); |
6fa6bfa1 | 1290 | ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter, |
fb25b9e3 | 1291 | component_class_filter_input_port_connected); |
6fa6bfa1 SM |
1292 | BT_ASSERT(ret == 0); |
1293 | ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter, | |
fb25b9e3 | 1294 | component_class_filter_output_port_connected); |
6fa6bfa1 | 1295 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1296 | ret = bt_component_class_filter_set_query_method(component_class_filter, component_class_filter_query); |
6fa6bfa1 SM |
1297 | BT_ASSERT(ret == 0); |
1298 | ret = bt_component_class_filter_set_message_iterator_init_method( | |
fb25b9e3 | 1299 | component_class_filter, component_class_filter_message_iterator_init); |
6fa6bfa1 SM |
1300 | BT_ASSERT(ret == 0); |
1301 | ret = bt_component_class_filter_set_message_iterator_finalize_method( | |
fb25b9e3 | 1302 | component_class_filter, component_class_message_iterator_finalize); |
6fa6bfa1 | 1303 | BT_ASSERT(ret == 0); |
6fa6bfa1 SM |
1304 | register_cc_ptr_to_py_cls(component_class, py_cls); |
1305 | ||
1306 | end: | |
1307 | return component_class_filter; | |
1308 | } | |
1309 | ||
1310 | static | |
fb25b9e3 | 1311 | bt_component_class_sink *bt_bt2_component_class_sink_create( |
6fa6bfa1 SM |
1312 | PyObject *py_cls, const char *name, const char *description, |
1313 | const char *help) | |
1314 | { | |
1315 | bt_component_class_sink *component_class_sink; | |
1316 | bt_component_class *component_class; | |
1317 | int ret; | |
1318 | ||
1319 | BT_ASSERT(py_cls); | |
fb25b9e3 | 1320 | component_class_sink = bt_component_class_sink_create(name, component_class_sink_consume); |
6fa6bfa1 SM |
1321 | |
1322 | if (!component_class_sink) { | |
1323 | BT_LOGE_STR("Cannot create sink component class."); | |
1324 | goto end; | |
1325 | } | |
1326 | ||
1327 | component_class = bt_component_class_sink_as_component_class(component_class_sink); | |
1328 | ||
fb25b9e3 | 1329 | if (component_class_set_help_and_desc(component_class, description, help)) { |
6fa6bfa1 SM |
1330 | goto end; |
1331 | } | |
1332 | ||
fb25b9e3 | 1333 | ret = bt_component_class_sink_set_init_method(component_class_sink, component_class_sink_init); |
6fa6bfa1 | 1334 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1335 | ret = bt_component_class_sink_set_finalize_method(component_class_sink, component_class_sink_finalize); |
6fa6bfa1 | 1336 | BT_ASSERT(ret == 0); |
6fa6bfa1 | 1337 | ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink, |
fb25b9e3 | 1338 | component_class_sink_input_port_connected); |
6fa6bfa1 SM |
1339 | BT_ASSERT(ret == 0); |
1340 | ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink, | |
fb25b9e3 | 1341 | component_class_sink_graph_is_configured); |
6fa6bfa1 | 1342 | BT_ASSERT(ret == 0); |
fb25b9e3 | 1343 | ret = bt_component_class_sink_set_query_method(component_class_sink, component_class_sink_query); |
6fa6bfa1 | 1344 | BT_ASSERT(ret == 0); |
6fa6bfa1 SM |
1345 | register_cc_ptr_to_py_cls(component_class, py_cls); |
1346 | ||
1347 | end: | |
1348 | return component_class_sink; | |
1349 | } | |
1350 | %} | |
1351 | ||
fb25b9e3 | 1352 | struct bt_component_class_source *bt_bt2_component_class_source_create( |
6fa6bfa1 SM |
1353 | PyObject *py_cls, const char *name, const char *description, |
1354 | const char *help); | |
fb25b9e3 | 1355 | struct bt_component_class_filter *bt_bt2_component_class_filter_create( |
6fa6bfa1 SM |
1356 | PyObject *py_cls, const char *name, const char *description, |
1357 | const char *help); | |
fb25b9e3 | 1358 | struct bt_component_class_sink *bt_bt2_component_class_sink_create( |
6fa6bfa1 SM |
1359 | PyObject *py_cls, const char *name, const char *description, |
1360 | const char *help); | |
fb25b9e3 PP |
1361 | void bt_bt2_cc_init_from_bt2(void); |
1362 | void bt_bt2_cc_exit_handler(void); |