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 | ||
e4b56bb9 SM |
25 | %include <babeltrace2/graph/component-class-const.h> |
26 | %include <babeltrace2/graph/component-class-source-const.h> | |
27 | %include <babeltrace2/graph/component-class-source.h> | |
28 | %include <babeltrace2/graph/component-class-filter-const.h> | |
29 | %include <babeltrace2/graph/component-class-filter.h> | |
30 | %include <babeltrace2/graph/component-class-sink-const.h> | |
31 | %include <babeltrace2/graph/component-class-sink.h> | |
32 | %include <babeltrace2/graph/self-component-class-source.h> | |
33 | %include <babeltrace2/graph/self-component-class-filter.h> | |
34 | %include <babeltrace2/graph/self-component-class-sink.h> | |
6fa6bfa1 SM |
35 | |
36 | %{ | |
37 | /* | |
38 | * This hash table associates a BT component class object address to a | |
39 | * user-defined Python class (PyObject *). The keys and values are NOT | |
40 | * owned by this hash table. The Python class objects are owned by the | |
41 | * Python module, which should not be unloaded until it is not possible | |
42 | * to create a user Python component anyway. | |
43 | * | |
44 | * This hash table is written to when a user-defined Python component | |
45 | * class is created by one of the bt_py3_component_class_*_create() | |
46 | * functions. | |
47 | * | |
48 | * This function is read from when a user calls bt_component_create() | |
49 | * with a component class pointer created by one of the functions above. | |
50 | * In this case, the original Python class needs to be found to | |
51 | * instantiate it and associate the created Python component object with | |
52 | * a BT component object instance. | |
53 | */ | |
54 | ||
55 | static GHashTable *bt_cc_ptr_to_py_cls; | |
56 | ||
efb177a7 SM |
57 | static |
58 | void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc, | |
6fa6bfa1 SM |
59 | PyObject *py_cls) |
60 | { | |
61 | if (!bt_cc_ptr_to_py_cls) { | |
62 | /* | |
63 | * Lazy-initializing this GHashTable because GLib | |
64 | * might not be initialized yet and it needs to be | |
65 | * before we call g_hash_table_new() | |
66 | */ | |
67 | BT_LOGD_STR("Creating native component class to Python component class hash table."); | |
68 | bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal); | |
69 | BT_ASSERT(bt_cc_ptr_to_py_cls); | |
70 | } | |
71 | ||
72 | g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc, | |
73 | (gpointer) py_cls); | |
74 | } | |
75 | ||
efb177a7 SM |
76 | static |
77 | PyObject *lookup_cc_ptr_to_py_cls(const bt_component_class *bt_cc) | |
6fa6bfa1 SM |
78 | { |
79 | if (!bt_cc_ptr_to_py_cls) { | |
80 | BT_LOGW("Cannot look up Python component class because hash table is NULL: " | |
81 | "comp-cls-addr=%p", bt_cc); | |
82 | return NULL; | |
83 | } | |
84 | ||
85 | return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls, | |
86 | (gconstpointer) bt_cc); | |
87 | } | |
88 | ||
89 | ||
90 | /* | |
91 | * Useful Python objects. | |
92 | */ | |
93 | ||
94 | static PyObject *py_mod_bt2 = NULL; | |
95 | static PyObject *py_mod_bt2_exc_error_type = NULL; | |
96 | static PyObject *py_mod_bt2_exc_try_again_type = NULL; | |
97 | static PyObject *py_mod_bt2_exc_stop_type = NULL; | |
fa4c33e3 | 98 | static PyObject *py_mod_bt2_exc_msg_iter_canceled_type = NULL; |
6fa6bfa1 SM |
99 | static PyObject *py_mod_bt2_exc_invalid_query_object_type = NULL; |
100 | static PyObject *py_mod_bt2_exc_invalid_query_params_type = NULL; | |
101 | ||
efb177a7 SM |
102 | static |
103 | void bt_py3_cc_init_from_bt2(void) | |
6fa6bfa1 SM |
104 | { |
105 | /* | |
106 | * This is called once the bt2 package is loaded. | |
107 | * | |
108 | * Those modules and functions are needed while the package is | |
109 | * used. Loading them here is safe because we know the bt2 | |
110 | * package is imported, and we know that the user cannot use the | |
111 | * code here without importing bt2 first. | |
112 | */ | |
113 | py_mod_bt2 = PyImport_ImportModule("bt2"); | |
114 | BT_ASSERT(py_mod_bt2); | |
115 | py_mod_bt2_exc_error_type = | |
116 | PyObject_GetAttrString(py_mod_bt2, "Error"); | |
117 | BT_ASSERT(py_mod_bt2_exc_error_type); | |
118 | py_mod_bt2_exc_try_again_type = | |
119 | PyObject_GetAttrString(py_mod_bt2, "TryAgain"); | |
120 | BT_ASSERT(py_mod_bt2_exc_try_again_type); | |
121 | py_mod_bt2_exc_stop_type = | |
122 | PyObject_GetAttrString(py_mod_bt2, "Stop"); | |
123 | BT_ASSERT(py_mod_bt2_exc_stop_type); | |
6fa6bfa1 SM |
124 | py_mod_bt2_exc_invalid_query_object_type = |
125 | PyObject_GetAttrString(py_mod_bt2, "InvalidQueryObject"); | |
126 | BT_ASSERT(py_mod_bt2_exc_invalid_query_object_type); | |
127 | py_mod_bt2_exc_invalid_query_params_type = | |
128 | PyObject_GetAttrString(py_mod_bt2, "InvalidQueryParams"); | |
129 | BT_ASSERT(py_mod_bt2_exc_invalid_query_params_type); | |
130 | } | |
131 | ||
efb177a7 SM |
132 | static |
133 | void bt_py3_cc_exit_handler(void) | |
6fa6bfa1 SM |
134 | { |
135 | /* | |
136 | * This is an exit handler (set by the bt2 package). | |
137 | * | |
138 | * We only give back the references that we took in | |
139 | * bt_py3_cc_init_from_bt2() here. The global variables continue | |
140 | * to exist for the code of this file, but they are now borrowed | |
141 | * references. If this code is executed, it means that somehow | |
142 | * the modules are still loaded, so it should be safe to use | |
143 | * them even without a strong reference. | |
144 | * | |
145 | * We cannot do this in the library's destructor because it | |
146 | * gets executed once Python is already finalized. | |
147 | */ | |
148 | Py_XDECREF(py_mod_bt2); | |
149 | Py_XDECREF(py_mod_bt2_exc_error_type); | |
150 | Py_XDECREF(py_mod_bt2_exc_try_again_type); | |
151 | Py_XDECREF(py_mod_bt2_exc_stop_type); | |
fa4c33e3 | 152 | Py_XDECREF(py_mod_bt2_exc_msg_iter_canceled_type); |
6fa6bfa1 SM |
153 | Py_XDECREF(py_mod_bt2_exc_invalid_query_object_type); |
154 | Py_XDECREF(py_mod_bt2_exc_invalid_query_params_type); | |
155 | } | |
156 | ||
157 | ||
158 | /* Library destructor */ | |
159 | ||
160 | __attribute__((destructor)) | |
efb177a7 SM |
161 | static |
162 | void bt_py3_native_comp_class_dtor(void) { | |
6fa6bfa1 SM |
163 | /* Destroy component class association hash table */ |
164 | if (bt_cc_ptr_to_py_cls) { | |
165 | BT_LOGD_STR("Destroying native component class to Python component class hash table."); | |
166 | g_hash_table_destroy(bt_cc_ptr_to_py_cls); | |
167 | } | |
168 | } | |
169 | ||
170 | ||
171 | // TODO: maybe we can wrap code in the Python methods (e.g. _query_from_native) | |
172 | // in a try catch and print the error there instead, it would be simpler. | |
173 | static | |
174 | void bt2_py_loge_exception(void) | |
175 | { | |
176 | PyObject *type = NULL; | |
177 | PyObject *value = NULL; | |
178 | PyObject *traceback = NULL; | |
179 | PyObject *traceback_module = NULL; | |
180 | PyObject *format_exception_func = NULL; | |
181 | PyObject *exc_str_list = NULL; | |
182 | GString *msg_buf = NULL; | |
183 | Py_ssize_t i; | |
184 | ||
185 | BT_ASSERT(PyErr_Occurred() != NULL); | |
186 | ||
187 | PyErr_Fetch(&type, &value, &traceback); | |
188 | ||
189 | BT_ASSERT(type != NULL); | |
190 | ||
191 | /* | |
192 | * traceback can be NULL, when we fail to call a Python function from the | |
aa23edf1 | 193 | * native code (there is no Python stack at that point). E.g.: |
6fa6bfa1 | 194 | * |
aa23edf1 | 195 | * TypeError: _query_from_native() takes 5 positional arguments but 8 were given |
6fa6bfa1 SM |
196 | */ |
197 | ||
198 | ||
199 | /* Make sure `value` is what we expected - an instance of `type`. */ | |
200 | PyErr_NormalizeException(&type, &value, &traceback); | |
201 | ||
202 | traceback_module = PyImport_ImportModule("traceback"); | |
203 | if (!traceback_module) { | |
204 | BT_LOGE_STR("Failed to log Python exception (could not import traceback module)."); | |
205 | goto end; | |
206 | } | |
207 | ||
208 | format_exception_func = PyObject_GetAttrString(traceback_module, | |
209 | traceback ? "format_exception" : "format_exception_only"); | |
210 | if (!format_exception_func) { | |
211 | BT_LOGE_STR("Failed to log Python exception (could not find format_exception)."); | |
212 | goto end; | |
213 | } | |
214 | ||
215 | if (!PyCallable_Check(format_exception_func)) { | |
216 | BT_LOGE_STR("Failed to log Python exception (format_exception is not callable)."); | |
217 | goto end; | |
218 | } | |
219 | ||
220 | exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func, type, value, traceback, NULL); | |
221 | if (!exc_str_list) { | |
222 | PyErr_Print(); | |
223 | BT_LOGE_STR("Failed to log Python exception (call to format_exception failed)."); | |
224 | goto end; | |
225 | } | |
226 | ||
227 | msg_buf = g_string_new(NULL); | |
228 | ||
229 | for (i = 0; i < PyList_Size(exc_str_list); i++) { | |
230 | PyObject *exc_str = PyList_GetItem(exc_str_list, i); | |
231 | const char *str = PyUnicode_AsUTF8(exc_str); | |
232 | if (!str) { | |
233 | BT_LOGE_STR("Failed to log Python exception (failed to convert exception to string)."); | |
234 | goto end; | |
235 | } | |
236 | ||
237 | g_string_append(msg_buf, str); | |
238 | } | |
239 | ||
240 | BT_LOGE_STR(msg_buf->str); | |
241 | ||
242 | end: | |
243 | if (msg_buf) { | |
244 | g_string_free(msg_buf, TRUE); | |
245 | } | |
246 | Py_XDECREF(exc_str_list); | |
247 | Py_XDECREF(format_exception_func); | |
248 | Py_XDECREF(traceback_module); | |
249 | ||
250 | /* PyErr_Restore takes our references. */ | |
251 | PyErr_Restore(type, value, traceback); | |
252 | } | |
253 | ||
efb177a7 SM |
254 | static |
255 | bt_self_component_status bt_py3_exc_to_self_component_status(void) | |
6fa6bfa1 SM |
256 | { |
257 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; | |
258 | PyObject *exc = PyErr_Occurred(); | |
259 | ||
260 | if (!exc) { | |
261 | goto end; | |
262 | } | |
263 | ||
264 | if (PyErr_GivenExceptionMatches(exc, | |
265 | py_mod_bt2_exc_try_again_type)) { | |
266 | status = BT_SELF_COMPONENT_STATUS_AGAIN; | |
267 | } else if (PyErr_GivenExceptionMatches(exc, | |
268 | py_mod_bt2_exc_stop_type)) { | |
269 | status = BT_SELF_COMPONENT_STATUS_END; | |
6fa6bfa1 SM |
270 | } else { |
271 | bt2_py_loge_exception(); | |
272 | status = BT_SELF_COMPONENT_STATUS_ERROR; | |
273 | } | |
274 | ||
275 | end: | |
276 | PyErr_Clear(); | |
277 | return status; | |
278 | } | |
279 | ||
280 | /* Component class proxy methods (delegate to the attached Python object) */ | |
281 | ||
efb177a7 SM |
282 | static |
283 | bt_self_message_iterator_status bt_py3_exc_to_self_message_iterator_status(void) | |
6fa6bfa1 SM |
284 | { |
285 | enum bt_self_message_iterator_status status = | |
286 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
287 | PyObject *exc = PyErr_Occurred(); | |
288 | ||
289 | if (!exc) { | |
290 | goto end; | |
291 | } | |
292 | ||
293 | if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_stop_type)) { | |
c348a6fd | 294 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_END; |
6fa6bfa1 | 295 | } else if (PyErr_GivenExceptionMatches(exc, py_mod_bt2_exc_try_again_type)) { |
c348a6fd | 296 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN; |
6fa6bfa1 SM |
297 | } else { |
298 | bt2_py_loge_exception(); | |
c348a6fd | 299 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
6fa6bfa1 SM |
300 | } |
301 | ||
302 | end: | |
303 | PyErr_Clear(); | |
304 | return status; | |
305 | } | |
306 | ||
efb177a7 SM |
307 | static |
308 | enum bt_query_status bt_py3_exc_to_query_status(void) | |
6fa6bfa1 SM |
309 | { |
310 | enum bt_query_status status = BT_QUERY_STATUS_OK; | |
311 | PyObject *exc = PyErr_Occurred(); | |
312 | ||
313 | if (!exc) { | |
314 | goto end; | |
315 | } | |
316 | ||
317 | if (PyErr_GivenExceptionMatches(exc, | |
318 | py_mod_bt2_exc_invalid_query_object_type)) { | |
319 | status = BT_QUERY_STATUS_INVALID_OBJECT; | |
320 | } else if (PyErr_GivenExceptionMatches(exc, | |
321 | py_mod_bt2_exc_invalid_query_params_type)) { | |
322 | status = BT_QUERY_STATUS_INVALID_PARAMS; | |
323 | } else if (PyErr_GivenExceptionMatches(exc, | |
324 | py_mod_bt2_exc_try_again_type)) { | |
325 | status = BT_QUERY_STATUS_AGAIN; | |
326 | } else { | |
327 | bt2_py_loge_exception(); | |
328 | status = BT_QUERY_STATUS_ERROR; | |
329 | } | |
330 | ||
331 | end: | |
332 | PyErr_Clear(); | |
333 | return status; | |
334 | } | |
335 | ||
efb177a7 SM |
336 | static |
337 | bt_self_component_status bt_py3_component_class_init( | |
6fa6bfa1 SM |
338 | bt_self_component *self_component, |
339 | void *self_component_v, | |
340 | swig_type_info *self_comp_cls_type_swig_type, | |
341 | const bt_value *params, | |
342 | void *init_method_data) | |
343 | { | |
344 | const bt_component *component = bt_self_component_as_component(self_component); | |
345 | const bt_component_class *component_class = bt_component_borrow_class_const(component); | |
346 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; | |
347 | PyObject *py_cls = NULL; | |
348 | PyObject *py_comp = NULL; | |
349 | PyObject *py_params_ptr = NULL; | |
350 | PyObject *py_comp_ptr = NULL; | |
351 | ||
352 | (void) init_method_data; | |
353 | ||
354 | BT_ASSERT(self_component); | |
355 | BT_ASSERT(self_component_v); | |
356 | BT_ASSERT(self_comp_cls_type_swig_type); | |
357 | ||
358 | /* | |
359 | * Get the user-defined Python class which created this | |
360 | * component's class in the first place (borrowed | |
361 | * reference). | |
362 | */ | |
363 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
364 | if (!py_cls) { | |
365 | BT_LOGE("Cannot find Python class associated to native component class: " | |
366 | "comp-cls-addr=%p", component_class); | |
367 | goto error; | |
368 | } | |
369 | ||
370 | /* Parameters pointer -> SWIG pointer Python object */ | |
371 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
372 | SWIGTYPE_p_bt_value, 0); | |
373 | if (!py_params_ptr) { | |
374 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
375 | goto error; | |
376 | } | |
377 | ||
378 | py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_v), | |
379 | self_comp_cls_type_swig_type, 0); | |
380 | if (!py_comp_ptr) { | |
381 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
382 | goto error; | |
383 | } | |
384 | ||
385 | /* | |
386 | * Do the equivalent of this: | |
387 | * | |
388 | * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr) | |
389 | * | |
390 | * _UserComponentType._init_from_native() calls the Python | |
391 | * component object's __init__() function. | |
392 | */ | |
393 | py_comp = PyObject_CallMethod(py_cls, | |
394 | "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr); | |
395 | if (!py_comp) { | |
396 | bt2_py_loge_exception(); | |
397 | BT_LOGE("Failed to call Python class's _init_from_native() method: " | |
398 | "py-cls-addr=%p", py_cls); | |
399 | ||
400 | goto error; | |
401 | } | |
402 | ||
403 | /* | |
404 | * Our user Python component object is now fully created and | |
405 | * initialized by the user. Since we just created it, this | |
406 | * native component is its only (persistent) owner. | |
407 | */ | |
408 | bt_self_component_set_data(self_component, py_comp); | |
409 | py_comp = NULL; | |
410 | goto end; | |
411 | ||
412 | error: | |
413 | status = BT_SELF_COMPONENT_STATUS_ERROR; | |
414 | ||
415 | /* | |
416 | * Clear any exception: we're returning a bad status anyway. If | |
417 | * this call originated from Python (creation from a plugin's | |
418 | * component class, for example), then the user gets an | |
419 | * appropriate creation error. | |
420 | */ | |
421 | PyErr_Clear(); | |
422 | ||
423 | end: | |
424 | Py_XDECREF(py_comp); | |
425 | Py_XDECREF(py_params_ptr); | |
426 | Py_XDECREF(py_comp_ptr); | |
427 | return status; | |
428 | } | |
429 | ||
430 | /* | |
431 | * Method of bt_component_class_source to initialize a bt_self_component_source | |
432 | * of that class. | |
433 | */ | |
434 | ||
efb177a7 SM |
435 | static |
436 | bt_self_component_status bt_py3_component_class_source_init( | |
437 | bt_self_component_source *self_component_source, | |
6fa6bfa1 SM |
438 | const bt_value *params, void *init_method_data) |
439 | { | |
440 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
441 | return bt_py3_component_class_init( | |
442 | self_component, | |
443 | self_component_source, | |
444 | SWIGTYPE_p_bt_self_component_source, | |
445 | params, init_method_data); | |
446 | } | |
447 | ||
efb177a7 SM |
448 | static |
449 | bt_self_component_status bt_py3_component_class_filter_init( | |
450 | bt_self_component_filter *self_component_filter, | |
6fa6bfa1 SM |
451 | const bt_value *params, void *init_method_data) |
452 | { | |
453 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
454 | return bt_py3_component_class_init( | |
455 | self_component, | |
456 | self_component_filter, | |
457 | SWIGTYPE_p_bt_self_component_filter, | |
458 | params, init_method_data); | |
459 | } | |
460 | ||
efb177a7 SM |
461 | static |
462 | bt_self_component_status bt_py3_component_class_sink_init( | |
463 | bt_self_component_sink *self_component_sink, | |
6fa6bfa1 SM |
464 | const bt_value *params, void *init_method_data) |
465 | { | |
466 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
467 | return bt_py3_component_class_init( | |
468 | self_component, | |
469 | self_component_sink, | |
470 | SWIGTYPE_p_bt_self_component_sink, | |
471 | params, init_method_data); | |
472 | } | |
473 | ||
efb177a7 SM |
474 | static |
475 | void bt_py3_component_class_finalize(bt_self_component *self_component) | |
6fa6bfa1 SM |
476 | { |
477 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
478 | BT_ASSERT(py_comp); | |
479 | ||
480 | /* Call user's _finalize() method */ | |
481 | PyObject *py_method_result = PyObject_CallMethod(py_comp, | |
482 | "_finalize", NULL); | |
483 | ||
484 | if (PyErr_Occurred()) { | |
485 | BT_LOGW("User's _finalize() method raised an exception: ignoring."); | |
486 | } | |
487 | ||
488 | /* | |
489 | * Ignore any exception raised by the _finalize() method because | |
490 | * it won't change anything at this point: the component is | |
491 | * being destroyed anyway. | |
492 | */ | |
493 | PyErr_Clear(); | |
494 | Py_XDECREF(py_method_result); | |
495 | Py_DECREF(py_comp); | |
496 | } | |
497 | ||
efb177a7 SM |
498 | static |
499 | void bt_py3_component_class_source_finalize(bt_self_component_source *self_component_source) | |
6fa6bfa1 SM |
500 | { |
501 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
502 | bt_py3_component_class_finalize(self_component); | |
503 | } | |
504 | ||
efb177a7 SM |
505 | static |
506 | void bt_py3_component_class_filter_finalize(bt_self_component_filter *self_component_filter) | |
6fa6bfa1 SM |
507 | { |
508 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
509 | bt_py3_component_class_finalize(self_component); | |
510 | } | |
511 | ||
efb177a7 SM |
512 | static |
513 | void bt_py3_component_class_sink_finalize(bt_self_component_sink *self_component_sink) | |
6fa6bfa1 SM |
514 | { |
515 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
516 | bt_py3_component_class_finalize(self_component); | |
517 | } | |
518 | ||
efb177a7 SM |
519 | static |
520 | bt_self_component_status bt_py3_component_class_port_connected( | |
6fa6bfa1 SM |
521 | bt_self_component *self_component, |
522 | void *self_component_port, | |
523 | swig_type_info *self_component_port_swig_type, | |
524 | bt_port_type self_component_port_type, | |
525 | const void *other_port, | |
526 | swig_type_info *other_port_swig_type) | |
527 | { | |
528 | bt_self_component_status status; | |
529 | PyObject *py_comp = NULL; | |
530 | PyObject *py_self_port_ptr = NULL; | |
531 | PyObject *py_other_port_ptr = NULL; | |
532 | PyObject *py_method_result = NULL; | |
533 | ||
534 | py_comp = bt_self_component_get_data(self_component); | |
535 | BT_ASSERT(py_comp); | |
536 | ||
537 | py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port), | |
538 | self_component_port_swig_type, 0); | |
539 | if (!py_self_port_ptr) { | |
540 | BT_LOGF_STR("Failed to create a SWIG pointer object."); | |
541 | status = BT_SELF_COMPONENT_STATUS_NOMEM; | |
542 | goto end; | |
543 | } | |
544 | ||
545 | py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port), | |
546 | other_port_swig_type, 0); | |
547 | if (!py_other_port_ptr) { | |
548 | BT_LOGF_STR("Failed to create a SWIG pointer object."); | |
549 | status = BT_SELF_COMPONENT_STATUS_NOMEM; | |
550 | goto end; } | |
551 | ||
552 | py_method_result = PyObject_CallMethod(py_comp, | |
553 | "_port_connected_from_native", "(OiO)", py_self_port_ptr, | |
554 | self_component_port_type, py_other_port_ptr); | |
555 | ||
556 | BT_ASSERT(!py_method_result || py_method_result == Py_None); | |
557 | ||
558 | status = bt_py3_exc_to_self_component_status(); | |
559 | ||
560 | end: | |
561 | Py_XDECREF(py_self_port_ptr); | |
562 | Py_XDECREF(py_other_port_ptr); | |
563 | Py_XDECREF(py_method_result); | |
564 | ||
565 | return status; | |
566 | } | |
567 | ||
efb177a7 SM |
568 | static |
569 | bt_self_component_status bt_py3_component_class_source_output_port_connected( | |
6fa6bfa1 SM |
570 | bt_self_component_source *self_component_source, |
571 | bt_self_component_port_output *self_component_port_output, | |
572 | const bt_port_input *other_port_input) | |
573 | { | |
574 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
575 | ||
576 | return bt_py3_component_class_port_connected( | |
577 | self_component, | |
578 | self_component_port_output, | |
579 | SWIGTYPE_p_bt_self_component_port_output, | |
580 | BT_PORT_TYPE_OUTPUT, | |
581 | other_port_input, | |
582 | SWIGTYPE_p_bt_port_input); | |
583 | } | |
584 | ||
efb177a7 SM |
585 | static |
586 | bt_self_component_status bt_py3_component_class_filter_input_port_connected( | |
6fa6bfa1 SM |
587 | bt_self_component_filter *self_component_filter, |
588 | bt_self_component_port_input *self_component_port_input, | |
589 | const bt_port_output *other_port_output) | |
590 | { | |
591 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
592 | ||
593 | return bt_py3_component_class_port_connected( | |
594 | self_component, | |
595 | self_component_port_input, | |
596 | SWIGTYPE_p_bt_self_component_port_input, | |
597 | BT_PORT_TYPE_INPUT, | |
598 | other_port_output, | |
599 | SWIGTYPE_p_bt_port_output); | |
600 | } | |
601 | ||
efb177a7 SM |
602 | static |
603 | bt_self_component_status bt_py3_component_class_filter_output_port_connected( | |
6fa6bfa1 SM |
604 | bt_self_component_filter *self_component_filter, |
605 | bt_self_component_port_output *self_component_port_output, | |
606 | const bt_port_input *other_port_input) | |
607 | { | |
608 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
609 | ||
610 | return bt_py3_component_class_port_connected( | |
611 | self_component, | |
612 | self_component_port_output, | |
613 | SWIGTYPE_p_bt_self_component_port_output, | |
614 | BT_PORT_TYPE_OUTPUT, | |
615 | other_port_input, | |
616 | SWIGTYPE_p_bt_port_input); | |
617 | } | |
618 | ||
efb177a7 SM |
619 | static |
620 | bt_self_component_status bt_py3_component_class_sink_input_port_connected( | |
6fa6bfa1 SM |
621 | bt_self_component_sink *self_component_sink, |
622 | bt_self_component_port_input *self_component_port_input, | |
623 | const bt_port_output *other_port_output) | |
624 | { | |
625 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
626 | ||
627 | return bt_py3_component_class_port_connected( | |
628 | self_component, | |
629 | self_component_port_input, | |
630 | SWIGTYPE_p_bt_self_component_port_input, | |
631 | BT_PORT_TYPE_INPUT, | |
632 | other_port_output, | |
633 | SWIGTYPE_p_bt_port_output); | |
634 | } | |
635 | ||
efb177a7 SM |
636 | static |
637 | bt_self_component_status bt_py3_component_class_sink_graph_is_configured( | |
638 | bt_self_component_sink *self_component_sink) | |
6fa6bfa1 SM |
639 | { |
640 | PyObject *py_comp = NULL; | |
641 | PyObject *py_method_result = NULL; | |
642 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; | |
643 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
644 | ||
645 | py_comp = bt_self_component_get_data(self_component); | |
646 | py_method_result = PyObject_CallMethod(py_comp, | |
647 | "_graph_is_configured_from_native", NULL); | |
648 | ||
649 | BT_ASSERT(!py_method_result || py_method_result == Py_None); | |
650 | ||
651 | status = bt_py3_exc_to_self_component_status(); | |
652 | ||
653 | Py_XDECREF(py_method_result); | |
654 | ||
655 | return status; | |
656 | } | |
657 | ||
efb177a7 SM |
658 | static |
659 | bt_query_status bt_py3_component_class_query( | |
6fa6bfa1 SM |
660 | const bt_component_class *component_class, |
661 | const bt_query_executor *query_executor, | |
662 | const char *object, const bt_value *params, | |
83da519a | 663 | bt_logging_level log_level, |
6fa6bfa1 SM |
664 | const bt_value **result) |
665 | { | |
666 | PyObject *py_cls = NULL; | |
667 | PyObject *py_params_ptr = NULL; | |
668 | PyObject *py_query_exec_ptr = NULL; | |
669 | PyObject *py_query_func = NULL; | |
670 | PyObject *py_object = NULL; | |
671 | PyObject *py_results_addr = NULL; | |
672 | bt_query_status status = BT_QUERY_STATUS_OK; | |
673 | ||
674 | py_cls = lookup_cc_ptr_to_py_cls(component_class); | |
675 | if (!py_cls) { | |
676 | BT_LOGE("Cannot find Python class associated to native component class: " | |
677 | "comp-cls-addr=%p", component_class); | |
678 | goto error; | |
679 | } | |
680 | ||
681 | py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params), | |
682 | SWIGTYPE_p_bt_value, 0); | |
683 | if (!py_params_ptr) { | |
684 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
685 | goto error; | |
686 | } | |
687 | ||
688 | py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_executor), | |
689 | SWIGTYPE_p_bt_query_executor, 0); | |
690 | if (!py_query_exec_ptr) { | |
691 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
692 | goto error; | |
693 | } | |
694 | ||
695 | py_object = SWIG_FromCharPtr(object); | |
696 | if (!py_object) { | |
697 | BT_LOGE_STR("Failed to create a Python string."); | |
698 | goto error; | |
699 | } | |
700 | ||
701 | py_results_addr = PyObject_CallMethod(py_cls, | |
83da519a PP |
702 | "_query_from_native", "(OOOi)", py_query_exec_ptr, |
703 | py_object, py_params_ptr, (int) log_level); | |
6fa6bfa1 SM |
704 | |
705 | if (!py_results_addr) { | |
706 | BT_LOGE("Failed to call Python class's _query_from_native() method: " | |
707 | "py-cls-addr=%p", py_cls); | |
708 | status = bt_py3_exc_to_query_status(); | |
709 | goto end; | |
710 | } | |
711 | ||
712 | /* | |
713 | * The returned object, on success, is an integer object | |
714 | * (PyLong) containing the address of a BT value object (new | |
715 | * reference). | |
716 | */ | |
c240a5d8 | 717 | *result = PyLong_AsVoidPtr(py_results_addr); |
6fa6bfa1 SM |
718 | BT_ASSERT(!PyErr_Occurred()); |
719 | BT_ASSERT(*result); | |
720 | goto end; | |
721 | ||
722 | error: | |
723 | PyErr_Clear(); | |
724 | status = BT_QUERY_STATUS_ERROR; | |
725 | ||
726 | end: | |
727 | Py_XDECREF(py_params_ptr); | |
728 | Py_XDECREF(py_query_exec_ptr); | |
729 | Py_XDECREF(py_query_func); | |
730 | Py_XDECREF(py_object); | |
731 | Py_XDECREF(py_results_addr); | |
732 | return status; | |
733 | } | |
734 | ||
efb177a7 SM |
735 | static |
736 | bt_query_status bt_py3_component_class_source_query( | |
6fa6bfa1 SM |
737 | bt_self_component_class_source *self_component_class_source, |
738 | const bt_query_executor *query_executor, | |
739 | const char *object, const bt_value *params, | |
83da519a | 740 | bt_logging_level log_level, |
6fa6bfa1 SM |
741 | const bt_value **result) |
742 | { | |
743 | const bt_component_class_source *component_class_source = bt_self_component_class_source_as_component_class_source(self_component_class_source); | |
744 | const bt_component_class *component_class = bt_component_class_source_as_component_class_const(component_class_source); | |
83da519a | 745 | return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result); |
6fa6bfa1 SM |
746 | } |
747 | ||
efb177a7 SM |
748 | static |
749 | bt_query_status bt_py3_component_class_filter_query( | |
6fa6bfa1 SM |
750 | bt_self_component_class_filter *self_component_class_filter, |
751 | const bt_query_executor *query_executor, | |
752 | const char *object, const bt_value *params, | |
83da519a | 753 | bt_logging_level log_level, |
6fa6bfa1 SM |
754 | const bt_value **result) |
755 | { | |
756 | const bt_component_class_filter *component_class_filter = bt_self_component_class_filter_as_component_class_filter(self_component_class_filter); | |
757 | const bt_component_class *component_class = bt_component_class_filter_as_component_class_const(component_class_filter); | |
83da519a | 758 | return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result); |
6fa6bfa1 SM |
759 | } |
760 | ||
efb177a7 SM |
761 | static |
762 | bt_query_status bt_py3_component_class_sink_query( | |
6fa6bfa1 SM |
763 | bt_self_component_class_sink *self_component_class_sink, |
764 | const bt_query_executor *query_executor, | |
765 | const char *object, const bt_value *params, | |
83da519a | 766 | bt_logging_level log_level, |
6fa6bfa1 SM |
767 | const bt_value **result) |
768 | { | |
769 | const bt_component_class_sink *component_class_sink = bt_self_component_class_sink_as_component_class_sink(self_component_class_sink); | |
770 | const bt_component_class *component_class = bt_component_class_sink_as_component_class_const(component_class_sink); | |
83da519a | 771 | return bt_py3_component_class_query(component_class, query_executor, object, params, log_level, result); |
6fa6bfa1 SM |
772 | } |
773 | ||
efb177a7 SM |
774 | static |
775 | bt_self_message_iterator_status bt_py3_component_class_message_iterator_init( | |
6fa6bfa1 SM |
776 | bt_self_message_iterator *self_message_iterator, |
777 | bt_self_component *self_component, | |
778 | bt_self_component_port_output *self_component_port_output) | |
779 | { | |
c348a6fd | 780 | bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
6fa6bfa1 SM |
781 | PyObject *py_comp_cls = NULL; |
782 | PyObject *py_iter_cls = NULL; | |
783 | PyObject *py_iter_ptr = NULL; | |
a4dcfa96 | 784 | PyObject *py_component_port_output_ptr = NULL; |
6fa6bfa1 SM |
785 | PyObject *py_init_method_result = NULL; |
786 | PyObject *py_iter = NULL; | |
787 | PyObject *py_comp; | |
788 | ||
789 | py_comp = bt_self_component_get_data(self_component); | |
790 | ||
fa4c33e3 | 791 | /* Find user's Python message iterator class */ |
6fa6bfa1 SM |
792 | py_comp_cls = PyObject_GetAttrString(py_comp, "__class__"); |
793 | if (!py_comp_cls) { | |
794 | BT_LOGE_STR("Cannot get Python object's `__class__` attribute."); | |
795 | goto error; | |
796 | } | |
797 | ||
798 | py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls"); | |
799 | if (!py_iter_cls) { | |
800 | BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute."); | |
801 | goto error; | |
802 | } | |
803 | ||
804 | py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator), | |
805 | SWIGTYPE_p_bt_self_message_iterator, 0); | |
806 | if (!py_iter_ptr) { | |
807 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
808 | goto error; | |
809 | } | |
810 | ||
811 | /* | |
fa4c33e3 | 812 | * Create object with borrowed native message iterator |
6fa6bfa1 SM |
813 | * reference: |
814 | * | |
815 | * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr) | |
816 | */ | |
817 | py_iter = PyObject_CallMethod(py_iter_cls, "__new__", | |
818 | "(OO)", py_iter_cls, py_iter_ptr); | |
819 | if (!py_iter) { | |
820 | BT_LOGE("Failed to call Python class's __new__() method: " | |
821 | "py-cls-addr=%p", py_iter_cls); | |
822 | bt2_py_loge_exception(); | |
823 | goto error; | |
824 | } | |
825 | ||
826 | /* | |
827 | * Initialize object: | |
828 | * | |
a4dcfa96 SM |
829 | * py_iter.__init__(self_output_port) |
830 | * | |
831 | * through the _init_for_native helper static method. | |
6fa6bfa1 SM |
832 | * |
833 | * At this point, py_iter._ptr is set, so this initialization | |
834 | * function has access to self._component (which gives it the | |
835 | * user Python component object from which the iterator was | |
836 | * created). | |
837 | */ | |
a4dcfa96 SM |
838 | py_component_port_output_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_component_port_output), |
839 | SWIGTYPE_p_bt_self_component_port_output, 0); | |
840 | if (!py_component_port_output_ptr) { | |
841 | BT_LOGE_STR("Failed to create a SWIG pointer object."); | |
842 | goto error; | |
843 | } | |
844 | ||
845 | py_init_method_result = PyObject_CallMethod(py_iter, "_init_from_native", "O", py_component_port_output_ptr); | |
6fa6bfa1 SM |
846 | if (!py_init_method_result) { |
847 | BT_LOGE_STR("User's __init__() method failed."); | |
848 | bt2_py_loge_exception(); | |
849 | goto error; | |
850 | } | |
851 | ||
852 | /* | |
853 | * Since the Python code can never instantiate a user-defined | |
fa4c33e3 SM |
854 | * message iterator class, the native message iterator |
855 | * object does NOT belong to a user Python message iterator | |
6fa6bfa1 | 856 | * object (borrowed reference). However this Python object is |
fa4c33e3 | 857 | * owned by this native message iterator object. |
6fa6bfa1 | 858 | * |
fa4c33e3 SM |
859 | * In the Python world, the lifetime of the native message |
860 | * iterator is managed by a _GenericMessageIterator | |
6fa6bfa1 SM |
861 | * instance: |
862 | * | |
fa4c33e3 SM |
863 | * _GenericMessageIterator instance: |
864 | * owns a native bt_message_iterator object (iter) | |
6fa6bfa1 SM |
865 | * owns a _UserMessageIterator instance (py_iter) |
866 | * self._ptr is a borrowed reference to the | |
fa4c33e3 | 867 | * native bt_private_connection_private_message_iterator |
6fa6bfa1 SM |
868 | * object (iter) |
869 | */ | |
870 | bt_self_message_iterator_set_data(self_message_iterator, py_iter); | |
871 | py_iter = NULL; | |
872 | goto end; | |
873 | ||
874 | error: | |
875 | status = bt_py3_exc_to_self_message_iterator_status(); | |
876 | if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { | |
877 | /* | |
878 | * Looks like there wasn't any exception from the Python | |
879 | * side, but we're still in an error state here. | |
880 | */ | |
881 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
882 | } | |
883 | ||
884 | /* | |
885 | * Clear any exception: we're returning a bad status anyway. If | |
886 | * this call originated from Python, then the user gets an | |
887 | * appropriate creation error. | |
888 | */ | |
889 | PyErr_Clear(); | |
890 | ||
891 | end: | |
892 | Py_XDECREF(py_comp_cls); | |
893 | Py_XDECREF(py_iter_cls); | |
894 | Py_XDECREF(py_iter_ptr); | |
a4dcfa96 | 895 | Py_XDECREF(py_component_port_output_ptr); |
6fa6bfa1 SM |
896 | Py_XDECREF(py_init_method_result); |
897 | Py_XDECREF(py_iter); | |
898 | return status; | |
899 | } | |
900 | ||
efb177a7 SM |
901 | static |
902 | bt_self_message_iterator_status bt_py3_component_class_source_message_iterator_init( | |
6fa6bfa1 SM |
903 | bt_self_message_iterator *self_message_iterator, |
904 | bt_self_component_source *self_component_source, | |
905 | bt_self_component_port_output *self_component_port_output) | |
906 | { | |
907 | bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); | |
908 | return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); | |
909 | } | |
910 | ||
efb177a7 SM |
911 | static |
912 | bt_self_message_iterator_status bt_py3_component_class_filter_message_iterator_init( | |
6fa6bfa1 SM |
913 | bt_self_message_iterator *self_message_iterator, |
914 | bt_self_component_filter *self_component_filter, | |
915 | bt_self_component_port_output *self_component_port_output) | |
916 | { | |
917 | bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); | |
918 | return bt_py3_component_class_message_iterator_init(self_message_iterator, self_component, self_component_port_output); | |
919 | } | |
920 | ||
efb177a7 SM |
921 | static |
922 | void bt_py3_component_class_message_iterator_finalize( | |
6fa6bfa1 SM |
923 | bt_self_message_iterator *message_iterator) |
924 | { | |
925 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); | |
926 | PyObject *py_method_result = NULL; | |
927 | ||
928 | BT_ASSERT(py_message_iter); | |
929 | ||
930 | /* Call user's _finalize() method */ | |
931 | py_method_result = PyObject_CallMethod(py_message_iter, | |
932 | "_finalize", NULL); | |
933 | ||
934 | if (PyErr_Occurred()) { | |
935 | BT_LOGW("User's _finalize() method raised an exception: ignoring."); | |
936 | } | |
937 | ||
938 | /* | |
939 | * Ignore any exception raised by the _finalize() method because | |
940 | * it won't change anything at this point: the component is | |
941 | * being destroyed anyway. | |
942 | */ | |
943 | PyErr_Clear(); | |
944 | Py_XDECREF(py_method_result); | |
945 | Py_DECREF(py_message_iter); | |
946 | } | |
947 | ||
948 | /* Valid for both sources and filters. */ | |
949 | ||
efb177a7 SM |
950 | static |
951 | bt_self_message_iterator_status bt_py3_component_class_message_iterator_next( | |
952 | bt_self_message_iterator *message_iterator, | |
953 | bt_message_array_const msgs, uint64_t capacity, | |
954 | uint64_t *count) | |
6fa6bfa1 | 955 | { |
c348a6fd | 956 | bt_self_message_iterator_status status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
6fa6bfa1 SM |
957 | PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); |
958 | PyObject *py_method_result = NULL; | |
959 | ||
960 | BT_ASSERT(py_message_iter); | |
961 | py_method_result = PyObject_CallMethod(py_message_iter, | |
962 | "_next_from_native", NULL); | |
963 | if (!py_method_result) { | |
964 | status = bt_py3_exc_to_self_message_iterator_status(); | |
965 | BT_ASSERT(status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK); | |
966 | goto end; | |
967 | } | |
968 | ||
969 | /* | |
970 | * The returned object, on success, is an integer object | |
fa4c33e3 | 971 | * (PyLong) containing the address of a native message |
6fa6bfa1 SM |
972 | * object (which is now ours). |
973 | */ | |
c240a5d8 | 974 | msgs[0] = PyLong_AsVoidPtr(py_method_result); |
6fa6bfa1 SM |
975 | *count = 1; |
976 | ||
977 | /* Clear potential overflow error; should never happen */ | |
978 | BT_ASSERT(!PyErr_Occurred()); | |
979 | goto end; | |
980 | ||
981 | end: | |
982 | Py_XDECREF(py_method_result); | |
983 | return status; | |
984 | } | |
985 | ||
efb177a7 SM |
986 | static |
987 | bt_self_component_status bt_py3_component_class_sink_consume( | |
988 | bt_self_component_sink *self_component_sink) | |
6fa6bfa1 SM |
989 | { |
990 | bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); | |
991 | PyObject *py_comp = bt_self_component_get_data(self_component); | |
992 | PyObject *py_method_result = NULL; | |
993 | bt_self_component_status status; | |
994 | ||
995 | BT_ASSERT(py_comp); | |
996 | py_method_result = PyObject_CallMethod(py_comp, | |
997 | "_consume", NULL); | |
998 | ||
999 | status = bt_py3_exc_to_self_component_status(); | |
1000 | if (!py_method_result && status == BT_SELF_COMPONENT_STATUS_OK) { | |
1001 | /* Pretty sure this should never happen, but just in case */ | |
1002 | BT_LOGE("User's _consume() method failed without raising an exception: " | |
1003 | "status=%d", status); | |
1004 | status = BT_SELF_COMPONENT_STATUS_ERROR; | |
1005 | } | |
1006 | ||
1007 | Py_XDECREF(py_method_result); | |
1008 | return status; | |
1009 | } | |
1010 | ||
1011 | static | |
1012 | int bt_py3_component_class_set_help_and_desc( | |
1013 | bt_component_class *component_class, | |
1014 | const char *description, const char *help) | |
1015 | { | |
1016 | int ret; | |
1017 | ||
1018 | if (description) { | |
1019 | ret = bt_component_class_set_description(component_class, description); | |
1020 | if (ret) { | |
1021 | BT_LOGE("Cannot set component class's description: " | |
1022 | "comp-cls-addr=%p", component_class); | |
1023 | goto end; | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | if (help) { | |
1028 | ret = bt_component_class_set_help(component_class, help); | |
1029 | if (ret) { | |
1030 | BT_LOGE("Cannot set component class's help text: " | |
1031 | "comp-cls-addr=%p", component_class); | |
1032 | goto end; | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | ret = 0; | |
1037 | ||
1038 | end: | |
1039 | return ret; | |
1040 | } | |
1041 | ||
1042 | static | |
1043 | bt_component_class_source *bt_py3_component_class_source_create( | |
1044 | PyObject *py_cls, const char *name, const char *description, | |
1045 | const char *help) | |
1046 | { | |
1047 | bt_component_class_source *component_class_source; | |
1048 | bt_component_class *component_class; | |
1049 | int ret; | |
1050 | ||
1051 | BT_ASSERT(py_cls); | |
1052 | ||
1053 | component_class_source = bt_component_class_source_create(name, | |
1054 | bt_py3_component_class_message_iterator_next); | |
1055 | if (!component_class_source) { | |
1056 | BT_LOGE_STR("Cannot create source component class."); | |
1057 | goto end; | |
1058 | } | |
1059 | ||
1060 | component_class = bt_component_class_source_as_component_class(component_class_source); | |
1061 | ||
1062 | if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) { | |
1063 | goto end; | |
1064 | } | |
1065 | ||
1066 | ret = bt_component_class_source_set_init_method(component_class_source, bt_py3_component_class_source_init); | |
1067 | BT_ASSERT(ret == 0); | |
1068 | ret = bt_component_class_source_set_finalize_method (component_class_source, bt_py3_component_class_source_finalize); | |
1069 | BT_ASSERT(ret == 0); | |
6fa6bfa1 SM |
1070 | ret = bt_component_class_source_set_output_port_connected_method(component_class_source, |
1071 | bt_py3_component_class_source_output_port_connected); | |
1072 | BT_ASSERT(ret == 0); | |
1073 | ret = bt_component_class_source_set_query_method(component_class_source, bt_py3_component_class_source_query); | |
1074 | BT_ASSERT(ret == 0); | |
1075 | ret = bt_component_class_source_set_message_iterator_init_method( | |
1076 | component_class_source, bt_py3_component_class_source_message_iterator_init); | |
1077 | BT_ASSERT(ret == 0); | |
1078 | ret = bt_component_class_source_set_message_iterator_finalize_method( | |
1079 | component_class_source, bt_py3_component_class_message_iterator_finalize); | |
1080 | BT_ASSERT(ret == 0); | |
1081 | ||
1082 | register_cc_ptr_to_py_cls(component_class, py_cls); | |
1083 | ||
1084 | end: | |
1085 | return component_class_source; | |
1086 | } | |
1087 | ||
1088 | static | |
1089 | bt_component_class_filter *bt_py3_component_class_filter_create( | |
1090 | PyObject *py_cls, const char *name, const char *description, | |
1091 | const char *help) | |
1092 | { | |
1093 | bt_component_class *component_class; | |
1094 | bt_component_class_filter *component_class_filter; | |
1095 | int ret; | |
1096 | ||
1097 | BT_ASSERT(py_cls); | |
1098 | ||
1099 | component_class_filter = bt_component_class_filter_create(name, | |
1100 | bt_py3_component_class_message_iterator_next); | |
1101 | if (!component_class_filter) { | |
1102 | BT_LOGE_STR("Cannot create filter component class."); | |
1103 | goto end; | |
1104 | } | |
1105 | ||
1106 | component_class = bt_component_class_filter_as_component_class(component_class_filter); | |
1107 | ||
1108 | if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) { | |
1109 | goto end; | |
1110 | } | |
1111 | ||
1112 | ret = bt_component_class_filter_set_init_method(component_class_filter, bt_py3_component_class_filter_init); | |
1113 | BT_ASSERT(ret == 0); | |
1114 | ret = bt_component_class_filter_set_finalize_method (component_class_filter, bt_py3_component_class_filter_finalize); | |
1115 | BT_ASSERT(ret == 0); | |
6fa6bfa1 SM |
1116 | ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter, |
1117 | bt_py3_component_class_filter_input_port_connected); | |
1118 | BT_ASSERT(ret == 0); | |
1119 | ret = bt_component_class_filter_set_output_port_connected_method(component_class_filter, | |
1120 | bt_py3_component_class_filter_output_port_connected); | |
1121 | BT_ASSERT(ret == 0); | |
1122 | ret = bt_component_class_filter_set_query_method(component_class_filter, bt_py3_component_class_filter_query); | |
1123 | BT_ASSERT(ret == 0); | |
1124 | ret = bt_component_class_filter_set_message_iterator_init_method( | |
1125 | component_class_filter, bt_py3_component_class_filter_message_iterator_init); | |
1126 | BT_ASSERT(ret == 0); | |
1127 | ret = bt_component_class_filter_set_message_iterator_finalize_method( | |
1128 | component_class_filter, bt_py3_component_class_message_iterator_finalize); | |
1129 | BT_ASSERT(ret == 0); | |
1130 | ||
1131 | register_cc_ptr_to_py_cls(component_class, py_cls); | |
1132 | ||
1133 | end: | |
1134 | return component_class_filter; | |
1135 | } | |
1136 | ||
1137 | static | |
1138 | bt_component_class_sink *bt_py3_component_class_sink_create( | |
1139 | PyObject *py_cls, const char *name, const char *description, | |
1140 | const char *help) | |
1141 | { | |
1142 | bt_component_class_sink *component_class_sink; | |
1143 | bt_component_class *component_class; | |
1144 | int ret; | |
1145 | ||
1146 | BT_ASSERT(py_cls); | |
1147 | ||
1148 | component_class_sink = bt_component_class_sink_create(name, bt_py3_component_class_sink_consume); | |
1149 | ||
1150 | if (!component_class_sink) { | |
1151 | BT_LOGE_STR("Cannot create sink component class."); | |
1152 | goto end; | |
1153 | } | |
1154 | ||
1155 | component_class = bt_component_class_sink_as_component_class(component_class_sink); | |
1156 | ||
1157 | if (bt_py3_component_class_set_help_and_desc(component_class, description, help)) { | |
1158 | goto end; | |
1159 | } | |
1160 | ||
1161 | ret = bt_component_class_sink_set_init_method(component_class_sink, bt_py3_component_class_sink_init); | |
1162 | BT_ASSERT(ret == 0); | |
1163 | ret = bt_component_class_sink_set_finalize_method(component_class_sink, bt_py3_component_class_sink_finalize); | |
1164 | BT_ASSERT(ret == 0); | |
6fa6bfa1 SM |
1165 | ret = bt_component_class_sink_set_input_port_connected_method(component_class_sink, |
1166 | bt_py3_component_class_sink_input_port_connected); | |
1167 | BT_ASSERT(ret == 0); | |
1168 | ret = bt_component_class_sink_set_graph_is_configured_method(component_class_sink, | |
1169 | bt_py3_component_class_sink_graph_is_configured); | |
1170 | BT_ASSERT(ret == 0); | |
1171 | ret = bt_component_class_sink_set_query_method(component_class_sink, bt_py3_component_class_sink_query); | |
1172 | BT_ASSERT(ret == 0); | |
1173 | ||
1174 | register_cc_ptr_to_py_cls(component_class, py_cls); | |
1175 | ||
1176 | end: | |
1177 | return component_class_sink; | |
1178 | } | |
1179 | %} | |
1180 | ||
1181 | struct bt_component_class_source *bt_py3_component_class_source_create( | |
1182 | PyObject *py_cls, const char *name, const char *description, | |
1183 | const char *help); | |
1184 | struct bt_component_class_filter *bt_py3_component_class_filter_create( | |
1185 | PyObject *py_cls, const char *name, const char *description, | |
1186 | const char *help); | |
1187 | struct bt_component_class_sink *bt_py3_component_class_sink_create( | |
1188 | PyObject *py_cls, const char *name, const char *description, | |
1189 | const char *help); | |
1190 | void bt_py3_cc_init_from_bt2(void); | |
1191 | void bt_py3_cc_exit_handler(void); |