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