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