lib: Make bt_value_null a const pointer
[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 {
f6a5e476
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{
f6a5e476
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);
8b45963b 78 BT_ASSERT(bt_cc_ptr_to_py_cls);
f6a5e476
PP
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{
f6a5e476
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;
f6a5e476 107static PyObject *py_mod_bt2_exc_port_connection_refused_type = NULL;
f6a5e476 108static PyObject *py_mod_bt2_exc_notif_iter_canceled_type = NULL;
1286dcbb
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");
8b45963b 123 BT_ASSERT(py_mod_bt2);
81447b5b
PP
124 py_mod_bt2_exc_error_type =
125 PyObject_GetAttrString(py_mod_bt2, "Error");
8b45963b 126 BT_ASSERT(py_mod_bt2_exc_error_type);
81447b5b
PP
127 py_mod_bt2_exc_unsupported_feature_type =
128 PyObject_GetAttrString(py_mod_bt2, "UnsupportedFeature");
8b45963b 129 BT_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");
8b45963b 132 BT_ASSERT(py_mod_bt2_exc_try_again_type);
81447b5b
PP
133 py_mod_bt2_exc_stop_type =
134 PyObject_GetAttrString(py_mod_bt2, "Stop");
8b45963b 135 BT_ASSERT(py_mod_bt2_exc_stop_type);
f6a5e476
PP
136 py_mod_bt2_exc_port_connection_refused_type =
137 PyObject_GetAttrString(py_mod_bt2, "PortConnectionRefused");
8b45963b 138 BT_ASSERT(py_mod_bt2_exc_port_connection_refused_type);
1286dcbb
PP
139 py_mod_bt2_exc_invalid_query_object_type =
140 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryObject");
8b45963b 141 BT_ASSERT(py_mod_bt2_exc_invalid_query_object_type);
1286dcbb
PP
142 py_mod_bt2_exc_invalid_query_params_type =
143 PyObject_GetAttrString(py_mod_bt2, "InvalidQueryParams");
8b45963b 144 BT_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
f6a5e476
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);
f6a5e476 167 Py_XDECREF(py_mod_bt2_exc_port_connection_refused_type);
f6a5e476 168 Py_XDECREF(py_mod_bt2_exc_notif_iter_canceled_type);
1286dcbb
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
f6a5e476 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) {
f6a5e476 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
f6a5e476 186/* Component class proxy methods (delegate to the attached Python object) */
81447b5b 187
f6a5e476 188static enum bt_notification_iterator_status bt_py3_exc_to_notif_iter_status(void)
81447b5b 189{
f6a5e476
PP
190 enum bt_notification_iterator_status status =
191 BT_NOTIFICATION_ITERATOR_STATUS_OK;
192 PyObject *exc = PyErr_Occurred();
81447b5b 193
f6a5e476 194 if (!exc) {
81447b5b
PP
195 goto end;
196 }
197
f6a5e476
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();
f6a5e476 213 return status;
81447b5b
PP
214}
215
1286dcbb
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
f6a5e476 243static enum bt_component_status bt_py3_exc_to_component_status(void)
81447b5b 244{
f6a5e476
PP
245 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
246 PyObject *exc = PyErr_Occurred();
81447b5b 247
f6a5e476
PP
248 if (!exc) {
249 goto end;
81447b5b
PP
250 }
251
f6a5e476
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
f6a5e476
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 =
41765449 278 bt_component_from_private(priv_comp);
f6a5e476
PP
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;
8b45963b
PP
287 BT_ASSERT(comp);
288 BT_ASSERT(comp_cls);
f6a5e476
PP
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
f6a5e476
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
f6a5e476
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
f6a5e476
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
f6a5e476
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:
f6a5e476 344 status = BT_COMPONENT_STATUS_ERROR;
81447b5b
PP
345
346 /*
f6a5e476
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 */
f6a5e476 352 PyErr_Clear();
81447b5b 353
f6a5e476
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
f6a5e476
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
8b45963b 368 BT_ASSERT(py_comp);
81447b5b 369
f6a5e476
PP
370 /* Call user's _finalize() method */
371 py_method_result = PyObject_CallMethod(py_comp,
372 "_finalize", NULL);
81447b5b 373
f6a5e476
PP
374 if (PyErr_Occurred()) {
375 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
376 }
81447b5b 377
f6a5e476
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
f6a5e476
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
f6a5e476 399 py_comp = bt_private_component_get_user_data(priv_comp);
8b45963b 400 BT_ASSERT(py_comp);
f6a5e476
PP
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
f6a5e476
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
f6a5e476
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
f6a5e476 426 if (status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
81447b5b 427 /*
f6a5e476
PP
428 * Looks like the user method raised
429 * PortConnectionRefused: accept this like if it
430 * returned False.
81447b5b 431 */
f6a5e476
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
8b45963b 439 BT_ASSERT(PyBool_Check(py_method_result));
81447b5b 440
f6a5e476
PP
441 if (py_method_result == Py_True) {
442 status = BT_COMPONENT_STATUS_OK;
81447b5b 443 } else {
f6a5e476
PP
444 status = BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION;
445 }
81447b5b 446
f6a5e476 447 goto end;
81447b5b 448
f6a5e476
PP
449error:
450 status = BT_COMPONENT_STATUS_ERROR;
81447b5b 451
f6a5e476
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
f6a5e476
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
f6a5e476
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{
f6a5e476
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
f6a5e476 476 py_comp = bt_private_component_get_user_data(priv_comp);
8b45963b 477 BT_ASSERT(py_comp);
f6a5e476
PP
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);
8b45963b 495 BT_ASSERT(py_method_result == Py_None);
f6a5e476
PP
496 Py_XDECREF(py_self_port_ptr);
497 Py_XDECREF(py_other_port_ptr);
498 Py_XDECREF(py_method_result);
499}
81447b5b 500
f6a5e476
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);
8b45963b 510 BT_ASSERT(py_comp);
f6a5e476
PP
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);
8b45963b 520 BT_ASSERT(py_method_result == Py_None);
f6a5e476
PP
521 Py_XDECREF(py_port_ptr);
522 Py_XDECREF(py_method_result);
81447b5b
PP
523}
524
fe7265b5 525static struct bt_component_class_query_method_return bt_py3_cc_query(
f6a5e476 526 struct bt_component_class *comp_cls,
1286dcbb 527 struct bt_query_executor *query_exec,
a67681c1 528 const char *object, struct bt_value *params)
911dec08
PP
529{
530 PyObject *py_cls = NULL;
f6a5e476 531 PyObject *py_params_ptr = NULL;
1286dcbb 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;
fe7265b5 536 struct bt_component_class_query_method_return ret = {
1286dcbb
PP
537 .status = BT_QUERY_STATUS_OK,
538 .result = NULL,
539 };
911dec08 540
f6a5e476 541 py_cls = lookup_cc_ptr_to_py_cls(comp_cls);
911dec08 542 if (!py_cls) {
f6a5e476
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
f6a5e476
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
1286dcbb
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) {
f6a5e476 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,
1286dcbb
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
f6a5e476
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
f6a5e476
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 */
1286dcbb 594 ret.result = (void *) PyLong_AsUnsignedLongLong(py_results_addr);
8b45963b
PP
595 BT_ASSERT(!PyErr_Occurred());
596 BT_ASSERT(ret.result);
911dec08
PP
597 goto end;
598
599error:
1286dcbb 600 BT_PUT(ret.result);
911dec08 601 PyErr_Clear();
1286dcbb
PP
602 ret.status = BT_QUERY_STATUS_ERROR;
603 BT_PUT(ret.result);
911dec08
PP
604
605end:
f6a5e476 606 Py_XDECREF(py_params_ptr);
1286dcbb 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);
1286dcbb 611 return ret;
911dec08
PP
612}
613
81447b5b 614static enum bt_notification_iterator_status bt_py3_cc_notification_iterator_init(
fe7265b5 615 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
f6a5e476 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;
f6a5e476 625 struct bt_private_component *priv_comp =
fe7265b5 626 bt_private_connection_private_notification_iterator_get_private_component(
f6a5e476
PP
627 priv_notif_iter);
628 PyObject *py_comp;
629
8b45963b 630 BT_ASSERT(priv_comp);
f6a5e476 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) {
f6a5e476 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) {
f6a5e476
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),
fe7265b5 647 SWIGTYPE_p_bt_private_connection_private_notification_iterator, 0);
f6a5e476
PP
648 if (!py_iter_ptr) {
649 BT_LOGE_STR("Failed to create a SWIG pointer object.");
81447b5b
PP
650 goto error;
651 }
652
653 /*
f6a5e476 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) {
f6a5e476
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
f6a5e476
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) {
f6a5e476 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
f6a5e476 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
f6a5e476 688 * owned by this native notification iterator object.
81447b5b 689 *
f6a5e476 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)
f6a5e476 696 * owns a _UserNotificationIterator instance (py_iter)
81447b5b 697 * self._ptr is a borrowed reference to the
fe7265b5 698 * native bt_private_connection_private_notification_iterator
f6a5e476 699 * object (iter)
81447b5b 700 */
fe7265b5 701 bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
f6a5e476 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:
f6a5e476 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
f6a5e476 733static void bt_py3_cc_notification_iterator_finalize(
fe7265b5 734 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
81447b5b
PP
735{
736 PyObject *py_notif_iter =
fe7265b5 737 bt_private_connection_private_notification_iterator_get_user_data(priv_notif_iter);
f6a5e476 738 PyObject *py_method_result = NULL;
81447b5b 739
8b45963b 740 BT_ASSERT(py_notif_iter);
f6a5e476
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 /*
f6a5e476
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();
f6a5e476 756 Py_XDECREF(py_method_result);
81447b5b
PP
757 Py_DECREF(py_notif_iter);
758}
759
fe7265b5 760static struct bt_notification_iterator_next_method_return
f6a5e476 761bt_py3_cc_notification_iterator_next(
fe7265b5 762 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
81447b5b 763{
fe7265b5 764 struct bt_notification_iterator_next_method_return next_ret = {
f6a5e476
PP
765 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
766 .notification = NULL,
767 };
81447b5b 768 PyObject *py_notif_iter =
fe7265b5 769 bt_private_connection_private_notification_iterator_get_user_data(priv_notif_iter);
f6a5e476 770 PyObject *py_method_result = NULL;
81447b5b 771
8b45963b 772 BT_ASSERT(py_notif_iter);
f6a5e476
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();
8b45963b 777 BT_ASSERT(next_ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK);
f6a5e476 778 goto end;
81447b5b
PP
779 }
780
781 /*
782 * The returned object, on success, is an integer object
f6a5e476 783 * (PyLong) containing the address of a native notification
911dec08 784 * object (which is now ours).
81447b5b 785 */
f6a5e476
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 */
8b45963b
PP
791 BT_ASSERT(!PyErr_Occurred());
792 BT_ASSERT(next_ret.notification);
81447b5b
PP
793 goto end;
794
81447b5b 795end:
f6a5e476
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(
f6a5e476 801 struct bt_private_component *priv_comp)
81447b5b 802{
f6a5e476
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
8b45963b 807 BT_ASSERT(py_comp);
f6a5e476 808 py_method_result = PyObject_CallMethod(py_comp,
81447b5b
PP
809 "_consume", NULL);
810 status = bt_py3_exc_to_component_status();
f6a5e476 811 if (!py_method_result && status == BT_COMPONENT_STATUS_OK) {
81447b5b 812 /* Pretty sure this should never happen, but just in case */
f6a5e476
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
f6a5e476 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) {
f6a5e476
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) {
f6a5e476
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);
8b45963b 849 BT_ASSERT(ret == 0);
f6a5e476 850 ret = bt_component_class_set_finalize_method(cc, bt_py3_cc_finalize);
8b45963b 851 BT_ASSERT(ret == 0);
f6a5e476
PP
852 ret = bt_component_class_set_accept_port_connection_method(cc,
853 bt_py3_cc_accept_port_connection);
8b45963b 854 BT_ASSERT(ret == 0);
f6a5e476
PP
855 ret = bt_component_class_set_port_connected_method(cc,
856 bt_py3_cc_port_connected);
8b45963b 857 BT_ASSERT(ret == 0);
f6a5e476
PP
858 ret = bt_component_class_set_port_disconnected_method(cc,
859 bt_py3_cc_port_disconnected);
8b45963b 860 BT_ASSERT(ret == 0);
a67681c1 861 ret = bt_component_class_set_query_method(cc, bt_py3_cc_query);
8b45963b 862 BT_ASSERT(ret == 0);
911dec08 863
81447b5b
PP
864end:
865 return ret;
866}
867
f6a5e476 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),
f6a5e476 870 int (*set_notif_iter_finalize_method)(struct bt_component_class *, bt_component_class_notification_iterator_finalize_method))
81447b5b 871{
dac20463 872 int ret __attribute__((unused));
81447b5b
PP
873
874 ret = set_notif_iter_init_method(
875 cc, bt_py3_cc_notification_iterator_init);
8b45963b 876 BT_ASSERT(ret == 0);
f6a5e476
PP
877 ret = set_notif_iter_finalize_method(
878 cc, bt_py3_cc_notification_iterator_finalize);
8b45963b 879 BT_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,
f6a5e476 884 const char *help)
81447b5b
PP
885{
886 struct bt_component_class *cc;
887 int ret;
888
8b45963b 889 BT_ASSERT(py_cls);
81447b5b 890 cc = bt_component_class_source_create(name,
81447b5b
PP
891 bt_py3_cc_notification_iterator_next);
892 if (!cc) {
f6a5e476 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) {
f6a5e476 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
f6a5e476 904 bt_py3_cc_set_optional_iter_methods(cc,
81447b5b 905 bt_component_class_source_set_notification_iterator_init_method,
f6a5e476 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,
f6a5e476 916 const char *help)
81447b5b
PP
917{
918 struct bt_component_class *cc;
919 int ret;
920
8b45963b 921 BT_ASSERT(py_cls);
81447b5b 922 cc = bt_component_class_filter_create(name,
81447b5b
PP
923 bt_py3_cc_notification_iterator_next);
924 if (!cc) {
f6a5e476 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) {
f6a5e476 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
f6a5e476 936 bt_py3_cc_set_optional_iter_methods(cc,
81447b5b 937 bt_component_class_filter_set_notification_iterator_init_method,
f6a5e476 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
8b45963b 953 BT_ASSERT(py_cls);
81447b5b
PP
954 cc = bt_component_class_sink_create(name, bt_py3_cc_sink_consume);
955 if (!cc) {
f6a5e476 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) {
f6a5e476 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,
f6a5e476 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,
f6a5e476 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.080467 seconds and 4 git commands to generate.