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