lib: Make bt_value_null a const pointer
[babeltrace.git] / bindings / python / bt2 / bt2 / native_btcomponentclass.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 /* Types */
26 struct bt_component_class;
27
28 /* Status */
29 enum bt_component_class_type {
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,
34 };
35
36 /* General functions */
37 const char *bt_component_class_get_name(
38 struct bt_component_class *component_class);
39 const char *bt_component_class_get_description(
40 struct bt_component_class *component_class);
41 const char *bt_component_class_get_help(
42 struct bt_component_class *component_class);
43 enum 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
65 static GHashTable *bt_cc_ptr_to_py_cls;
66
67 static void register_cc_ptr_to_py_cls(struct bt_component_class *bt_cc,
68 PyObject *py_cls)
69 {
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 BT_ASSERT(bt_cc_ptr_to_py_cls);
79 }
80
81 g_hash_table_insert(bt_cc_ptr_to_py_cls, (gpointer) bt_cc,
82 (gpointer) py_cls);
83 }
84
85 static PyObject *lookup_cc_ptr_to_py_cls(struct bt_component_class *bt_cc)
86 {
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
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
102 static PyObject *py_mod_bt2 = NULL;
103 static PyObject *py_mod_bt2_exc_error_type = NULL;
104 static PyObject *py_mod_bt2_exc_unsupported_feature_type = NULL;
105 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
106 static PyObject *py_mod_bt2_exc_stop_type = NULL;
107 static PyObject *py_mod_bt2_exc_port_connection_refused_type = NULL;
108 static PyObject *py_mod_bt2_exc_notif_iter_canceled_type = NULL;
109 static PyObject *py_mod_bt2_exc_invalid_query_object_type = NULL;
110 static PyObject *py_mod_bt2_exc_invalid_query_params_type = NULL;
111
112 static 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 BT_ASSERT(py_mod_bt2);
124 py_mod_bt2_exc_error_type =
125 PyObject_GetAttrString(py_mod_bt2, "Error");
126 BT_ASSERT(py_mod_bt2_exc_error_type);
127 py_mod_bt2_exc_unsupported_feature_type =
128 PyObject_GetAttrString(py_mod_bt2, "UnsupportedFeature");
129 BT_ASSERT(py_mod_bt2_exc_unsupported_feature_type);
130 py_mod_bt2_exc_try_again_type =
131 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
132 BT_ASSERT(py_mod_bt2_exc_try_again_type);
133 py_mod_bt2_exc_stop_type =
134 PyObject_GetAttrString(py_mod_bt2, "Stop");
135 BT_ASSERT(py_mod_bt2_exc_stop_type);
136 py_mod_bt2_exc_port_connection_refused_type =
137 PyObject_GetAttrString(py_mod_bt2, "PortConnectionRefused");
138 BT_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 BT_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 BT_ASSERT(py_mod_bt2_exc_invalid_query_params_type);
145 }
146
147 static 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
156 * the modules are still loaded, so it should be safe to use
157 * them even without a strong reference.
158 *
159 * We cannot do this in the library's destructor because it
160 * gets executed once Python is already finalized.
161 */
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);
167 Py_XDECREF(py_mod_bt2_exc_port_connection_refused_type);
168 Py_XDECREF(py_mod_bt2_exc_notif_iter_canceled_type);
169 Py_XDECREF(py_mod_bt2_exc_invalid_query_object_type);
170 Py_XDECREF(py_mod_bt2_exc_invalid_query_params_type);
171 }
172
173
174 /* Library destructor */
175
176 __attribute__((destructor))
177 static void bt_py3_native_comp_class_dtor(void) {
178 /* Destroy component class association hash table */
179 if (bt_cc_ptr_to_py_cls) {
180 BT_LOGD_STR("Destroying native component class to Python component class hash table.");
181 g_hash_table_destroy(bt_cc_ptr_to_py_cls);
182 }
183 }
184
185
186 /* Component class proxy methods (delegate to the attached Python object) */
187
188 static enum bt_notification_iterator_status bt_py3_exc_to_notif_iter_status(void)
189 {
190 enum bt_notification_iterator_status status =
191 BT_NOTIFICATION_ITERATOR_STATUS_OK;
192 PyObject *exc = PyErr_Occurred();
193
194 if (!exc) {
195 goto end;
196 }
197
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 }
210
211 end:
212 PyErr_Clear();
213 return status;
214 }
215
216 static 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
238 end:
239 PyErr_Clear();
240 return status;
241 }
242
243 static enum bt_component_status bt_py3_exc_to_component_status(void)
244 {
245 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
246 PyObject *exc = PyErr_Occurred();
247
248 if (!exc) {
249 goto end;
250 }
251
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;
266 }
267
268 end:
269 PyErr_Clear();
270 return status;
271 }
272
273 static 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(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 BT_ASSERT(comp);
288 BT_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);
299 goto error;
300 }
301
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.");
307 goto error;
308 }
309
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;
316 }
317
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);
331 goto error;
332 }
333
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;
341 goto end;
342
343 error:
344 status = BT_COMPONENT_STATUS_ERROR;
345
346 /*
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.
351 */
352 PyErr_Clear();
353
354 end:
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 }
362
363 static 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;
367
368 BT_ASSERT(py_comp);
369
370 /* Call user's _finalize() method */
371 py_method_result = PyObject_CallMethod(py_comp,
372 "_finalize", NULL);
373
374 if (PyErr_Occurred()) {
375 BT_LOGW("User's _finalize() method raised an exception: ignoring.");
376 }
377
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 }
387
388 static 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;
398
399 py_comp = bt_private_component_get_user_data(priv_comp);
400 BT_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 }
407
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 }
414
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 }
425
426 if (status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
427 /*
428 * Looks like the user method raised
429 * PortConnectionRefused: accept this like if it
430 * returned False.
431 */
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 }
438
439 BT_ASSERT(PyBool_Check(py_method_result));
440
441 if (py_method_result == Py_True) {
442 status = BT_COMPONENT_STATUS_OK;
443 } else {
444 status = BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION;
445 }
446
447 goto end;
448
449 error:
450 status = BT_COMPONENT_STATUS_ERROR;
451
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();
458
459 end:
460 Py_XDECREF(py_self_port_ptr);
461 Py_XDECREF(py_other_port_ptr);
462 Py_XDECREF(py_method_result);
463 return status;
464 }
465
466 static 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)
470 {
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;
475
476 py_comp = bt_private_component_get_user_data(priv_comp);
477 BT_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 BT_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 }
500
501 static 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 BT_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 BT_ASSERT(py_method_result == Py_None);
521 Py_XDECREF(py_port_ptr);
522 Py_XDECREF(py_method_result);
523 }
524
525 static struct bt_component_class_query_method_return bt_py3_cc_query(
526 struct bt_component_class *comp_cls,
527 struct bt_query_executor *query_exec,
528 const char *object, struct bt_value *params)
529 {
530 PyObject *py_cls = NULL;
531 PyObject *py_params_ptr = NULL;
532 PyObject *py_query_exec_ptr = NULL;
533 PyObject *py_query_func = NULL;
534 PyObject *py_object = NULL;
535 PyObject *py_results_addr = NULL;
536 struct bt_component_class_query_method_return ret = {
537 .status = BT_QUERY_STATUS_OK,
538 .result = NULL,
539 };
540
541 py_cls = lookup_cc_ptr_to_py_cls(comp_cls);
542 if (!py_cls) {
543 BT_LOGE("Cannot find Python class associated to native component class: "
544 "comp-cls-addr=%p", comp_cls);
545 goto error;
546 }
547
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.");
552 goto error;
553 }
554
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
562 py_object = SWIG_FromCharPtr(object);
563 if (!py_object) {
564 BT_LOGE_STR("Failed to create a Python string.");
565 goto error;
566 }
567
568 py_results_addr = PyObject_CallMethod(py_cls,
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;
582 }
583
584 if (py_results_addr == Py_NotImplemented) {
585 BT_LOGE_STR("User's _query() method is not implemented.");
586 goto error;
587 }
588
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 */
594 ret.result = (void *) PyLong_AsUnsignedLongLong(py_results_addr);
595 BT_ASSERT(!PyErr_Occurred());
596 BT_ASSERT(ret.result);
597 goto end;
598
599 error:
600 BT_PUT(ret.result);
601 PyErr_Clear();
602 ret.status = BT_QUERY_STATUS_ERROR;
603 BT_PUT(ret.result);
604
605 end:
606 Py_XDECREF(py_params_ptr);
607 Py_XDECREF(py_query_exec_ptr);
608 Py_XDECREF(py_query_func);
609 Py_XDECREF(py_object);
610 Py_XDECREF(py_results_addr);
611 return ret;
612 }
613
614 static enum bt_notification_iterator_status bt_py3_cc_notification_iterator_init(
615 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
616 struct bt_private_port *priv_port)
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;
625 struct bt_private_component *priv_comp =
626 bt_private_connection_private_notification_iterator_get_private_component(
627 priv_notif_iter);
628 PyObject *py_comp;
629
630 BT_ASSERT(priv_comp);
631 py_comp = bt_private_component_get_user_data(priv_comp);
632
633 /* Find user's Python notification iterator class */
634 py_comp_cls = PyObject_GetAttrString(py_comp, "__class__");
635 if (!py_comp_cls) {
636 BT_LOGE_STR("Cannot get Python object's `__class__` attribute.");
637 goto error;
638 }
639
640 py_iter_cls = PyObject_GetAttrString(py_comp_cls, "_iter_cls");
641 if (!py_iter_cls) {
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_connection_private_notification_iterator, 0);
648 if (!py_iter_ptr) {
649 BT_LOGE_STR("Failed to create a SWIG pointer object.");
650 goto error;
651 }
652
653 /*
654 * Create object with borrowed native notification iterator
655 * reference:
656 *
657 * py_iter = py_iter_cls.__new__(py_iter_cls, py_iter_ptr)
658 */
659 py_iter = PyObject_CallMethod(py_iter_cls, "__new__",
660 "(OO)", py_iter_cls, py_iter_ptr);
661 if (!py_iter) {
662 BT_LOGE("Failed to call Python class's __new__() method: "
663 "py-cls-addr=%p", py_iter_cls);
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
673 * function has access to self._component (which gives it the
674 * user Python component object from which the iterator was
675 * created).
676 */
677 py_init_method_result = PyObject_CallMethod(py_iter, "__init__", NULL);
678 if (!py_init_method_result) {
679 BT_LOGE_STR("User's __init__() method failed.");
680 goto error;
681 }
682
683 /*
684 * Since the Python code can never instantiate a user-defined
685 * notification iterator class, the native notification iterator
686 * object does NOT belong to a user Python notification iterator
687 * object (borrowed reference). However this Python object is
688 * owned by this native notification iterator object.
689 *
690 * In the Python world, the lifetime of the native notification
691 * iterator is managed by a _GenericNotificationIterator
692 * instance:
693 *
694 * _GenericNotificationIterator instance:
695 * owns a native bt_notification_iterator object (iter)
696 * owns a _UserNotificationIterator instance (py_iter)
697 * self._ptr is a borrowed reference to the
698 * native bt_private_connection_private_notification_iterator
699 * object (iter)
700 */
701 bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
702 py_iter);
703 py_iter = NULL;
704 goto end;
705
706 error:
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
723 end:
724 bt_put(priv_comp);
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
733 static void bt_py3_cc_notification_iterator_finalize(
734 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
735 {
736 PyObject *py_notif_iter =
737 bt_private_connection_private_notification_iterator_get_user_data(priv_notif_iter);
738 PyObject *py_method_result = NULL;
739
740 BT_ASSERT(py_notif_iter);
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 }
749
750 /*
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.
754 */
755 PyErr_Clear();
756 Py_XDECREF(py_method_result);
757 Py_DECREF(py_notif_iter);
758 }
759
760 static struct bt_notification_iterator_next_method_return
761 bt_py3_cc_notification_iterator_next(
762 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
763 {
764 struct bt_notification_iterator_next_method_return next_ret = {
765 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
766 .notification = NULL,
767 };
768 PyObject *py_notif_iter =
769 bt_private_connection_private_notification_iterator_get_user_data(priv_notif_iter);
770 PyObject *py_method_result = NULL;
771
772 BT_ASSERT(py_notif_iter);
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 BT_ASSERT(next_ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK);
778 goto end;
779 }
780
781 /*
782 * The returned object, on success, is an integer object
783 * (PyLong) containing the address of a native notification
784 * object (which is now ours).
785 */
786 next_ret.notification =
787 (struct bt_notification *) PyLong_AsUnsignedLongLong(
788 py_method_result);
789
790 /* Clear potential overflow error; should never happen */
791 BT_ASSERT(!PyErr_Occurred());
792 BT_ASSERT(next_ret.notification);
793 goto end;
794
795 end:
796 Py_XDECREF(py_method_result);
797 return next_ret;
798 }
799
800 static enum bt_component_status bt_py3_cc_sink_consume(
801 struct bt_private_component *priv_comp)
802 {
803 PyObject *py_comp = bt_private_component_get_user_data(priv_comp);
804 PyObject *py_method_result = NULL;
805 enum bt_component_status status;
806
807 BT_ASSERT(py_comp);
808 py_method_result = PyObject_CallMethod(py_comp,
809 "_consume", NULL);
810 status = bt_py3_exc_to_component_status();
811 if (!py_method_result && status == BT_COMPONENT_STATUS_OK) {
812 /* Pretty sure this should never happen, but just in case */
813 BT_LOGE("User's _consume() method failed without raising an exception: "
814 "status=%d", status);
815 status = BT_COMPONENT_STATUS_ERROR;
816 }
817
818 Py_XDECREF(py_method_result);
819 return status;
820 }
821
822
823 /* Component class creation functions (called from Python module) */
824
825 static int bt_py3_cc_set_optional_attrs_methods(struct bt_component_class *cc,
826 const char *description, const char *help)
827 {
828 int ret = 0;
829
830 if (description) {
831 ret = bt_component_class_set_description(cc, description);
832 if (ret) {
833 BT_LOGE("Cannot set component class's description: "
834 "comp-cls-addr=%p", cc);
835 goto end;
836 }
837 }
838
839 if (help) {
840 ret = bt_component_class_set_help(cc, help);
841 if (ret) {
842 BT_LOGE("Cannot set component class's help text: "
843 "comp-cls-addr=%p", cc);
844 goto end;
845 }
846 }
847
848 ret = bt_component_class_set_init_method(cc, bt_py3_cc_init);
849 BT_ASSERT(ret == 0);
850 ret = bt_component_class_set_finalize_method(cc, bt_py3_cc_finalize);
851 BT_ASSERT(ret == 0);
852 ret = bt_component_class_set_accept_port_connection_method(cc,
853 bt_py3_cc_accept_port_connection);
854 BT_ASSERT(ret == 0);
855 ret = bt_component_class_set_port_connected_method(cc,
856 bt_py3_cc_port_connected);
857 BT_ASSERT(ret == 0);
858 ret = bt_component_class_set_port_disconnected_method(cc,
859 bt_py3_cc_port_disconnected);
860 BT_ASSERT(ret == 0);
861 ret = bt_component_class_set_query_method(cc, bt_py3_cc_query);
862 BT_ASSERT(ret == 0);
863
864 end:
865 return ret;
866 }
867
868 static void bt_py3_cc_set_optional_iter_methods(struct bt_component_class *cc,
869 int (*set_notif_iter_init_method)(struct bt_component_class *, bt_component_class_notification_iterator_init_method),
870 int (*set_notif_iter_finalize_method)(struct bt_component_class *, bt_component_class_notification_iterator_finalize_method))
871 {
872 int ret __attribute__((unused));
873
874 ret = set_notif_iter_init_method(
875 cc, bt_py3_cc_notification_iterator_init);
876 BT_ASSERT(ret == 0);
877 ret = set_notif_iter_finalize_method(
878 cc, bt_py3_cc_notification_iterator_finalize);
879 BT_ASSERT(ret == 0);
880 }
881
882 static struct bt_component_class *bt_py3_component_class_source_create(
883 PyObject *py_cls, const char *name, const char *description,
884 const char *help)
885 {
886 struct bt_component_class *cc;
887 int ret;
888
889 BT_ASSERT(py_cls);
890 cc = bt_component_class_source_create(name,
891 bt_py3_cc_notification_iterator_next);
892 if (!cc) {
893 BT_LOGE_STR("Cannot create source component class.");
894 goto end;
895 }
896
897 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
898 if (ret) {
899 BT_LOGE_STR("Cannot set source component class's optional attributes and methods.");
900 BT_PUT(cc);
901 goto end;
902 }
903
904 bt_py3_cc_set_optional_iter_methods(cc,
905 bt_component_class_source_set_notification_iterator_init_method,
906 bt_component_class_source_set_notification_iterator_finalize_method);
907 register_cc_ptr_to_py_cls(cc, py_cls);
908 bt_component_class_freeze(cc);
909
910 end:
911 return cc;
912 }
913
914 static struct bt_component_class *bt_py3_component_class_filter_create(
915 PyObject *py_cls, const char *name, const char *description,
916 const char *help)
917 {
918 struct bt_component_class *cc;
919 int ret;
920
921 BT_ASSERT(py_cls);
922 cc = bt_component_class_filter_create(name,
923 bt_py3_cc_notification_iterator_next);
924 if (!cc) {
925 BT_LOGE_STR("Cannot create filter component class.");
926 goto end;
927 }
928
929 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
930 if (ret) {
931 BT_LOGE_STR("Cannot set filter component class's optional attributes and methods.");
932 BT_PUT(cc);
933 goto end;
934 }
935
936 bt_py3_cc_set_optional_iter_methods(cc,
937 bt_component_class_filter_set_notification_iterator_init_method,
938 bt_component_class_filter_set_notification_iterator_finalize_method);
939 register_cc_ptr_to_py_cls(cc, py_cls);
940 bt_component_class_freeze(cc);
941
942 end:
943 return cc;
944 }
945
946 static struct bt_component_class *bt_py3_component_class_sink_create(
947 PyObject *py_cls, const char *name, const char *description,
948 const char *help)
949 {
950 struct bt_component_class *cc;
951 int ret;
952
953 BT_ASSERT(py_cls);
954 cc = bt_component_class_sink_create(name, bt_py3_cc_sink_consume);
955 if (!cc) {
956 BT_LOGE_STR("Cannot create sink component class.");
957 goto end;
958 }
959
960 ret = bt_py3_cc_set_optional_attrs_methods(cc, description, help);
961 if (ret) {
962 BT_LOGE_STR("Cannot set sink component class's optional attributes and methods.");
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
970 end:
971 return cc;
972 }
973 %}
974
975 struct bt_component_class *bt_py3_component_class_source_create(
976 PyObject *py_cls, const char *name, const char *description,
977 const char *help);
978 struct bt_component_class *bt_py3_component_class_filter_create(
979 PyObject *py_cls, const char *name, const char *description,
980 const char *help);
981 struct bt_component_class *bt_py3_component_class_sink_create(
982 PyObject *py_cls, const char *name, const char *description,
983 const char *help);
984 void bt_py3_cc_init_from_bt2(void);
985 void bt_py3_cc_exit_handler(void);
This page took 0.05009 seconds and 4 git commands to generate.