17305dd5ef7ab859ea1affa1837144f5c196cfc1
[babeltrace.git] / src / python-plugin-provider / python-plugin-provider.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
5 *
6 * Babeltrace Python plugin provider
7 */
8
9 #define BT_LOG_TAG "LIB/PLUGIN-PY"
10 #include "lib/logging.h"
11
12 #include "python-plugin-provider.h"
13
14 #include "common/macros.h"
15 #include "compat/compiler.h"
16 #include <babeltrace2/plugin/plugin-loading.h>
17 #include "lib/plugin/plugin.h"
18 #include <babeltrace2/graph/component-class.h>
19 #include <babeltrace2/error-reporting.h>
20 #include "lib/graph/component-class.h"
21 #include "py-common/py-common.h"
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <signal.h>
25 #include <Python.h>
26 #include <glib.h>
27
28 #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_"
29 #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1)
30 #define PYTHON_PLUGIN_FILE_EXT ".py"
31 #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1)
32
33 static enum python_state {
34 /* init_python() not called yet */
35 PYTHON_STATE_NOT_INITED,
36
37 /* init_python() called once with success */
38 PYTHON_STATE_FULLY_INITIALIZED,
39
40 /* init_python() called once without success */
41 PYTHON_STATE_CANNOT_INITIALIZE,
42
43 /*
44 * init_python() called, but environment variable asks the
45 * Python interpreter not to be loaded.
46 */
47 PYTHON_STATE_WONT_INITIALIZE,
48 } python_state = PYTHON_STATE_NOT_INITED;
49
50 static PyObject *py_try_load_plugin_module_func = NULL;
51 static bool python_was_initialized_by_us;
52
53 static
54 void append_python_traceback_error_cause(void)
55 {
56 GString *exc = NULL;
57
58 if (Py_IsInitialized() && PyErr_Occurred()) {
59 exc = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL);
60 if (!exc) {
61 BT_LOGE_STR("Failed to format Python exception.");
62 goto end;
63 }
64
65 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
66 BT_LIB_LOG_LIBBABELTRACE2_NAME, "%s", exc->str);
67 }
68
69 end:
70 if (exc) {
71 g_string_free(exc, TRUE);
72 }
73 }
74
75 static
76 void log_python_traceback(int log_level)
77 {
78 GString *exc = NULL;
79
80 if (Py_IsInitialized() && PyErr_Occurred()) {
81 exc = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL);
82 if (!exc) {
83 BT_LOGE_STR("Failed to format Python exception.");
84 goto end;
85 }
86
87 BT_LOG_WRITE_PRINTF(log_level, BT_LOG_TAG,
88 "Exception occurred: Python traceback:\n%s", exc->str);
89 }
90
91 end:
92 if (exc) {
93 g_string_free(exc, TRUE);
94 }
95 }
96
97 static
98 void pyerr_clear(void)
99 {
100 if (Py_IsInitialized()) {
101 PyErr_Clear();
102 }
103 }
104
105 static
106 int init_python(void)
107 {
108 int ret = BT_FUNC_STATUS_OK;
109 PyObject *py_bt2_py_plugin_mod = NULL;
110 const char *dis_python_env;
111 #ifndef __MINGW32__
112 sig_t old_sigint = signal(SIGINT, SIG_DFL);
113 #endif
114
115 switch (python_state) {
116 case PYTHON_STATE_NOT_INITED:
117 break;
118 case PYTHON_STATE_FULLY_INITIALIZED:
119 goto end;
120 case PYTHON_STATE_WONT_INITIALIZE:
121 ret = BT_FUNC_STATUS_NOT_FOUND;
122 goto end;
123 case PYTHON_STATE_CANNOT_INITIALIZE:
124 ret = BT_FUNC_STATUS_ERROR;
125 goto end;
126 default:
127 bt_common_abort();
128 }
129
130 /*
131 * User can disable Python plugin support with the
132 * `LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` environment variable
133 * set to 1.
134 */
135 dis_python_env = getenv("LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS");
136 if (dis_python_env && strcmp(dis_python_env, "1") == 0) {
137 BT_LOGI_STR("Python plugin support is disabled because the "
138 "`LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` environment "
139 "variable is set to `1`.");
140 python_state = PYTHON_STATE_WONT_INITIALIZE;
141 ret = BT_FUNC_STATUS_NOT_FOUND;
142 goto end;
143 }
144
145 if (!Py_IsInitialized()) {
146 BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
147 Py_InitializeEx(0);
148 python_was_initialized_by_us = true;
149 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
150 Py_GetVersion());
151 } else {
152 BT_LOGI("Python interpreter is already initialized: version=\"%s\"",
153 Py_GetVersion());
154 }
155
156 py_bt2_py_plugin_mod = PyImport_ImportModule("bt2.py_plugin");
157 if (!py_bt2_py_plugin_mod) {
158 append_python_traceback_error_cause();
159 BT_LIB_LOGW_APPEND_CAUSE(
160 "Cannot import `bt2.py_plugin` Python module: "
161 "Python plugin support is disabled.");
162 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
163 ret = BT_FUNC_STATUS_ERROR;
164 goto end;
165 }
166
167 py_try_load_plugin_module_func =
168 PyObject_GetAttrString(py_bt2_py_plugin_mod, "_try_load_plugin_module");
169 if (!py_try_load_plugin_module_func) {
170 append_python_traceback_error_cause();
171 BT_LIB_LOGW_APPEND_CAUSE(
172 "Cannot get `_try_load_plugin_module` attribute from `bt2.py_plugin` Python module: "
173 "Python plugin support is disabled.");
174 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
175 ret = BT_FUNC_STATUS_ERROR;
176 goto end;
177 }
178
179 python_state = PYTHON_STATE_FULLY_INITIALIZED;
180
181 end:
182 #ifndef __MINGW32__
183 if (old_sigint != SIG_ERR) {
184 (void) signal(SIGINT, old_sigint);
185 }
186 #endif
187
188 log_python_traceback(ret == BT_FUNC_STATUS_ERROR ?
189 BT_LOG_WARNING : BT_LOG_INFO);
190 pyerr_clear();
191 Py_XDECREF(py_bt2_py_plugin_mod);
192 return ret;
193 }
194
195 __attribute__((destructor)) static
196 void fini_python(void) {
197 if (Py_IsInitialized() && python_was_initialized_by_us) {
198 if (py_try_load_plugin_module_func) {
199 Py_DECREF(py_try_load_plugin_module_func);
200 py_try_load_plugin_module_func = NULL;
201 }
202
203 Py_Finalize();
204 BT_LOGI_STR("Finalized Python interpreter.");
205 }
206
207 python_state = PYTHON_STATE_NOT_INITED;
208 }
209
210 static
211 int bt_plugin_from_python_plugin_info(PyObject *plugin_info,
212 bool fail_on_load_error, bt_plugin **plugin_out)
213 {
214 int status = BT_FUNC_STATUS_OK;
215 PyObject *py_name = NULL;
216 PyObject *py_author = NULL;
217 PyObject *py_description = NULL;
218 PyObject *py_license = NULL;
219 PyObject *py_version = NULL;
220 PyObject *py_comp_class_addrs = NULL;
221 const char *name = NULL;
222 const char *author = NULL;
223 const char *description = NULL;
224 const char *license = NULL;
225 unsigned int major = 0, minor = 0, patch = 0;
226 const char *version_extra = NULL;
227
228 BT_ASSERT(plugin_out);
229 *plugin_out = NULL;
230 BT_ASSERT(plugin_info);
231 BT_ASSERT(python_state == PYTHON_STATE_FULLY_INITIALIZED);
232 py_name = PyObject_GetAttrString(plugin_info, "name");
233 if (!py_name) {
234 if (fail_on_load_error) {
235 append_python_traceback_error_cause();
236 BT_LIB_LOGW_APPEND_CAUSE(
237 "Cannot find `name` attribute in Python plugin info object: "
238 "py-plugin-info-addr=%p", plugin_info);
239 status = BT_FUNC_STATUS_ERROR;
240 } else {
241 BT_LIB_LOGW(
242 "Cannot find `name` attribute in Python plugin info object: "
243 "py-plugin-info-addr=%p", plugin_info);
244 status = BT_FUNC_STATUS_NOT_FOUND;
245 }
246
247 goto error;
248 }
249
250 py_author = PyObject_GetAttrString(plugin_info, "author");
251 if (!py_author) {
252 if (fail_on_load_error) {
253 append_python_traceback_error_cause();
254 BT_LIB_LOGW_APPEND_CAUSE(
255 "Cannot find `author` attribute in Python plugin info object: "
256 "py-plugin-info-addr=%p", plugin_info);
257 status = BT_FUNC_STATUS_ERROR;
258 } else {
259 BT_LIB_LOGW(
260 "Cannot find `author` attribute in Python plugin info object: "
261 "py-plugin-info-addr=%p", plugin_info);
262 status = BT_FUNC_STATUS_NOT_FOUND;
263 }
264
265 goto error;
266 }
267
268 py_description = PyObject_GetAttrString(plugin_info, "description");
269 if (!py_description) {
270 if (fail_on_load_error) {
271 append_python_traceback_error_cause();
272 BT_LIB_LOGW_APPEND_CAUSE(
273 "Cannot find `description` attribute in Python plugin info object: "
274 "py-plugin-info-addr=%p", plugin_info);
275 status = BT_FUNC_STATUS_ERROR;
276 } else {
277 BT_LIB_LOGW(
278 "Cannot find `description` attribute in Python plugin info object: "
279 "py-plugin-info-addr=%p", plugin_info);
280 status = BT_FUNC_STATUS_NOT_FOUND;
281 }
282
283 goto error;
284 }
285
286 py_license = PyObject_GetAttrString(plugin_info, "license");
287 if (!py_license) {
288 if (fail_on_load_error) {
289 append_python_traceback_error_cause();
290 BT_LIB_LOGW_APPEND_CAUSE(
291 "Cannot find `license` attribute in Python plugin info object: "
292 "py-plugin-info-addr=%p", plugin_info);
293 status = BT_FUNC_STATUS_ERROR;
294 } else {
295 BT_LIB_LOGW(
296 "Cannot find `license` attribute in Python plugin info object: "
297 "py-plugin-info-addr=%p", plugin_info);
298 status = BT_FUNC_STATUS_NOT_FOUND;
299 }
300
301 goto error;
302 }
303
304 py_version = PyObject_GetAttrString(plugin_info, "version");
305 if (!py_version) {
306 if (fail_on_load_error) {
307 append_python_traceback_error_cause();
308 BT_LIB_LOGW_APPEND_CAUSE(
309 "Cannot find `version` attribute in Python plugin info object: "
310 "py-plugin-info-addr=%p", plugin_info);
311 status = BT_FUNC_STATUS_ERROR;
312 } else {
313 BT_LIB_LOGW(
314 "Cannot find `version` attribute in Python plugin info object: "
315 "py-plugin-info-addr=%p", plugin_info);
316 status = BT_FUNC_STATUS_NOT_FOUND;
317 }
318
319 goto error;
320 }
321
322 py_comp_class_addrs = PyObject_GetAttrString(plugin_info,
323 "comp_class_addrs");
324 if (!py_comp_class_addrs) {
325 if (fail_on_load_error) {
326 append_python_traceback_error_cause();
327 BT_LIB_LOGW_APPEND_CAUSE(
328 "Cannot find `comp_class_addrs` attribute in Python plugin info object: "
329 "py-plugin-info-addr=%p", plugin_info);
330 status = BT_FUNC_STATUS_ERROR;
331 } else {
332 BT_LIB_LOGW(
333 "Cannot find `comp_class_addrs` attribute in Python plugin info object: "
334 "py-plugin-info-addr=%p", plugin_info);
335 status = BT_FUNC_STATUS_NOT_FOUND;
336 }
337
338 goto error;
339 }
340
341 if (PyUnicode_Check(py_name)) {
342 name = PyUnicode_AsUTF8(py_name);
343 if (!name) {
344 if (fail_on_load_error) {
345 append_python_traceback_error_cause();
346 BT_LIB_LOGW_APPEND_CAUSE(
347 "Cannot decode Python plugin name string: "
348 "py-plugin-info-addr=%p", plugin_info);
349 status = BT_FUNC_STATUS_ERROR;
350 } else {
351 BT_LIB_LOGW(
352 "Cannot decode Python plugin name string: "
353 "py-plugin-info-addr=%p", plugin_info);
354 status = BT_FUNC_STATUS_NOT_FOUND;
355 }
356
357 goto error;
358 }
359 } else {
360 /* Plugin name is mandatory */
361 if (fail_on_load_error) {
362 append_python_traceback_error_cause();
363 BT_LIB_LOGW_APPEND_CAUSE(
364 "Plugin name is not a string: "
365 "py-plugin-info-addr=%p", plugin_info);
366 status = BT_FUNC_STATUS_ERROR;
367 } else {
368 BT_LIB_LOGW(
369 "Plugin name is not a string: "
370 "py-plugin-info-addr=%p", plugin_info);
371 status = BT_FUNC_STATUS_NOT_FOUND;
372 }
373
374 goto error;
375 }
376
377 if (PyUnicode_Check(py_author)) {
378 author = PyUnicode_AsUTF8(py_author);
379 if (!author) {
380 if (fail_on_load_error) {
381 append_python_traceback_error_cause();
382 BT_LIB_LOGW_APPEND_CAUSE(
383 "Cannot decode Python plugin author string: "
384 "py-plugin-info-addr=%p", plugin_info);
385 status = BT_FUNC_STATUS_ERROR;
386 } else {
387 BT_LIB_LOGW(
388 "Cannot decode Python plugin author string: "
389 "py-plugin-info-addr=%p", plugin_info);
390 status = BT_FUNC_STATUS_NOT_FOUND;
391 }
392
393 goto error;
394 }
395 }
396
397 if (PyUnicode_Check(py_description)) {
398 description = PyUnicode_AsUTF8(py_description);
399 if (!description) {
400 if (fail_on_load_error) {
401 append_python_traceback_error_cause();
402 BT_LIB_LOGW_APPEND_CAUSE(
403 "Cannot decode Python plugin description string: "
404 "py-plugin-info-addr=%p", plugin_info);
405 status = BT_FUNC_STATUS_ERROR;
406 } else {
407 BT_LIB_LOGW(
408 "Cannot decode Python plugin description string: "
409 "py-plugin-info-addr=%p", plugin_info);
410 status = BT_FUNC_STATUS_NOT_FOUND;
411 }
412
413 goto error;
414 }
415 }
416
417 if (PyUnicode_Check(py_license)) {
418 license = PyUnicode_AsUTF8(py_license);
419 if (!license) {
420 if (fail_on_load_error) {
421 append_python_traceback_error_cause();
422 BT_LIB_LOGW_APPEND_CAUSE(
423 "Cannot decode Python plugin license string: "
424 "py-plugin-info-addr=%p", plugin_info);
425 status = BT_FUNC_STATUS_ERROR;
426 } else {
427 BT_LIB_LOGW(
428 "Cannot decode Python plugin license string: "
429 "py-plugin-info-addr=%p", plugin_info);
430 status = BT_FUNC_STATUS_NOT_FOUND;
431 }
432
433 goto error;
434 }
435 }
436
437 if (PyTuple_Check(py_version)) {
438 if (PyTuple_Size(py_version) >= 3) {
439 PyObject *py_major = PyTuple_GetItem(py_version, 0);
440 PyObject *py_minor = PyTuple_GetItem(py_version, 1);
441 PyObject *py_patch = PyTuple_GetItem(py_version, 2);
442
443 BT_ASSERT(py_major);
444 BT_ASSERT(py_minor);
445 BT_ASSERT(py_patch);
446
447 if (PyLong_Check(py_major)) {
448 major = PyLong_AsUnsignedLong(py_major);
449 }
450
451 if (PyLong_Check(py_minor)) {
452 minor = PyLong_AsUnsignedLong(py_minor);
453 }
454
455 if (PyLong_Check(py_patch)) {
456 patch = PyLong_AsUnsignedLong(py_patch);
457 }
458
459 if (PyErr_Occurred()) {
460 /* Overflow error, most probably */
461 if (fail_on_load_error) {
462 append_python_traceback_error_cause();
463 BT_LIB_LOGW_APPEND_CAUSE(
464 "Invalid Python plugin version format: "
465 "py-plugin-info-addr=%p", plugin_info);
466 status = BT_FUNC_STATUS_ERROR;
467 } else {
468 BT_LIB_LOGW(
469 "Invalid Python plugin version format: "
470 "py-plugin-info-addr=%p", plugin_info);
471 status = BT_FUNC_STATUS_NOT_FOUND;
472 }
473
474 goto error;
475 }
476 }
477
478 if (PyTuple_Size(py_version) >= 4) {
479 PyObject *py_extra = PyTuple_GetItem(py_version, 3);
480
481 BT_ASSERT(py_extra);
482
483 if (PyUnicode_Check(py_extra)) {
484 version_extra = PyUnicode_AsUTF8(py_extra);
485 if (!version_extra) {
486 if (fail_on_load_error) {
487 append_python_traceback_error_cause();
488 BT_LIB_LOGW_APPEND_CAUSE(
489 "Cannot decode Python plugin version's extra string: "
490 "py-plugin-info-addr=%p", plugin_info);
491 status = BT_FUNC_STATUS_ERROR;
492 } else {
493 BT_LIB_LOGW(
494 "Cannot decode Python plugin version's extra string: "
495 "py-plugin-info-addr=%p", plugin_info);
496 status = BT_FUNC_STATUS_NOT_FOUND;
497 }
498
499 goto error;
500 }
501 }
502 }
503 }
504
505 *plugin_out = bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON);
506 if (!*plugin_out) {
507 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin object.");
508 status = BT_FUNC_STATUS_MEMORY_ERROR;
509 goto error;
510 }
511
512 bt_plugin_set_name(*plugin_out, name);
513
514 if (description) {
515 bt_plugin_set_description(*plugin_out, description);
516 }
517
518 if (author) {
519 bt_plugin_set_author(*plugin_out, author);
520 }
521
522 if (license) {
523 bt_plugin_set_license(*plugin_out, license);
524 }
525
526 bt_plugin_set_version(*plugin_out, major, minor, patch, version_extra);
527
528 if (PyList_Check(py_comp_class_addrs)) {
529 size_t i;
530
531 for (i = 0; i < PyList_Size(py_comp_class_addrs); i++) {
532 bt_component_class *comp_class;
533 PyObject *py_comp_class_addr;
534
535 py_comp_class_addr =
536 PyList_GetItem(py_comp_class_addrs, i);
537 BT_ASSERT(py_comp_class_addr);
538 if (PyLong_Check(py_comp_class_addr)) {
539 comp_class = PyLong_AsVoidPtr(py_comp_class_addr);
540 } else {
541 if (fail_on_load_error) {
542 append_python_traceback_error_cause();
543 BT_LIB_LOGW_APPEND_CAUSE(
544 "Component class address is not an integer in Python plugin info object: "
545 "py-plugin-info-addr=%p, index=%zu",
546 plugin_info, i);
547 status = BT_FUNC_STATUS_ERROR;
548 } else {
549 BT_LIB_LOGW(
550 "Component class address is not an integer in Python plugin info object: "
551 "py-plugin-info-addr=%p, index=%zu",
552 plugin_info, i);
553 status = BT_FUNC_STATUS_NOT_FOUND;
554 }
555
556 continue;
557 }
558
559 status = bt_plugin_add_component_class(*plugin_out,
560 comp_class);
561 if (status < 0) {
562 BT_LIB_LOGE_APPEND_CAUSE(
563 "Cannot add component class to plugin: "
564 "py-plugin-info-addr=%p, "
565 "plugin-addr=%p, plugin-name=\"%s\", "
566 "comp-class-addr=%p, "
567 "comp-class-name=\"%s\", "
568 "comp-class-type=%s",
569 plugin_info, *plugin_out,
570 bt_plugin_get_name(*plugin_out),
571 comp_class,
572 bt_component_class_get_name(comp_class),
573 bt_common_component_class_type_string(
574 bt_component_class_get_type(comp_class)));
575 goto error;
576 }
577 }
578 }
579
580 goto end;
581
582 error:
583 BT_ASSERT(status != BT_FUNC_STATUS_OK);
584 log_python_traceback(fail_on_load_error ? BT_LOG_WARNING : BT_LOG_INFO);
585 pyerr_clear();
586 BT_OBJECT_PUT_REF_AND_RESET(*plugin_out);
587
588 end:
589 Py_XDECREF(py_name);
590 Py_XDECREF(py_author);
591 Py_XDECREF(py_description);
592 Py_XDECREF(py_license);
593 Py_XDECREF(py_version);
594 Py_XDECREF(py_comp_class_addrs);
595 return status;
596 }
597
598 BT_EXPORT
599 int bt_plugin_python_create_all_from_file(const char *path,
600 bool fail_on_load_error, struct bt_plugin_set **plugin_set_out)
601 {
602 bt_plugin *plugin = NULL;
603 PyObject *py_plugin_info = NULL;
604 gchar *basename = NULL;
605 size_t path_len;
606 int status = BT_FUNC_STATUS_OK;
607
608 BT_ASSERT(path);
609
610 if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) {
611 /*
612 * We do not even care about the rest of the function
613 * here because we already know Python cannot be fully
614 * initialized.
615 */
616 BT_LIB_LOGE_APPEND_CAUSE(
617 "Python interpreter could not be initialized previously.");
618 status = BT_FUNC_STATUS_ERROR;
619 goto error;
620 } else if (python_state == PYTHON_STATE_WONT_INITIALIZE) {
621 /*
622 * This is not an error: the environment requires that
623 * Python plugins are disabled, so it's simply not
624 * found.
625 */
626 BT_LOGI_STR("Python plugin support was disabled previously "
627 "because the `LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` "
628 "environment variable is set to `1`.");
629 status = BT_FUNC_STATUS_NOT_FOUND;
630 goto error;
631 }
632
633 BT_LOGI("Trying to create all Python plugins from file: path=\"%s\"",
634 path);
635 path_len = strlen(path);
636
637 /* File name ends with `.py` */
638 if (strncmp(path + path_len - PYTHON_PLUGIN_FILE_EXT_LEN,
639 PYTHON_PLUGIN_FILE_EXT,
640 PYTHON_PLUGIN_FILE_EXT_LEN) != 0) {
641 BT_LOGI("Skipping non-Python file: path=\"%s\"", path);
642 status = BT_FUNC_STATUS_NOT_FOUND;
643 goto error;
644 }
645
646 /* File name starts with `bt_plugin_` */
647 basename = g_path_get_basename(path);
648 if (!basename) {
649 BT_LIB_LOGE_APPEND_CAUSE(
650 "Cannot get path's basename: path=\"%s\"", path);
651 status = BT_FUNC_STATUS_ERROR;
652 goto error;
653 }
654
655 if (strncmp(basename, PYTHON_PLUGIN_FILE_PREFIX,
656 PYTHON_PLUGIN_FILE_PREFIX_LEN) != 0) {
657 BT_LOGI("Skipping Python file not starting with `%s`: "
658 "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX, path);
659 status = BT_FUNC_STATUS_NOT_FOUND;
660 goto error;
661 }
662
663 /*
664 * Initialize Python now.
665 *
666 * This is not done in the library contructor because the
667 * interpreter is somewhat slow to initialize. If you don't
668 * have any potential Python plugins, you don't need to endure
669 * this waiting time everytime you load the library.
670 */
671 status = init_python();
672 if (status != BT_FUNC_STATUS_OK) {
673 /* init_python() logs and append errors */
674 goto error;
675 }
676
677 /*
678 * Call bt2.py_plugin._try_load_plugin_module() with this path
679 * to get plugin info if the plugin is loadable and complete.
680 * This function returns None when there's an error, but just in
681 * case we also manually clear the last Python error state.
682 */
683 BT_LOGD_STR("Getting Python plugin info object from Python module.");
684 py_plugin_info = PyObject_CallFunction(py_try_load_plugin_module_func,
685 "(s)", path);
686 if (!py_plugin_info || py_plugin_info == Py_None) {
687 if (fail_on_load_error) {
688 append_python_traceback_error_cause();
689 BT_LIB_LOGW_APPEND_CAUSE(
690 "Cannot load Python plugin: path=\"%s\"", path);
691 status = BT_FUNC_STATUS_ERROR;
692 } else {
693 BT_LIB_LOGW(
694 "Cannot load Python plugin: path=\"%s\"", path);
695 status = BT_FUNC_STATUS_NOT_FOUND;
696 }
697
698 goto error;
699 }
700
701 /*
702 * Get bt_plugin from plugin info object.
703 */
704 plugin = NULL;
705 status = bt_plugin_from_python_plugin_info(py_plugin_info,
706 fail_on_load_error, &plugin);
707 if (status < 0) {
708 /*
709 * bt_plugin_from_python_plugin_info() handles
710 * `fail_on_load_error`, so this is a "real" error.
711 */
712 BT_LIB_LOGW_APPEND_CAUSE(
713 "Cannot create plugin object from Python plugin info object: "
714 "path=\"%s\", py-plugin-info-addr=%p",
715 path, py_plugin_info);
716 BT_ASSERT(!plugin);
717 goto error;
718 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
719 BT_ASSERT(!plugin);
720 goto error;
721 }
722
723 BT_ASSERT(status == BT_FUNC_STATUS_OK);
724 BT_ASSERT(plugin);
725 bt_plugin_set_path(plugin, path);
726 *plugin_set_out = bt_plugin_set_create();
727 if (!*plugin_set_out) {
728 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin set.");
729 status = BT_FUNC_STATUS_MEMORY_ERROR;
730 goto error;
731 }
732
733 bt_plugin_set_add_plugin(*plugin_set_out, plugin);
734 BT_LOGD("Created all Python plugins from file: path=\"%s\", "
735 "plugin-addr=%p, plugin-name=\"%s\"",
736 path, plugin, bt_plugin_get_name(plugin));
737 goto end;
738
739 error:
740 BT_ASSERT(status != BT_FUNC_STATUS_OK);
741 log_python_traceback(BT_LOG_WARNING);
742 pyerr_clear();
743 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out);
744
745 end:
746 bt_plugin_put_ref(plugin);
747 Py_XDECREF(py_plugin_info);
748
749 g_free(basename);
750
751 return status;
752 }
This page took 0.043729 seconds and 3 git commands to generate.