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