Python plugin provider: only finalize interpreter if we initialized it
[babeltrace.git] / bindings / python / bt2 / bt2 / native_btcomponentclass.i
CommitLineData
81447b5b
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
81447b5b
PP
25/* Types */
26struct bt_component_class;
27
28/* Status */
29enum bt_component_class_type {
811644b8
PP
30 BT_COMPONENT_CLASS_TYPE_UNKNOWN = -1,
31 BT_COMPONENT_CLASS_TYPE_SOURCE = 0,
32 BT_COMPONENT_CLASS_TYPE_SINK = 1,
33 BT_COMPONENT_CLASS_TYPE_FILTER = 2,
81447b5b
PP
34};
35
36/* General functions */
37const char *bt_component_class_get_name(
38 struct bt_component_class *component_class);
39const char *bt_component_class_get_description(
40 struct bt_component_class *component_class);
40910fbb
PP
41const char *bt_component_class_get_help(
42 struct bt_component_class *component_class);
81447b5b
PP
43enum bt_component_class_type bt_component_class_get_type(
44 struct bt_component_class *component_class);
45
46%{
47/*
48 * This hash table associates a BT component class object address to a
49 * user-defined Python class (PyObject *). The keys and values are NOT
50 * owned by this hash table. The Python class objects are owned by the
51 * Python module, which should not be unloaded until it is not possible
52 * to create a user Python component anyway.
53 *
54 * This hash table is written to when a user-defined Python component
55 * class is created by one of the bt_py3_component_class_*_create()
56 * functions.
57 *
58 * This function is read from when a user calls bt_component_create()
59 * with a component class pointer created by one of the functions above.
60 * In this case, the original Python class needs to be found to
61 * instantiate it and associate the created Python component object with
62 * a BT component object instance.
63 */
64
65static GHashTable *bt_cc_ptr_to_py_cls;
66
67static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
68 PyObject *py_cls)
69{
811644b8
PP
70 if (!bt_cc_ptr_to_py_cls) {
71 /*
72 * Lazy-initializing this GHashTable because GLib
73 * might not be initialized yet and it needs to be
74 * before we call g_hash_table_new()
75 */
76 BT_LOGD_STR("Creating native component class to Python component class hash table.");
77 bt_cc_ptr_to_py_cls = g_hash_table_new(g_direct_hash, g_direct_equal);
78 assert(bt_cc_ptr_to_py_cls);
79 }
80
81447b5b
PP
81 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
82 (gpointer) py_cls);
83}
84
85static PyObject *lookup_cc_ptr_to_py_cls(struct bt_component_class *bt_cc)
86{
811644b8
PP
87 if (!bt_cc_ptr_to_py_cls) {
88 BT_LOGW("Cannot look up Python component class because hash table is NULL: "
89 "comp-cls-addr=%p", bt_cc);
90 return NULL;
91 }
92
81447b5b
PP
93 return (PyObject *) g_hash_table_lookup(bt_cc_ptr_to_py_cls,
94 (gconstpointer) bt_cc);
95}
96
97
98/*
99 * Useful Python objects.
100 */
101
102static PyObject *py_mod_bt2 = NULL;
103static PyObject *py_mod_bt2_exc_error_type = NULL;
104static PyObject *py_mod_bt2_exc_unsupported_feature_type = NULL;
105static PyObject *py_mod_bt2_exc_try_again_type = NULL;
106static PyObject *py_mod_bt2_exc_stop_type = NULL;
811644b8 107static PyObject *py_mod_bt2_exc_port_connection_refused_type = NULL;
811644b8 108static PyObject *py_mod_bt2_exc_notif_iter_canceled_type = NULL;
c7eee084
PP
109static PyObject *py_mod_bt2_exc_invalid_query_object_type = NULL;
110static PyObject *py_mod_bt2_exc_invalid_query_params_type = NULL;
81447b5b
PP
111
112static void bt_py3_cc_init_from_bt2(void)
113{
114 /*
115 * This is called once the bt2 package is loaded.
116 *
117 * Those modules and functions are needed while the package is
118 * used. Loading them here is safe because we know the bt2
119 * package is imported, and we know that the user cannot use the
120 * code here without importing bt2 first.
121 */
122 py_mod_bt2 = PyImport_ImportModule("bt2");
123 assert(py_mod_bt2);
81447b5b
PP
124 py_mod_bt2_exc_error_type =
125 PyObject_GetAttrString(py_mod_bt2, "Error");
126 assert(py_mod_bt2_exc_error_type);
127 py_mod_bt2_exc_unsupported_feature_type =
128 PyObject_GetAttrString(py_mod_bt2, "UnsupportedFeature");
c7eee084 129 assert(py_mod_bt2_exc_unsupported_feature_type);
81447b5b
PP
130 py_mod_bt2_exc_try_again_type =
131 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
c7eee084 132 assert(py_mod_bt2_exc_try_again_type);
81447b5b
PP
133 py_mod_bt2_exc_stop_type =
134 PyObject_GetAttrString(py_mod_bt2, "Stop");
c7eee084 135 assert(py_mod_bt2_exc_stop_type);
811644b8
PP
136 py_mod_bt2_exc_port_connection_refused_type =
137 PyObject_GetAttrString(py_mod_bt2, "PortConnectionRefused");
c7eee084
PP
138 assert(py_mod_bt2_exc_port_connection_refused_type);
139 py_mod_bt2_exc_invalid_query_object_type =
140 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryObject");
141 assert(py_mod_bt2_exc_invalid_query_object_type);
142 py_mod_bt2_exc_invalid_query_params_type =
143 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryParams");
144 assert(py_mod_bt2_exc_invalid_query_params_type);
81447b5b
PP
145}
146
147static void bt_py3_cc_exit_handler(void)
148{
149 /*
150 * This is an exit handler (set by the bt2 package).
151 *
152 * We only give back the references that we took in
153 * bt_py3_cc_init_from_bt2() here. The global variables continue
154 * to exist for the code of this file, but they are now borrowed
155 * references. If this code is executed, it means that somehow
811644b8
PP
156 * the modules are still loaded, so it should be safe to use
157 * them even without a strong reference.
81447b5b
PP
158 *
159 * We cannot do this in the library's destructor because it
160 * gets executed once Python is already finalized.
161 */
81447b5b
PP
162 Py_XDECREF(py_mod_bt2);
163 Py_XDECREF(py_mod_bt2_exc_error_type);
164 Py_XDECREF(py_mod_bt2_exc_unsupported_feature_type);
165 Py_XDECREF(py_mod_bt2_exc_try_again_type);
166 Py_XDECREF(py_mod_bt2_exc_stop_type);
811644b8 167 Py_XDECREF(py_mod_bt2_exc_port_connection_refused_type);
811644b8 168 Py_XDECREF(py_mod_bt2_exc_notif_iter_canceled_type);
c7eee084
PP
169 Py_XDECREF(py_mod_bt2_exc_invalid_query_object_type);
170 Py_XDECREF(py_mod_bt2_exc_invalid_query_params_type);
81447b5b
PP
171}
172
173
811644b8 174/* Library destructor */
81447b5b
PP
175
176__attribute__((destructor))
177static void bt_py3_native_comp_class_dtor(void) {
178 /* Destroy component class association hash table */
179 if (bt_cc_ptr_to_py_cls) {
811644b8 180 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
81447b5b
PP
181 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
182 }
183}
184
185
811644b8 186/* Component class proxy methods (delegate to the attached Python object) */
81447b5b 187
811644b8 188static enum bt_notification_iterator_status bt_py3_exc_to_notif_iter_status(void)
81447b5b 189{
811644b8
PP
190 enum bt_notification_iterator_status status =
191 BT_NOTIFICATION_ITERATOR_STATUS_OK;
192 PyObject *exc = PyErr_Occurred();
81447b5b 193
811644b8 194 if (!exc) {
81447b5b
PP
195 goto end;
196 }
197
811644b8
PP
198 if (PyErr_GivenExceptionMatches(exc,
199 py_mod_bt2_exc_unsupported_feature_type)) {
200 status = BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED;
201 } else if (PyErr_GivenExceptionMatches(exc,
202 py_mod_bt2_exc_stop_type)) {
203 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
204 } else if (PyErr_GivenExceptionMatches(exc,
205 py_mod_bt2_exc_try_again_type)) {
206 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
207 } else {
208 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
209 }
81447b5b
PP
210
211end:
81447b5b 212 PyErr_Clear();
811644b8 213 return status;
81447b5b
PP
214}
215
c7eee084
PP
216static enum bt_query_status bt_py3_exc_to_query_status(void)
217{
218 enum bt_query_status status = BT_QUERY_STATUS_OK;
219 PyObject *exc = PyErr_Occurred();
220
221 if (!exc) {
222 goto end;
223 }
224
225 if (PyErr_GivenExceptionMatches(exc,
226 py_mod_bt2_exc_invalid_query_object_type)) {
227 status = BT_QUERY_STATUS_INVALID_OBJECT;
228 } else if (PyErr_GivenExceptionMatches(exc,
229 py_mod_bt2_exc_invalid_query_params_type)) {
230 status = BT_QUERY_STATUS_INVALID_PARAMS;
231 } else if (PyErr_GivenExceptionMatches(exc,
232 py_mod_bt2_exc_try_again_type)) {
233 status = BT_QUERY_STATUS_AGAIN;
234 } else {
235 status = BT_QUERY_STATUS_ERROR;
236 }
237
238end:
239 PyErr_Clear();
240 return status;
241}
242
811644b8 243static enum bt_component_status bt_py3_exc_to_component_status(void)
81447b5b 244{
811644b8
PP
245 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
246 PyObject *exc = PyErr_Occurred();
81447b5b 247
811644b8
PP
248 if (!exc) {
249 goto end;
81447b5b
PP
250 }
251
811644b8
PP
252 if (PyErr_GivenExceptionMatches(exc,
253 py_mod_bt2_exc_unsupported_feature_type)) {
254 status = BT_COMPONENT_STATUS_UNSUPPORTED;
255 } else if (PyErr_GivenExceptionMatches(exc,
256 py_mod_bt2_exc_try_again_type)) {
257 status = BT_COMPONENT_STATUS_AGAIN;
258 } else if (PyErr_GivenExceptionMatches(exc,
259 py_mod_bt2_exc_stop_type)) {
260 status = BT_COMPONENT_STATUS_END;
261 } else if (PyErr_GivenExceptionMatches(exc,
262 py_mod_bt2_exc_port_connection_refused_type)) {
263 status = BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION;
264 } else {
265 status = BT_COMPONENT_STATUS_ERROR;
81447b5b
PP
266 }
267
811644b8
PP
268end:
269 PyErr_Clear();
270 return status;
271}
272
273static enum bt_component_status bt_py3_cc_init(
274 struct bt_private_component *priv_comp,
275 struct bt_value *params, void *init_method_data)
276{
277 struct bt_component *comp =
278 bt_component_from_private_component(priv_comp);
279 struct bt_component_class *comp_cls = bt_component_get_class(comp);
280 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
281 PyObject *py_cls = NULL;
282 PyObject *py_comp = NULL;
283 PyObject *py_params_ptr = NULL;
284 PyObject *py_comp_ptr = NULL;
285
286 (void) init_method_data;
287 assert(comp);
288 assert(comp_cls);
289
290 /*
291 * Get the user-defined Python class which created this
292 * component's class in the first place (borrowed
293 * reference).
294 */
295 py_cls = lookup_cc_ptr_to_py_cls(comp_cls);
296 if (!py_cls) {
297 BT_LOGE("Cannot find Python class associated to native component class: "
298 "comp-cls-addr=%p", comp_cls);
81447b5b
PP
299 goto error;
300 }
301
811644b8
PP
302 /* Parameters pointer -> SWIG pointer Python object */
303 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
304 SWIGTYPE_p_bt_value, 0);
305 if (!py_params_ptr) {
306 BT_LOGE_STR("Failed to create a SWIG pointer object.");
81447b5b
PP
307 goto error;
308 }
309
811644b8
PP
310 /* Private component pointer -> SWIG pointer Python object */
311 py_comp_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(priv_comp),
312 SWIGTYPE_p_bt_private_component, 0);
313 if (!py_comp_ptr) {
314 BT_LOGE_STR("Failed to create a SWIG pointer object.");
315 goto error;
81447b5b
PP
316 }
317
811644b8
PP
318 /*
319 * Do the equivalent of this:
320 *
321 * py_comp = py_cls._init_from_native(py_comp_ptr, py_params_ptr)
322 *
323 * _UserComponentType._init_from_native() calls the Python
324 * component object's __init__() function.
325 */
326 py_comp = PyObject_CallMethod(py_cls,
327 "_init_from_native", "(OO)", py_comp_ptr, py_params_ptr);
328 if (!py_comp) {
329 BT_LOGE("Failed to call Python class's _init_from_native() method: "
330 "py-cls-addr=%p", py_cls);
81447b5b
PP
331 goto error;
332 }
333
811644b8
PP
334 /*
335 * Our user Python component object is now fully created and
336 * initialized by the user. Since we just created it, this
337 * native component is its only (persistent) owner.
338 */
339 bt_private_component_set_user_data(priv_comp, py_comp);
340 py_comp = NULL;
81447b5b
PP
341 goto end;
342
343error:
811644b8 344 status = BT_COMPONENT_STATUS_ERROR;
81447b5b
PP
345
346 /*
811644b8
PP
347 * Clear any exception: we're returning a bad status anyway. If
348 * this call originated from Python (creation from a plugin's
349 * component class, for example), then the user gets an
350 * appropriate creation error.
81447b5b 351 */
811644b8 352 PyErr_Clear();
81447b5b 353
811644b8
PP
354end:
355 bt_put(comp_cls);
356 bt_put(comp);
357 Py_XDECREF(py_comp);
358 Py_XDECREF(py_params_ptr);
359 Py_XDECREF(py_comp_ptr);
360 return status;
361}
81447b5b 362
811644b8
PP
363static void bt_py3_cc_finalize(struct bt_private_component *priv_comp)
364{
365 PyObject *py_comp = bt_private_component_get_user_data(priv_comp);
366 PyObject *py_method_result = NULL;
81447b5b 367
811644b8 368 assert(py_comp);
81447b5b 369
811644b8
PP
370 /* Call user's _finalize() method */
371 py_method_result = PyObject_CallMethod(py_comp,
372 "_finalize", NULL);
81447b5b 373
811644b8
PP
374 if (PyErr_Occurred()) {
375 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
376 }
81447b5b 377
811644b8
PP
378 /*
379 * Ignore any exception raised by the _finalize() method because
380 * it won't change anything at this point: the component is
381 * being destroyed anyway.
382 */
383 PyErr_Clear();
384 Py_XDECREF(py_method_result);
385 Py_DECREF(py_comp);
386}
81447b5b 387
811644b8
PP
388static enum bt_component_status bt_py3_cc_accept_port_connection(
389 struct bt_private_component *priv_comp,
390 struct bt_private_port *self_priv_port,
391 struct bt_port *other_port)
392{
393 enum bt_component_status status;
394 PyObject *py_comp = NULL;
395 PyObject *py_self_port_ptr = NULL;
396 PyObject *py_other_port_ptr = NULL;
397 PyObject *py_method_result = NULL;
81447b5b 398
811644b8
PP
399 py_comp = bt_private_component_get_user_data(priv_comp);
400 assert(py_comp);
401 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_priv_port),
402 SWIGTYPE_p_bt_private_port, 0);
403 if (!py_self_port_ptr) {
404 BT_LOGE_STR("Failed to create a SWIG pointer object.");
405 goto error;
406 }
81447b5b 407
811644b8
PP
408 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
409 SWIGTYPE_p_bt_port, 0);
410 if (!py_other_port_ptr) {
411 BT_LOGE_STR("Failed to create a SWIG pointer object.");
412 goto error;
413 }
81447b5b 414
811644b8
PP
415 py_method_result = PyObject_CallMethod(py_comp,
416 "_accept_port_connection_from_native", "(OO)", py_self_port_ptr,
417 py_other_port_ptr);
418 status = bt_py3_exc_to_component_status();
419 if (!py_method_result && status == BT_COMPONENT_STATUS_OK) {
420 /* Pretty sure this should never happen, but just in case */
421 BT_LOGE("User's _accept_port_connection() method failed without raising an exception: "
422 "status=%d", status);
423 goto error;
424 }
81447b5b 425
811644b8 426 if (status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
81447b5b 427 /*
811644b8
PP
428 * Looks like the user method raised
429 * PortConnectionRefused: accept this like if it
430 * returned False.
81447b5b 431 */
811644b8
PP
432 goto end;
433 } else if (status != BT_COMPONENT_STATUS_OK) {
434 BT_LOGE("User's _accept_port_connection() raised an unexpected exception: "
435 "status=%d", status);
436 goto error;
437 }
81447b5b 438
811644b8 439 assert(PyBool_Check(py_method_result));
81447b5b 440
811644b8
PP
441 if (py_method_result == Py_True) {
442 status = BT_COMPONENT_STATUS_OK;
81447b5b 443 } else {
811644b8
PP
444 status = BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION;
445 }
81447b5b 446
811644b8 447 goto end;
81447b5b 448
811644b8
PP
449error:
450 status = BT_COMPONENT_STATUS_ERROR;
81447b5b 451
811644b8
PP
452 /*
453 * Clear any exception: we're returning a bad status anyway. If
454 * this call originated from Python, then the user gets an
455 * appropriate error.
456 */
457 PyErr_Clear();
81447b5b 458
811644b8
PP
459end:
460 Py_XDECREF(py_self_port_ptr);
461 Py_XDECREF(py_other_port_ptr);
462 Py_XDECREF(py_method_result);
81447b5b
PP
463 return status;
464}
465
811644b8
PP
466static void bt_py3_cc_port_connected(
467 struct bt_private_component *priv_comp,
468 struct bt_private_port *self_priv_port,
469 struct bt_port *other_port)
81447b5b 470{
811644b8
PP
471 PyObject *py_comp = NULL;
472 PyObject *py_self_port_ptr = NULL;
473 PyObject *py_other_port_ptr = NULL;
474 PyObject *py_method_result = NULL;
81447b5b 475
811644b8
PP
476 py_comp = bt_private_component_get_user_data(priv_comp);
477 assert(py_comp);
478 py_self_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_priv_port),
479 SWIGTYPE_p_bt_private_port, 0);
480 if (!py_self_port_ptr) {
481 BT_LOGF_STR("Failed to create a SWIG pointer object.");
482 abort();
483 }
484
485 py_other_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(other_port),
486 SWIGTYPE_p_bt_port, 0);
487 if (!py_other_port_ptr) {
488 BT_LOGF_STR("Failed to create a SWIG pointer object.");
489 abort();
490 }
491
492 py_method_result = PyObject_CallMethod(py_comp,
493 "_port_connected_from_native", "(OO)", py_self_port_ptr,
494 py_other_port_ptr);
495 assert(py_method_result == Py_None);
496 Py_XDECREF(py_self_port_ptr);
497 Py_XDECREF(py_other_port_ptr);
498 Py_XDECREF(py_method_result);
499}
81447b5b 500
811644b8
PP
501static void bt_py3_cc_port_disconnected(
502 struct bt_private_component *priv_comp,
503 struct bt_private_port *priv_port)
504{
505 PyObject *py_comp = NULL;
506 PyObject *py_port_ptr = NULL;
507 PyObject *py_method_result = NULL;
508
509 py_comp = bt_private_component_get_user_data(priv_comp);
510 assert(py_comp);
511 py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(priv_port),
512 SWIGTYPE_p_bt_private_port, 0);
513 if (!py_port_ptr) {
514 BT_LOGF_STR("Failed to create a SWIG pointer object.");
515 abort();
516 }
517
518 py_method_result = PyObject_CallMethod(py_comp,
519 "_port_disconnected_from_native", "(O)", py_port_ptr);
520 assert(py_method_result == Py_None);
521 Py_XDECREF(py_port_ptr);
522 Py_XDECREF(py_method_result);
81447b5b
PP
523}
524
c7eee084 525static struct bt_component_class_query_return bt_py3_cc_query(
811644b8 526 struct bt_component_class *comp_cls,
c7eee084 527 struct bt_query_executor *query_exec,
a67681c1 528 const char *object, struct bt_value *params)
911dec08
PP
529{
530 PyObject *py_cls = NULL;
811644b8 531 PyObject *py_params_ptr = NULL;
c7eee084 532 PyObject *py_query_exec_ptr = NULL;
a67681c1
PP
533 PyObject *py_query_func = NULL;
534 PyObject *py_object = NULL;
911dec08 535 PyObject *py_results_addr = NULL;
c7eee084
PP
536 struct bt_component_class_query_return ret = {
537 .status = BT_QUERY_STATUS_OK,
538 .result = NULL,
539 };
911dec08 540
811644b8 541 py_cls = lookup_cc_ptr_to_py_cls(comp_cls);
911dec08 542 if (!py_cls) {
811644b8
PP
543 BT_LOGE("Cannot find Python class associated to native component class: "
544 "comp-cls-addr=%p", comp_cls);
911dec08
PP
545 goto error;
546 }
547
811644b8
PP
548 py_params_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(params),
549 SWIGTYPE_p_bt_value, 0);
550 if (!py_params_ptr) {
551 BT_LOGE_STR("Failed to create a SWIG pointer object.");
911dec08
PP
552 goto error;
553 }
554
c7eee084
PP
555 py_query_exec_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(query_exec),
556 SWIGTYPE_p_bt_query_executor, 0);
557 if (!py_query_exec_ptr) {
558 BT_LOGE_STR("Failed to create a SWIG pointer object.");
559 goto error;
560 }
561
a67681c1
PP
562 py_object = SWIG_FromCharPtr(object);
563 if (!py_object) {
811644b8 564 BT_LOGE_STR("Failed to create a Python string.");
911dec08
PP
565 goto error;
566 }
567
568 py_results_addr = PyObject_CallMethod(py_cls,
c7eee084
PP
569 "_query_from_native", "(OOO)", py_query_exec_ptr,
570 py_object, py_params_ptr);
571 ret.status = bt_py3_exc_to_query_status();
572 if (!py_results_addr && ret.status == BT_QUERY_STATUS_OK) {
573 /* Pretty sure this should never happen, but just in case */
574 BT_LOGE("_query_from_native() class method failed without raising an exception: "
575 "status=%d", ret.status);
576 ret.status = BT_QUERY_STATUS_ERROR;
577 goto end;
578 }
579
580 if (ret.status != BT_QUERY_STATUS_OK) {
581 goto end;
911dec08
PP
582 }
583
811644b8
PP
584 if (py_results_addr == Py_NotImplemented) {
585 BT_LOGE_STR("User's _query() method is not implemented.");
911dec08
PP
586 goto error;
587 }
588
811644b8
PP
589 /*
590 * The returned object, on success, is an integer object
591 * (PyLong) containing the address of a BT value object (new
592 * reference).
593 */
c7eee084 594 ret.result = (void *) PyLong_AsUnsignedLongLong(py_results_addr);
811644b8 595 assert(!PyErr_Occurred());
c7eee084 596 assert(ret.result);
911dec08
PP
597 goto end;
598
599error:
c7eee084 600 BT_PUT(ret.result);
911dec08 601 PyErr_Clear();
c7eee084
PP
602 ret.status = BT_QUERY_STATUS_ERROR;
603 BT_PUT(ret.result);
911dec08
PP
604
605end:
811644b8 606 Py_XDECREF(py_params_ptr);
c7eee084 607 Py_XDECREF(py_query_exec_ptr);
a67681c1
PP
608 Py_XDECREF(py_query_func);
609 Py_XDECREF(py_object);
911dec08 610 Py_XDECREF(py_results_addr);
c7eee084 611 return ret;
911dec08
PP
612}
613
81447b5b 614static enum bt_notification_iterator_status bt_py3_cc_notification_iterator_init(
811644b8
PP
615 struct bt_private_notification_iterator *priv_notif_iter,
616 struct bt_private_port *priv_port)
81447b5b
PP
617{
618 enum bt_notification_iterator_status status =
619 BT_NOTIFICATION_ITERATOR_STATUS_OK;
620 PyObject *py_comp_cls = NULL;
621 PyObject *py_iter_cls = NULL;
622 PyObject *py_iter_ptr = NULL;
623 PyObject *py_init_method_result = NULL;
624 PyObject *py_iter = NULL;
811644b8
PP
625 struct bt_private_component *priv_comp =
626 bt_private_notification_iterator_get_private_component(
627 priv_notif_iter);
628 PyObject *py_comp;
629
630 assert(priv_comp);
631 py_comp = bt_private_component_get_user_data(priv_comp);
81447b5b
PP
632
633 /* Find user's Python notification iterator class */
634 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
635 if (!py_comp_cls) {
811644b8 636 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
81447b5b
PP
637 goto error;
638 }
639
640 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
641 if (!py_iter_cls) {
811644b8
PP
642 BT_LOGE_STR("Cannot get Python class's `_iter_cls` attribute.");
643 goto error;
644 }
645
646 py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(priv_notif_iter),
647 SWIGTYPE_p_bt_private_notification_iterator, 0);
648 if (!py_iter_ptr) {
649 BT_LOGE_STR("Failed to create a SWIG pointer object.");
81447b5b
PP
650 goto error;
651 }
652
653 /*
811644b8 654 * Create object with borrowed native notification iterator
81447b5b
PP
655 * reference:
656 *
657 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
658 */
81447b5b
PP
659 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
660 "(OO)", py_iter_cls, py_iter_ptr);
661 if (!py_iter) {
811644b8
PP
662 BT_LOGE("Failed to call Python class's __new__() method: "
663 "py-cls-addr=%p", py_iter_cls);
81447b5b
PP
664 goto error;
665 }
666
667 /*
668 * Initialize object:
669 *
670 * py_iter.__init__()
671 *
672 * At this point, py_iter._ptr is set, so this initialization
811644b8
PP
673 * function has access to self._component (which gives it the
674 * user Python component object from which the iterator was
675 * created).
81447b5b
PP
676 */
677 py_init_method_result = PyObject_CallMethod(py_iter, "__init__", NULL);
678 if (!py_init_method_result) {
811644b8 679 BT_LOGE_STR("User's __init__() method failed.");
81447b5b
PP
680 goto error;
681 }
682
683 /*
684 * Since the Python code can never instantiate a user-defined
811644b8 685 * notification iterator class, the native notification iterator
81447b5b
PP
686 * object does NOT belong to a user Python notification iterator
687 * object (borrowed reference). However this Python object is
811644b8 688 * owned by this native notification iterator object.
81447b5b 689 *
811644b8 690 * In the Python world, the lifetime of the native notification
81447b5b
PP
691 * iterator is managed by a _GenericNotificationIterator
692 * instance:
693 *
694 * _GenericNotificationIterator instance:
695 * owns a native bt_notification_iterator object (iter)
811644b8 696 * owns a _UserNotificationIterator instance (py_iter)
81447b5b 697 * self._ptr is a borrowed reference to the
811644b8
PP
698 * native bt_private_notification_iterator
699 * object (iter)
81447b5b 700 */
811644b8
PP
701 bt_private_notification_iterator_set_user_data(priv_notif_iter,
702 py_iter);
81447b5b
PP
703 py_iter = NULL;
704 goto end;
705
706error:
707 status = bt_py3_exc_to_notif_iter_status();
708 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
709 /*
710 * Looks like there wasn't any exception from the Python
711 * side, but we're still in an error state here.
712 */
713 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
714 }
715
716 /*
717 * Clear any exception: we're returning a bad status anyway. If
718 * this call originated from Python, then the user gets an
719 * appropriate creation error.
720 */
721 PyErr_Clear();
722
723end:
811644b8 724 bt_put(priv_comp);
81447b5b
PP
725 Py_XDECREF(py_comp_cls);
726 Py_XDECREF(py_iter_cls);
727 Py_XDECREF(py_iter_ptr);
728 Py_XDECREF(py_init_method_result);
729 Py_XDECREF(py_iter);
730 return status;
731}
732
811644b8
PP
733static void bt_py3_cc_notification_iterator_finalize(
734 struct bt_private_notification_iterator *priv_notif_iter)
81447b5b
PP
735{
736 PyObject *py_notif_iter =
811644b8
PP
737 bt_private_notification_iterator_get_user_data(priv_notif_iter);
738 PyObject *py_method_result = NULL;
81447b5b
PP
739
740 assert(py_notif_iter);
811644b8
PP
741
742 /* Call user's _finalize() method */
743 py_method_result = PyObject_CallMethod(py_notif_iter,
744 "_finalize", NULL);
745
746 if (PyErr_Occurred()) {
747 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
748 }
81447b5b
PP
749
750 /*
811644b8
PP
751 * Ignore any exception raised by the _finalize() method because
752 * it won't change anything at this point: the component is
753 * being destroyed anyway.
81447b5b
PP
754 */
755 PyErr_Clear();
811644b8 756 Py_XDECREF(py_method_result);
81447b5b
PP
757 Py_DECREF(py_notif_iter);
758}
759
811644b8
PP
760static struct bt_notification_iterator_next_return
761bt_py3_cc_notification_iterator_next(
762 struct bt_private_notification_iterator *priv_notif_iter)
81447b5b 763{
811644b8
PP
764 struct bt_notification_iterator_next_return next_ret = {
765 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
766 .notification = NULL,
767 };
81447b5b 768 PyObject *py_notif_iter =
811644b8
PP
769 bt_private_notification_iterator_get_user_data(priv_notif_iter);
770 PyObject *py_method_result = NULL;
81447b5b
PP
771
772 assert(py_notif_iter);
811644b8
PP
773 py_method_result = PyObject_CallMethod(py_notif_iter,
774 "_next_from_native", NULL);
775 if (!py_method_result) {
776 next_ret.status = bt_py3_exc_to_notif_iter_status();
777 assert(next_ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK);
778 goto end;
81447b5b
PP
779 }
780
781 /*
782 * The returned object, on success, is an integer object
811644b8 783 * (PyLong) containing the address of a native notification
911dec08 784 * object (which is now ours).
81447b5b 785 */
811644b8
PP
786 next_ret.notification =
787 (struct bt_notification *) PyLong_AsUnsignedLongLong(
788 py_method_result);
81447b5b
PP
789
790 /* Clear potential overflow error; should never happen */
811644b8
PP
791 assert(!PyErr_Occurred());
792 assert(next_ret.notification);
81447b5b
PP
793 goto end;
794
81447b5b 795end:
811644b8
PP
796 Py_XDECREF(py_method_result);
797 return next_ret;
81447b5b
PP
798}
799
800static enum bt_component_status bt_py3_cc_sink_consume(
811644b8 801 struct bt_private_component *priv_comp)
81447b5b 802{
811644b8
PP
803 PyObject *py_comp = bt_private_component_get_user_data(priv_comp);
804 PyObject *py_method_result = NULL;
81447b5b
PP
805 enum bt_component_status status;
806
811644b8
PP
807 assert(py_comp);
808 py_method_result = PyObject_CallMethod(py_comp,
81447b5b
PP
809 "_consume", NULL);
810 status = bt_py3_exc_to_component_status();
811644b8 811 if (!py_method_result && status == BT_COMPONENT_STATUS_OK) {
81447b5b 812 /* Pretty sure this should never happen, but just in case */
811644b8
PP
813 BT_LOGE("User's _consume() method failed without raising an exception: "
814 "status=%d", status);
81447b5b
PP
815 status = BT_COMPONENT_STATUS_ERROR;
816 }
817
811644b8 818 Py_XDECREF(py_method_result);
81447b5b
PP
819 return status;
820}
821
822
823/* Component class creation functions (called from Python module) */
824
825static int bt_py3_cc_set_optional_attrs_methods(struct bt_component_class *cc,
40910fbb 826 const char *description, const char *help)
81447b5b
PP
827{
828 int ret = 0;
829
830 if (description) {
831 ret = bt_component_class_set_description(cc, description);
832 if (ret) {
811644b8
PP
833 BT_LOGE("Cannot set component class's description: "
834 "comp-cls-addr=%p", cc);
81447b5b
PP
835 goto end;
836 }
837 }
838
40910fbb
PP
839 if (help) {
840 ret = bt_component_class_set_help(cc, help);
841 if (ret) {
811644b8
PP
842 BT_LOGE("Cannot set component class's help text: "
843 "comp-cls-addr=%p", cc);
40910fbb
PP
844 goto end;
845 }
846 }
847
81447b5b 848 ret = bt_component_class_set_init_method(cc, bt_py3_cc_init);
811644b8
PP
849 assert(ret == 0);
850 ret = bt_component_class_set_finalize_method(cc, bt_py3_cc_finalize);
851 assert(ret == 0);
852 ret = bt_component_class_set_accept_port_connection_method(cc,
853 bt_py3_cc_accept_port_connection);
854 assert(ret == 0);
855 ret = bt_component_class_set_port_connected_method(cc,
856 bt_py3_cc_port_connected);
857 assert(ret == 0);
858 ret = bt_component_class_set_port_disconnected_method(cc,
859 bt_py3_cc_port_disconnected);
860 assert(ret == 0);
a67681c1 861 ret = bt_component_class_set_query_method(cc, bt_py3_cc_query);
811644b8 862 assert(ret == 0);
911dec08 863
81447b5b
PP
864end:
865 return ret;
866}
867
811644b8 868static void bt_py3_cc_set_optional_iter_methods(struct bt_component_class *cc,
81447b5b 869 int (*set_notif_iter_init_method)(struct bt_component_class *, bt_component_class_notification_iterator_init_method),
811644b8 870 int (*set_notif_iter_finalize_method)(struct bt_component_class *, bt_component_class_notification_iterator_finalize_method))
81447b5b 871{
d080d694 872 int ret __attribute__((unused));
81447b5b
PP
873
874 ret = set_notif_iter_init_method(
875 cc, bt_py3_cc_notification_iterator_init);
811644b8
PP
876 assert(ret == 0);
877 ret = set_notif_iter_finalize_method(
878 cc, bt_py3_cc_notification_iterator_finalize);
879 assert(ret == 0);
81447b5b
PP
880}
881
882static struct bt_component_class *bt_py3_component_class_source_create(
883 PyObject *py_cls, const char *name, const char *description,
811644b8 884 const char *help)
81447b5b
PP
885{
886 struct bt_component_class *cc;
887 int ret;
888
889 assert(py_cls);
890 cc = bt_component_class_source_create(name,
81447b5b
PP
891 bt_py3_cc_notification_iterator_next);
892 if (!cc) {
811644b8 893 BT_LOGE_STR("Cannot create source component class.");
81447b5b
PP
894 goto end;
895 }
896
40910fbb 897 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
81447b5b 898 if (ret) {
811644b8 899 BT_LOGE_STR("Cannot set source component class's optional attributes and methods.");
81447b5b
PP
900 BT_PUT(cc);
901 goto end;
902 }
903
811644b8 904 bt_py3_cc_set_optional_iter_methods(cc,
81447b5b 905 bt_component_class_source_set_notification_iterator_init_method,
811644b8 906 bt_component_class_source_set_notification_iterator_finalize_method);
81447b5b
PP
907 register_cc_ptr_to_py_cls(cc, py_cls);
908 bt_component_class_freeze(cc);
909
910end:
911 return cc;
912}
913
914static struct bt_component_class *bt_py3_component_class_filter_create(
915 PyObject *py_cls, const char *name, const char *description,
811644b8 916 const char *help)
81447b5b
PP
917{
918 struct bt_component_class *cc;
919 int ret;
920
921 assert(py_cls);
922 cc = bt_component_class_filter_create(name,
81447b5b
PP
923 bt_py3_cc_notification_iterator_next);
924 if (!cc) {
811644b8 925 BT_LOGE_STR("Cannot create filter component class.");
81447b5b
PP
926 goto end;
927 }
928
40910fbb 929 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
81447b5b 930 if (ret) {
811644b8 931 BT_LOGE_STR("Cannot set filter component class's optional attributes and methods.");
81447b5b
PP
932 BT_PUT(cc);
933 goto end;
934 }
935
811644b8 936 bt_py3_cc_set_optional_iter_methods(cc,
81447b5b 937 bt_component_class_filter_set_notification_iterator_init_method,
811644b8 938 bt_component_class_filter_set_notification_iterator_finalize_method);
81447b5b
PP
939 register_cc_ptr_to_py_cls(cc, py_cls);
940 bt_component_class_freeze(cc);
941
942end:
943 return cc;
944}
945
946static struct bt_component_class *bt_py3_component_class_sink_create(
40910fbb
PP
947 PyObject *py_cls, const char *name, const char *description,
948 const char *help)
81447b5b
PP
949{
950 struct bt_component_class *cc;
951 int ret;
952
953 assert(py_cls);
954 cc = bt_component_class_sink_create(name, bt_py3_cc_sink_consume);
955 if (!cc) {
811644b8 956 BT_LOGE_STR("Cannot create sink component class.");
81447b5b
PP
957 goto end;
958 }
959
40910fbb 960 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
81447b5b 961 if (ret) {
811644b8 962 BT_LOGE_STR("Cannot set sink component class's optional attributes and methods.");
81447b5b
PP
963 BT_PUT(cc);
964 goto end;
965 }
966
967 register_cc_ptr_to_py_cls(cc, py_cls);
968 bt_component_class_freeze(cc);
969
970end:
971 return cc;
972}
81447b5b
PP
973%}
974
81447b5b
PP
975struct bt_component_class *bt_py3_component_class_source_create(
976 PyObject *py_cls, const char *name, const char *description,
811644b8 977 const char *help);
81447b5b
PP
978struct bt_component_class *bt_py3_component_class_filter_create(
979 PyObject *py_cls, const char *name, const char *description,
811644b8 980 const char *help);
81447b5b 981struct bt_component_class *bt_py3_component_class_sink_create(
40910fbb
PP
982 PyObject *py_cls, const char *name, const char *description,
983 const char *help);
81447b5b
PP
984void bt_py3_cc_init_from_bt2(void);
985void bt_py3_cc_exit_handler(void);
This page took 0.075243 seconds and 4 git commands to generate.