summaryrefslogtreecommitdiff
path: root/src/plugins/python/weechat-python.c
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2022-01-22 21:34:04 +0100
committerSébastien Helleu <flashcode@flashtux.org>2022-01-22 21:34:04 +0100
commit574f1837f9713d6d9e71a94cd5d906b2864f6f6c (patch)
tree78c0c79bf970dad975f6bf5a7827a78b67dabb5f /src/plugins/python/weechat-python.c
parent5172f202177cb8ca6873f32345b6776168c65b9a (diff)
downloadweechat-574f1837f9713d6d9e71a94cd5d906b2864f6f6c.zip
python: fix crash in hook callbacks after script loading failure (closes #1740)
Diffstat (limited to 'src/plugins/python/weechat-python.c')
-rw-r--r--src/plugins/python/weechat-python.c47
1 files changed, 36 insertions, 11 deletions
diff --git a/src/plugins/python/weechat-python.c b/src/plugins/python/weechat-python.c
index d6d501e43..0a4cca493 100644
--- a/src/plugins/python/weechat-python.c
+++ b/src/plugins/python/weechat-python.c
@@ -480,6 +480,7 @@ weechat_python_exec (struct t_plugin_script *script,
old_python_current_script = python_current_script;
python_current_script = script;
old_interpreter = NULL;
+
if (script->interpreter)
{
old_interpreter = PyThreadState_Swap (NULL);
@@ -487,6 +488,12 @@ weechat_python_exec (struct t_plugin_script *script,
}
evMain = PyImport_AddModule ((char *) "__main__");
+ /*
+ * FIXME: sometimes NULL is returned with nested calls of hook callbacks,
+ * to prevent any crash, we just skip execution of the function
+ */
+ if (!evMain)
+ goto end;
evDict = PyModule_GetDict (evMain);
evFunc = PyDict_GetItemString (evDict, function);
@@ -506,26 +513,38 @@ weechat_python_exec (struct t_plugin_script *script,
{
if (i < argc)
{
- argv2[i] = argv[i];
- if (format[i] == 's')
+ switch (format[i])
{
+ case 's': /* string */
+ argv2[i] = argv[i];
#if PY_MAJOR_VERSION >= 3
- if (weechat_utf8_is_valid (argv2[i], -1, NULL))
- format2[i] = 's'; /* Python 3: str */
- else
- format2[i] = 'y'; /* Python 3: bytes */
+ if (weechat_utf8_is_valid (argv2[i], -1, NULL))
+ format2[i] = 's'; /* Python 3: str */
+ else
+ format2[i] = 'y'; /* Python 3: bytes */
#else
- format2[i] = 's'; /* Python 2: str */
+ format2[i] = 's'; /* Python 2: str */
#endif
- }
- else
- {
- format2[i] = format[i];
+ break;
+ case 'i': /* integer */
+ argv2[i] = PyLong_FromLong ((long)(*((int *)argv[i])));
+ format2[i] = 'O';
+ break;
+ case 'h': /* hash */
+ argv2[i] = weechat_python_hashtable_to_dict (
+ (struct t_hashtable *)argv[i]);
+ format2[i] = 'O';
+ break;
+ case 'O': /* object */
+ argv2[i] = argv[i];
+ format2[i] = 'O';
+ break;
}
}
else
{
argv2[i] = NULL;
+ format2[i] = '\0';
}
}
format2[argc] = '\0';
@@ -539,6 +558,12 @@ weechat_python_exec (struct t_plugin_script *script,
argv2[10], argv2[11],
argv2[12], argv2[13],
argv2[14], argv2[15]);
+
+ for (i = 0; i < argc; i++)
+ {
+ if (argv2[i] && (format2[i] == 'O'))
+ Py_XDECREF((PyObject *)argv2[i]);
+ }
}
else
{