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