Python bt2: remove type restriction on structure __setitem__
[babeltrace.git] / 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 "PLUGIN-PY"
28 #include "logging.h"
29
30 #include <babeltrace/babeltrace-internal.h>
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/plugin/plugin.h>
34 #include <babeltrace/plugin/plugin-internal.h>
35 #include <babeltrace/graph/component-class.h>
36 #include <babeltrace/graph/component-class-internal.h>
37 #include <stdlib.h>
38 #include <signal.h>
39 #include <Python.h>
40 #include <glib.h>
41 #include <gmodule.h>
42
43 #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_"
44 #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1)
45 #define PYTHON_PLUGIN_FILE_EXT ".py"
46 #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1)
47
48 enum python_state {
49 /* init_python() not called yet */
50 PYTHON_STATE_NOT_INITED,
51
52 /* init_python() called once with success */
53 PYTHON_STATE_FULLY_INITIALIZED,
54
55 /* init_python() called once without success */
56 PYTHON_STATE_CANNOT_INITIALIZE,
57 } python_state = PYTHON_STATE_NOT_INITED;
58
59 static PyObject *py_try_load_plugin_module_func = NULL;
60 static bool python_was_initialized_by_us;
61
62 static
63 void print_python_traceback_warn(void)
64 {
65 if (BT_LOG_ON_WARN && Py_IsInitialized() && PyErr_Occurred()) {
66 BT_LOGW_STR("Exception occured: traceback: ");
67 PyErr_Print();
68 }
69 }
70
71 static
72 void pyerr_clear(void)
73 {
74 if (Py_IsInitialized()) {
75 PyErr_Clear();
76 }
77 }
78
79 static
80 void init_python(void)
81 {
82 PyObject *py_bt2_py_plugin_mod = NULL;
83 const char *dis_python_env;
84 sighandler_t old_sigint = signal(SIGINT, SIG_DFL);
85
86 if (python_state != PYTHON_STATE_NOT_INITED) {
87 goto end;
88 }
89
90 /*
91 * User can disable Python plugin support with the
92 * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to
93 * 1.
94 */
95 dis_python_env = getenv("BABELTRACE_DISABLE_PYTHON_PLUGINS");
96 if (dis_python_env && strcmp(dis_python_env, "1") == 0) {
97 BT_LOGI_STR("Python plugin support is disabled because `BABELTRACE_DISABLE_PYTHON_PLUGINS=1`.");
98 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
99 goto end;
100 }
101
102 if (!Py_IsInitialized()) {
103 BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
104 Py_InitializeEx(0);
105 python_was_initialized_by_us = true;
106 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
107 Py_GetVersion());
108 } else {
109 BT_LOGI("Python interpreter is already initialized: version=\"%s\"",
110 Py_GetVersion());
111 }
112
113 py_bt2_py_plugin_mod = PyImport_ImportModule("bt2.py_plugin");
114 if (!py_bt2_py_plugin_mod) {
115 BT_LOGI_STR("Cannot import bt2.py_plugin Python module: Python plugin support is disabled.");
116 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
117 goto end;
118 }
119
120 py_try_load_plugin_module_func =
121 PyObject_GetAttrString(py_bt2_py_plugin_mod, "_try_load_plugin_module");
122 if (!py_try_load_plugin_module_func) {
123 BT_LOGI_STR("Cannot get _try_load_plugin_module attribute from bt2.py_plugin Python module: Python plugin support is disabled.");
124 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
125 goto end;
126 }
127
128 python_state = PYTHON_STATE_FULLY_INITIALIZED;
129
130 end:
131 if (old_sigint != SIG_ERR) {
132 (void) signal(SIGINT, old_sigint);
133 }
134
135 print_python_traceback_warn();
136 pyerr_clear();
137 Py_XDECREF(py_bt2_py_plugin_mod);
138 return;
139 }
140
141 __attribute__((destructor)) static
142 void fini_python(void) {
143 if (Py_IsInitialized() && python_was_initialized_by_us) {
144 if (py_try_load_plugin_module_func) {
145 Py_DECREF(py_try_load_plugin_module_func);
146 py_try_load_plugin_module_func = NULL;
147 }
148
149 Py_Finalize();
150 BT_LOGI_STR("Finalized Python interpreter.");
151 }
152
153 python_state = PYTHON_STATE_NOT_INITED;
154 }
155
156 static
157 struct bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
158 {
159 struct bt_plugin *plugin = NULL;
160 PyObject *py_name = NULL;
161 PyObject *py_author = NULL;
162 PyObject *py_description = NULL;
163 PyObject *py_license = NULL;
164 PyObject *py_version = NULL;
165 PyObject *py_comp_class_addrs = NULL;
166 const char *name = NULL;
167 const char *author = NULL;
168 const char *description = NULL;
169 const char *license = NULL;
170 unsigned int major = 0, minor = 0, patch = 0;
171 const char *version_extra = NULL;
172 int ret;
173
174 assert(plugin_info);
175 assert(python_state == PYTHON_STATE_FULLY_INITIALIZED);
176 py_name = PyObject_GetAttrString(plugin_info, "name");
177 if (!py_name) {
178 BT_LOGW("Cannot find `name` attribute in Python plugin info object: "
179 "py-plugin-info-addr=%p", plugin_info);
180 goto error;
181 }
182
183 py_author = PyObject_GetAttrString(plugin_info, "author");
184 if (!py_author) {
185 BT_LOGW("Cannot find `author` attribute in Python plugin info object: "
186 "py-plugin-info-addr=%p", plugin_info);
187 goto error;
188 }
189
190 py_description = PyObject_GetAttrString(plugin_info, "description");
191 if (!py_description) {
192 BT_LOGW("Cannot find `desciption` attribute in Python plugin info object: "
193 "py-plugin-info-addr=%p", plugin_info);
194 goto error;
195 }
196
197 py_license = PyObject_GetAttrString(plugin_info, "license");
198 if (!py_license) {
199 BT_LOGW("Cannot find `license` attribute in Python plugin info object: "
200 "py-plugin-info-addr=%p", plugin_info);
201 goto error;
202 }
203
204 py_version = PyObject_GetAttrString(plugin_info, "version");
205 if (!py_version) {
206 BT_LOGW("Cannot find `version` attribute in Python plugin info object: "
207 "py-plugin-info-addr=%p", plugin_info);
208 goto error;
209 }
210
211 py_comp_class_addrs = PyObject_GetAttrString(plugin_info,
212 "comp_class_addrs");
213 if (!py_comp_class_addrs) {
214 BT_LOGW("Cannot find `comp_class_addrs` attribute in Python plugin info object: "
215 "py-plugin-info-addr=%p", plugin_info);
216 goto error;
217 }
218
219 if (PyUnicode_Check(py_name)) {
220 name = PyUnicode_AsUTF8(py_name);
221 if (!name) {
222 BT_LOGW("Cannot decode Python plugin name string: "
223 "py-plugin-info-addr=%p", plugin_info);
224 goto error;
225 }
226 } else {
227 /* Plugin name is mandatory */
228 BT_LOGW("Plugin name is not a string: "
229 "py-plugin-info-addr=%p", plugin_info);
230 goto error;
231 }
232
233 if (PyUnicode_Check(py_author)) {
234 author = PyUnicode_AsUTF8(py_author);
235 if (!author) {
236 BT_LOGW("Cannot decode Python plugin author string: "
237 "py-plugin-info-addr=%p", plugin_info);
238 goto error;
239 }
240 }
241
242 if (PyUnicode_Check(py_description)) {
243 description = PyUnicode_AsUTF8(py_description);
244 if (!description) {
245 BT_LOGW("Cannot decode Python plugin description string: "
246 "py-plugin-info-addr=%p", plugin_info);
247 goto error;
248 }
249 }
250
251 if (PyUnicode_Check(py_license)) {
252 license = PyUnicode_AsUTF8(py_license);
253 if (!license) {
254 BT_LOGW("Cannot decode Python plugin license string: "
255 "py-plugin-info-addr=%p", plugin_info);
256 goto error;
257 }
258 }
259
260 if (PyTuple_Check(py_version)) {
261 if (PyTuple_Size(py_version) >= 3) {
262 PyObject *py_major = PyTuple_GetItem(py_version, 0);
263 PyObject *py_minor = PyTuple_GetItem(py_version, 1);
264 PyObject *py_patch = PyTuple_GetItem(py_version, 2);
265
266 assert(py_major);
267 assert(py_minor);
268 assert(py_patch);
269
270 if (PyLong_Check(py_major)) {
271 major = PyLong_AsUnsignedLong(py_major);
272 }
273
274 if (PyLong_Check(py_minor)) {
275 minor = PyLong_AsUnsignedLong(py_minor);
276 }
277
278 if (PyLong_Check(py_patch)) {
279 patch = PyLong_AsUnsignedLong(py_patch);
280 }
281
282 if (PyErr_Occurred()) {
283 /* Overflow error, most probably */
284 BT_LOGW("Invalid Python plugin version format: "
285 "py-plugin-info-addr=%p", plugin_info);
286 goto error;
287 }
288 }
289
290 if (PyTuple_Size(py_version) >= 4) {
291 PyObject *py_extra = PyTuple_GetItem(py_version, 3);
292
293 assert(py_extra);
294
295 if (PyUnicode_Check(py_extra)) {
296 version_extra = PyUnicode_AsUTF8(py_extra);
297 if (!version_extra) {
298 BT_LOGW("Cannot decode Python plugin version's extra string: "
299 "py-plugin-info-addr=%p", plugin_info);
300 goto error;
301 }
302 }
303 }
304 }
305
306 plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON);
307 if (!plugin) {
308 BT_LOGE_STR("Cannot create empty plugin object.");
309 goto error;
310 }
311
312 bt_plugin_set_name(plugin, name);
313
314 if (description) {
315 bt_plugin_set_description(plugin, description);
316 }
317
318 if (author) {
319 bt_plugin_set_author(plugin, author);
320 }
321
322 if (license) {
323 bt_plugin_set_license(plugin, license);
324 }
325
326 bt_plugin_set_version(plugin, major, minor, patch, version_extra);
327
328 if (PyList_Check(py_comp_class_addrs)) {
329 size_t i;
330
331 for (i = 0; i < PyList_Size(py_comp_class_addrs); i++) {
332 struct bt_component_class *comp_class;
333 PyObject *py_comp_class_addr;
334
335 py_comp_class_addr =
336 PyList_GetItem(py_comp_class_addrs, i);
337 assert(py_comp_class_addr);
338 if (PyLong_Check(py_comp_class_addr)) {
339 comp_class = (struct bt_component_class *)
340 PyLong_AsUnsignedLongLong(py_comp_class_addr);
341 } else {
342 BT_LOGW("Component class address is not an integer in Python plugin info object: "
343 "py-plugin-info-addr=%p, index=%zu",
344 plugin_info, i);
345 continue;
346 }
347
348 ret = bt_plugin_add_component_class(plugin, comp_class);
349 if (ret < 0) {
350 BT_LOGE("Cannot add component class to plugin: "
351 "py-plugin-info-addr=%p, "
352 "plugin-addr=%p, plugin-name=\"%s\", "
353 "comp-class-addr=%p, "
354 "comp-class-name=\"%s\", "
355 "comp-class-type=%s",
356 plugin_info,
357 plugin, bt_plugin_get_name(plugin),
358 comp_class,
359 bt_component_class_get_name(comp_class),
360 bt_component_class_type_string(
361 bt_component_class_get_type(comp_class)));
362 continue;
363 }
364 }
365 }
366
367 bt_plugin_freeze(plugin);
368
369 goto end;
370
371 error:
372 print_python_traceback_warn();
373 pyerr_clear();
374 BT_PUT(plugin);
375
376 end:
377 Py_XDECREF(py_name);
378 Py_XDECREF(py_author);
379 Py_XDECREF(py_description);
380 Py_XDECREF(py_license);
381 Py_XDECREF(py_version);
382 Py_XDECREF(py_comp_class_addrs);
383 return plugin;
384 }
385
386 G_MODULE_EXPORT
387 struct bt_plugin_set *bt_plugin_python_create_all_from_file(const char *path)
388 {
389 struct bt_plugin_set *plugin_set = NULL;
390 struct bt_plugin *plugin = NULL;
391 PyObject *py_plugin_info = NULL;
392 gchar *basename = NULL;
393 size_t path_len;
394
395 assert(path);
396
397 if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) {
398 /*
399 * We do not even care about the rest of the function
400 * here because we already know Python cannot be fully
401 * initialized.
402 */
403 goto error;
404 }
405
406 BT_LOGD("Creating all Python plugins from file: path=\"%s\"", path);
407 path_len = strlen(path);
408
409 /* File name ends with `.py` */
410 if (strncmp(path + path_len - PYTHON_PLUGIN_FILE_EXT_LEN,
411 PYTHON_PLUGIN_FILE_EXT,
412 PYTHON_PLUGIN_FILE_EXT_LEN) != 0) {
413 BT_LOGD("Skipping non-Python file: path=\"%s\"", path);
414 goto error;
415 }
416
417 /* File name starts with `bt_plugin_` */
418 basename = g_path_get_basename(path);
419 if (!basename) {
420 BT_LOGW("Cannot get path's basename: path=\"%s\"", path);
421 goto error;
422 }
423
424 if (strncmp(basename, PYTHON_PLUGIN_FILE_PREFIX,
425 PYTHON_PLUGIN_FILE_PREFIX_LEN) != 0) {
426 BT_LOGD("Skipping Python file not starting with `%s`: "
427 "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX, path);
428 goto error;
429 }
430
431 /*
432 * Initialize Python now.
433 *
434 * This is not done in the library contructor because the
435 * interpreter is somewhat slow to initialize. If you don't
436 * have any potential Python plugins, you don't need to endure
437 * this waiting time everytime you load the library.
438 */
439 init_python();
440 if (python_state != PYTHON_STATE_FULLY_INITIALIZED) {
441 /*
442 * For some reason we cannot initialize Python,
443 * import the required modules, and get the required
444 * attributes from them.
445 */
446 BT_LOGI("Failed to initialize Python interpreter.");
447 goto error;
448 }
449
450 /*
451 * Call bt2.py_plugin._try_load_plugin_module() with this path
452 * to get plugin info if the plugin is loadable and complete.
453 * This function returns None when there's an error, but just in
454 * case we also manually clear the last Python error state.
455 */
456 BT_LOGD_STR("Getting Python plugin info object from Python module.");
457 py_plugin_info = PyObject_CallFunction(py_try_load_plugin_module_func,
458 "(s)", path);
459 if (!py_plugin_info || py_plugin_info == Py_None) {
460 BT_LOGW("Cannot load Python plugin: path=\"%s\"", path);
461 print_python_traceback_warn();
462 PyErr_Clear();
463 goto error;
464 }
465
466 /*
467 * Get bt_plugin from plugin info object.
468 */
469 plugin = bt_plugin_from_python_plugin_info(py_plugin_info);
470 if (!plugin) {
471 BT_LOGW("Cannot create plugin object from Python plugin info object: "
472 "path=\"%s\", py-plugin-info-addr=%p",
473 path, py_plugin_info);
474 goto error;
475 }
476
477 bt_plugin_set_path(plugin, path);
478 plugin_set = bt_plugin_set_create();
479 if (!plugin_set) {
480 BT_LOGE_STR("Cannot create empty plugin set.");
481 goto error;
482 }
483
484 bt_plugin_set_add_plugin(plugin_set, plugin);
485 BT_LOGD("Created all Python plugins from file: path=\"%s\", "
486 "plugin-addr=%p, plugin-name=\"%s\"",
487 path, plugin, bt_plugin_get_name(plugin));
488 goto end;
489
490 error:
491 BT_PUT(plugin_set);
492
493 end:
494 bt_put(plugin);
495 Py_XDECREF(py_plugin_info);
496 g_free(basename);
497 return plugin_set;
498 }
This page took 0.049125 seconds and 4 git commands to generate.