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