1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Reaload python module if previous loaded on /plugin load

This commit is contained in:
James Booth 2016-07-09 23:13:36 +01:00
parent 1926ceea3d
commit 7776c1494a

View File

@ -43,6 +43,7 @@
#include "ui/ui.h" #include "ui/ui.h"
static PyThreadState *thread_state; static PyThreadState *thread_state;
static GHashTable *unloaded_modules;
void void
allow_python_threads() allow_python_threads()
@ -59,6 +60,8 @@ disable_python_threads()
void void
python_env_init(void) python_env_init(void)
{ {
unloaded_modules = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
Py_Initialize(); Py_Initialize();
PyEval_InitThreads(); PyEval_InitThreads();
python_api_init(); python_api_init();
@ -81,7 +84,15 @@ python_plugin_create(const char *const filename)
{ {
disable_python_threads(); disable_python_threads();
gchar *module_name = g_strndup(filename, strlen(filename) - 3); gchar *module_name = g_strndup(filename, strlen(filename) - 3);
PyObject *p_module = PyImport_ImportModule(module_name);
PyObject *p_module = g_hash_table_lookup(unloaded_modules, filename);
if (p_module) {
p_module = PyImport_ReloadModule(p_module);
g_hash_table_remove(unloaded_modules, filename);
} else {
p_module = PyImport_ImportModule(module_name);
}
python_check_error(); python_check_error();
if (p_module) { if (p_module) {
ProfPlugin *plugin = malloc(sizeof(ProfPlugin)); ProfPlugin *plugin = malloc(sizeof(ProfPlugin));
@ -910,8 +921,9 @@ python_plugin_destroy(ProfPlugin *plugin)
{ {
disable_python_threads(); disable_python_threads();
callbacks_remove(plugin->name); callbacks_remove(plugin->name);
g_hash_table_insert(unloaded_modules, strdup(plugin->name), plugin->module);
// Py_XDECREF(plugin->module);
free(plugin->name); free(plugin->name);
Py_XDECREF(plugin->module);
free(plugin); free(plugin);
allow_python_threads(); allow_python_threads();
} }