xed/plugin-loaders/python/pluma-plugin-loader-python.c

720 lines
18 KiB
C
Raw Normal View History

2011-11-07 13:46:58 -06:00
/*
2011-11-07 16:52:18 -06:00
* pluma-plugin-loader-python.c
* This file is part of pluma
2011-11-07 13:46:58 -06:00
*
* Copyright (C) 2008 - Jesse van den Kieboom
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2012-11-18 19:54:49 -06:00
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
2011-11-07 13:46:58 -06:00
*/
2011-11-07 16:52:18 -06:00
#include "pluma-plugin-loader-python.h"
#include "pluma-plugin-python.h"
#include <pluma/pluma-object-module.h>
2011-11-07 13:46:58 -06:00
#define NO_IMPORT_PYGOBJECT
#define NO_IMPORT_PYGTK
#include <Python.h>
#include <pygobject.h>
#include <pygtk/pygtk.h>
#include <signal.h>
#include "config.h"
#if PY_VERSION_HEX < 0x02050000
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
2011-11-07 16:52:18 -06:00
#define PLUMA_PLUGIN_LOADER_PYTHON_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), PLUMA_TYPE_PLUGIN_LOADER_PYTHON, PlumaPluginLoaderPythonPrivate))
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
struct _PlumaPluginLoaderPythonPrivate
2011-11-07 13:46:58 -06:00
{
GHashTable *loaded_plugins;
guint idle_gc;
gboolean init_failed;
};
typedef struct
{
PyObject *type;
PyObject *instance;
gchar *path;
} PythonInfo;
2011-11-07 16:52:18 -06:00
static void pluma_plugin_loader_iface_init (gpointer g_iface, gpointer iface_data);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
/* Exported by pypluma module */
void pypluma_register_classes (PyObject *d);
void pypluma_add_constants (PyObject *module, const gchar *strip_prefix);
extern PyMethodDef pypluma_functions[];
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
/* Exported by pyplumautils module */
void pyplumautils_register_classes (PyObject *d);
extern PyMethodDef pyplumautils_functions[];
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
/* Exported by pyplumacommands module */
void pyplumacommands_register_classes (PyObject *d);
extern PyMethodDef pyplumacommands_functions[];
2011-11-07 13:46:58 -06:00
/* We retreive this to check for correct class hierarchy */
2011-11-07 16:52:18 -06:00
static PyTypeObject *PyPlumaPlugin_Type;
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
PLUMA_PLUGIN_LOADER_REGISTER_TYPE (PlumaPluginLoaderPython, pluma_plugin_loader_python, G_TYPE_OBJECT, pluma_plugin_loader_iface_init);
2011-11-07 13:46:58 -06:00
static PyObject *
2011-11-07 16:52:18 -06:00
find_python_plugin_type (PlumaPluginInfo *info,
2011-11-07 13:46:58 -06:00
PyObject *pymodule)
{
PyObject *locals, *key, *value;
Py_ssize_t pos = 0;
locals = PyModule_GetDict (pymodule);
while (PyDict_Next (locals, &pos, &key, &value))
{
if (!PyType_Check(value))
continue;
2011-11-07 16:52:18 -06:00
if (PyObject_IsSubclass (value, (PyObject*) PyPlumaPlugin_Type))
2011-11-07 13:46:58 -06:00
return value;
}
2011-11-07 16:52:18 -06:00
g_warning ("No PlumaPlugin derivative found in Python plugin '%s'",
pluma_plugin_info_get_name (info));
2011-11-07 13:46:58 -06:00
return NULL;
}
2011-11-07 16:52:18 -06:00
static PlumaPlugin *
new_plugin_from_info (PlumaPluginLoaderPython *loader,
PlumaPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
PythonInfo *pyinfo;
PyTypeObject *pytype;
PyObject *pyobject;
PyGObject *pygobject;
2011-11-07 16:52:18 -06:00
PlumaPlugin *instance;
2011-11-07 13:46:58 -06:00
PyObject *emptyarg;
pyinfo = (PythonInfo *)g_hash_table_lookup (loader->priv->loaded_plugins, info);
if (pyinfo == NULL)
return NULL;
pytype = (PyTypeObject *)pyinfo->type;
if (pytype->tp_new == NULL)
return NULL;
emptyarg = PyTuple_New(0);
pyobject = pytype->tp_new (pytype, emptyarg, NULL);
Py_DECREF (emptyarg);
if (pyobject == NULL)
{
2011-11-07 16:52:18 -06:00
g_error ("Could not create instance for %s.", pluma_plugin_info_get_name (info));
2011-11-07 13:46:58 -06:00
return NULL;
}
pygobject = (PyGObject *)pyobject;
if (pygobject->obj != NULL)
{
Py_DECREF (pyobject);
2011-11-07 16:52:18 -06:00
g_error ("Could not create instance for %s (GObject already initialized).", pluma_plugin_info_get_name (info));
2011-11-07 13:46:58 -06:00
return NULL;
}
pygobject_construct (pygobject,
"install-dir", pyinfo->path,
2011-11-07 16:52:18 -06:00
"data-dir-name", pluma_plugin_info_get_module_name (info),
2011-11-07 13:46:58 -06:00
NULL);
if (pygobject->obj == NULL)
{
2011-11-07 16:52:18 -06:00
g_error ("Could not create instance for %s (GObject not constructed).", pluma_plugin_info_get_name (info));
2011-11-07 13:46:58 -06:00
Py_DECREF (pyobject);
return NULL;
}
/* now call tp_init manually */
if (PyType_IsSubtype (pyobject->ob_type, pytype) &&
pyobject->ob_type->tp_init != NULL)
{
emptyarg = PyTuple_New(0);
pyobject->ob_type->tp_init (pyobject, emptyarg, NULL);
Py_DECREF (emptyarg);
}
2011-11-07 16:52:18 -06:00
instance = PLUMA_PLUGIN (pygobject->obj);
2011-11-07 13:46:58 -06:00
pyinfo->instance = (PyObject *)pygobject;
2011-11-07 16:52:18 -06:00
/* make sure to register the python instance for the PlumaPluginPython
object to it can wrap the virtual pluma plugin funcs back to python */
_pluma_plugin_python_set_instance (PLUMA_PLUGIN_PYTHON (instance), (PyObject *)pygobject);
2011-11-07 13:46:58 -06:00
/* we return a reference here because the other is owned by python */
2011-11-07 16:52:18 -06:00
return PLUMA_PLUGIN (g_object_ref (instance));
2011-11-07 13:46:58 -06:00
}
2011-11-07 16:52:18 -06:00
static PlumaPlugin *
add_python_info (PlumaPluginLoaderPython *loader,
PlumaPluginInfo *info,
2011-11-07 13:46:58 -06:00
PyObject *module,
const gchar *path,
PyObject *type)
{
PythonInfo *pyinfo;
pyinfo = g_new (PythonInfo, 1);
pyinfo->path = g_strdup (path);
pyinfo->type = type;
Py_INCREF (pyinfo->type);
g_hash_table_insert (loader->priv->loaded_plugins, info, pyinfo);
return new_plugin_from_info (loader, info);
}
static const gchar *
2011-11-07 16:52:18 -06:00
pluma_plugin_loader_iface_get_id (void)
2011-11-07 13:46:58 -06:00
{
return "Python";
}
2011-11-07 16:52:18 -06:00
static PlumaPlugin *
pluma_plugin_loader_iface_load (PlumaPluginLoader *loader,
PlumaPluginInfo *info,
2011-11-07 13:46:58 -06:00
const gchar *path)
{
2011-11-07 16:52:18 -06:00
PlumaPluginLoaderPython *pyloader = PLUMA_PLUGIN_LOADER_PYTHON (loader);
2011-11-07 13:46:58 -06:00
PyObject *main_module, *main_locals, *pytype;
PyObject *pymodule, *fromlist;
gchar *module_name;
2011-11-07 16:52:18 -06:00
PlumaPlugin *result;
2011-11-07 13:46:58 -06:00
if (pyloader->priv->init_failed)
{
2011-11-07 16:52:18 -06:00
g_warning ("Cannot load python plugin Python '%s' since pluma was"
2011-11-07 13:46:58 -06:00
"not able to initialize the Python interpreter.",
2011-11-07 16:52:18 -06:00
pluma_plugin_info_get_name (info));
2011-11-07 13:46:58 -06:00
return NULL;
}
/* see if py definition for the plugin is already loaded */
result = new_plugin_from_info (pyloader, info);
if (result != NULL)
return result;
2011-11-07 16:52:18 -06:00
main_module = PyImport_AddModule ("pluma.plugins");
2011-11-07 13:46:58 -06:00
if (main_module == NULL)
{
2011-11-07 16:52:18 -06:00
g_warning ("Could not get pluma.plugins.");
2011-11-07 13:46:58 -06:00
return NULL;
}
/* If we have a special path, we register it */
if (path != NULL)
{
PyObject *sys_path = PySys_GetObject ("path");
PyObject *pypath = PyString_FromString (path);
if (PySequence_Contains (sys_path, pypath) == 0)
PyList_Insert (sys_path, 0, pypath);
Py_DECREF (pypath);
}
main_locals = PyModule_GetDict (main_module);
/* we need a fromlist to be able to import modules with a '.' in the
name. */
fromlist = PyTuple_New(0);
2011-11-07 16:52:18 -06:00
module_name = g_strdup (pluma_plugin_info_get_module_name (info));
2011-11-07 13:46:58 -06:00
pymodule = PyImport_ImportModuleEx (module_name,
main_locals,
main_locals,
fromlist);
Py_DECREF(fromlist);
if (!pymodule)
{
g_free (module_name);
PyErr_Print ();
return NULL;
}
PyDict_SetItemString (main_locals, module_name, pymodule);
g_free (module_name);
pytype = find_python_plugin_type (info, pymodule);
if (pytype)
return add_python_info (pyloader, info, pymodule, path, pytype);
return NULL;
}
static void
2011-11-07 16:52:18 -06:00
pluma_plugin_loader_iface_unload (PlumaPluginLoader *loader,
PlumaPluginInfo *info)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
PlumaPluginLoaderPython *pyloader = PLUMA_PLUGIN_LOADER_PYTHON (loader);
2011-11-07 13:46:58 -06:00
PythonInfo *pyinfo;
PyGILState_STATE state;
pyinfo = (PythonInfo *)g_hash_table_lookup (pyloader->priv->loaded_plugins, info);
if (!pyinfo)
return;
state = pyg_gil_state_ensure ();
Py_XDECREF (pyinfo->instance);
pyg_gil_state_release (state);
pyinfo->instance = NULL;
}
static gboolean
2011-11-07 16:52:18 -06:00
run_gc (PlumaPluginLoaderPython *loader)
2011-11-07 13:46:58 -06:00
{
while (PyGC_Collect ())
;
loader->priv->idle_gc = 0;
return FALSE;
}
static void
2011-11-07 16:52:18 -06:00
pluma_plugin_loader_iface_garbage_collect (PlumaPluginLoader *loader)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
PlumaPluginLoaderPython *pyloader;
2011-11-07 13:46:58 -06:00
if (!Py_IsInitialized())
return;
2011-11-07 16:52:18 -06:00
pyloader = PLUMA_PLUGIN_LOADER_PYTHON (loader);
2011-11-07 13:46:58 -06:00
/*
* We both run the GC right now and we schedule
* a further collection in the main loop.
*/
while (PyGC_Collect ())
;
if (pyloader->priv->idle_gc == 0)
pyloader->priv->idle_gc = g_idle_add ((GSourceFunc)run_gc, pyloader);
}
static void
2011-11-07 16:52:18 -06:00
pluma_plugin_loader_iface_init (gpointer g_iface,
2011-11-07 13:46:58 -06:00
gpointer iface_data)
{
2011-11-07 16:52:18 -06:00
PlumaPluginLoaderInterface *iface = (PlumaPluginLoaderInterface *)g_iface;
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
iface->get_id = pluma_plugin_loader_iface_get_id;
iface->load = pluma_plugin_loader_iface_load;
iface->unload = pluma_plugin_loader_iface_unload;
iface->garbage_collect = pluma_plugin_loader_iface_garbage_collect;
2011-11-07 13:46:58 -06:00
}
static void
2011-11-07 16:52:18 -06:00
pluma_python_shutdown (PlumaPluginLoaderPython *loader)
2011-11-07 13:46:58 -06:00
{
if (!Py_IsInitialized ())
return;
if (loader->priv->idle_gc != 0)
{
g_source_remove (loader->priv->idle_gc);
loader->priv->idle_gc = 0;
}
while (PyGC_Collect ())
;
Py_Finalize ();
}
/* C equivalent of
* import pygtk
* pygtk.require ("2.0")
*/
static gboolean
2011-11-07 16:52:18 -06:00
pluma_check_pygtk2 (void)
2011-11-07 13:46:58 -06:00
{
PyObject *pygtk, *mdict, *require;
/* pygtk.require("2.0") */
pygtk = PyImport_ImportModule ("pygtk");
if (pygtk == NULL)
{
g_warning ("Error initializing Python interpreter: could not import pygtk.");
return FALSE;
}
mdict = PyModule_GetDict (pygtk);
require = PyDict_GetItemString (mdict, "require");
PyObject_CallObject (require,
Py_BuildValue ("(S)", PyString_FromString ("2.0")));
if (PyErr_Occurred())
{
g_warning ("Error initializing Python interpreter: pygtk 2 is required.");
return FALSE;
}
return TRUE;
}
/* Note: the following two functions are needed because
* init_pyobject and init_pygtk which are *macros* which in case
* case of error set the PyErr and then make the calling
* function return behind our back.
* It's up to the caller to check the result with PyErr_Occurred()
*/
static void
2011-11-07 16:52:18 -06:00
pluma_init_pygobject (void)
2011-11-07 13:46:58 -06:00
{
init_pygobject_check (2, 11, 5); /* FIXME: get from config */
}
static void
2011-11-07 16:52:18 -06:00
pluma_init_pygtk (void)
2011-11-07 13:46:58 -06:00
{
PyObject *gtk, *mdict, *version, *required_version;
init_pygtk ();
/* there isn't init_pygtk_check(), do the version
* check ourselves */
gtk = PyImport_ImportModule("gtk");
mdict = PyModule_GetDict(gtk);
version = PyDict_GetItemString (mdict, "pygtk_version");
if (!version)
{
PyErr_SetString (PyExc_ImportError,
"PyGObject version too old");
return;
}
required_version = Py_BuildValue ("(iii)", 2, 4, 0); /* FIXME */
if (PyObject_Compare (version, required_version) == -1)
{
PyErr_SetString (PyExc_ImportError,
"PyGObject version too old");
Py_DECREF (required_version);
return;
}
Py_DECREF (required_version);
}
static void
old_gtksourceview_init (void)
{
PyErr_SetString(PyExc_ImportError,
"gtksourceview module not allowed, use gtksourceview2");
}
static void
2011-11-07 16:52:18 -06:00
pluma_init_pygtksourceview (void)
2011-11-07 13:46:58 -06:00
{
PyObject *gtksourceview, *mdict, *version, *required_version;
gtksourceview = PyImport_ImportModule("gtksourceview2");
if (gtksourceview == NULL)
{
PyErr_SetString (PyExc_ImportError,
"could not import gtksourceview");
return;
}
mdict = PyModule_GetDict (gtksourceview);
version = PyDict_GetItemString (mdict, "pygtksourceview2_version");
if (!version)
{
PyErr_SetString (PyExc_ImportError,
"PyGtkSourceView version too old");
return;
}
required_version = Py_BuildValue ("(iii)", 0, 8, 0); /* FIXME */
if (PyObject_Compare (version, required_version) == -1)
{
PyErr_SetString (PyExc_ImportError,
"PyGtkSourceView version too old");
Py_DECREF (required_version);
return;
}
Py_DECREF (required_version);
/* Create a dummy 'gtksourceview' module to prevent
* loading of the old 'gtksourceview' modules that
* has conflicting symbols with the gtksourceview2 module.
* Raise an exception when trying to import it.
*/
PyImport_AppendInittab ("gtksourceview", old_gtksourceview_init);
}
static gboolean
2011-11-07 16:52:18 -06:00
pluma_python_init (PlumaPluginLoaderPython *loader)
2011-11-07 13:46:58 -06:00
{
PyObject *mdict, *tuple;
2011-11-07 16:52:18 -06:00
PyObject *pluma, *plumautils, *plumacommands, *plumaplugins;
2011-11-07 13:46:58 -06:00
PyObject *gettext, *install, *gettext_args;
2011-11-07 16:52:18 -06:00
//char *argv[] = { "pluma", NULL };
char *argv[] = { PLUMA_PLUGINS_LIBS_DIR, NULL };
2011-11-07 13:46:58 -06:00
#ifdef HAVE_SIGACTION
gint res;
struct sigaction old_sigint;
#endif
if (loader->priv->init_failed)
{
/* We already failed to initialized Python, don't need to
* retry again */
return FALSE;
}
if (Py_IsInitialized ())
{
/* Python has already been successfully initialized */
return TRUE;
}
/* We are trying to initialize Python for the first time,
set init_failed to FALSE only if the entire initialization process
ends with success */
loader->priv->init_failed = TRUE;
/* Hack to make python not overwrite SIGINT: this is needed to avoid
* the crash reported on bug #326191 */
/* CHECK: can't we use Py_InitializeEx instead of Py_Initialize in order
to avoid to manage signal handlers ? - Paolo (Dec. 31, 2006) */
#ifdef HAVE_SIGACTION
/* Save old handler */
res = sigaction (SIGINT, NULL, &old_sigint);
if (res != 0)
{
g_warning ("Error initializing Python interpreter: cannot get "
"handler to SIGINT signal (%s)",
g_strerror (errno));
return FALSE;
}
#endif
/* Python initialization */
Py_Initialize ();
#ifdef HAVE_SIGACTION
/* Restore old handler */
res = sigaction (SIGINT, &old_sigint, NULL);
if (res != 0)
{
g_warning ("Error initializing Python interpreter: cannot restore "
"handler to SIGINT signal (%s).",
g_strerror (errno));
goto python_init_error;
}
#endif
PySys_SetArgv (1, argv);
2011-11-07 16:52:18 -06:00
if (!pluma_check_pygtk2 ())
2011-11-07 13:46:58 -06:00
{
/* Warning message already printed in check_pygtk2 */
goto python_init_error;
}
/* import gobject */
2011-11-07 16:52:18 -06:00
pluma_init_pygobject ();
2011-11-07 13:46:58 -06:00
if (PyErr_Occurred ())
{
g_warning ("Error initializing Python interpreter: could not import pygobject.");
goto python_init_error;
}
/* import gtk */
2011-11-07 16:52:18 -06:00
pluma_init_pygtk ();
2011-11-07 13:46:58 -06:00
if (PyErr_Occurred ())
{
g_warning ("Error initializing Python interpreter: could not import pygtk.");
goto python_init_error;
}
/* import gtksourceview */
2011-11-07 16:52:18 -06:00
pluma_init_pygtksourceview ();
2011-11-07 13:46:58 -06:00
if (PyErr_Occurred ())
{
PyErr_Print ();
g_warning ("Error initializing Python interpreter: could not import pygtksourceview.");
goto python_init_error;
}
2011-11-07 16:52:18 -06:00
/* import pluma */
pluma = Py_InitModule ("pluma", pypluma_functions);
mdict = PyModule_GetDict (pluma);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
pypluma_register_classes (mdict);
pypluma_add_constants (pluma, "PLUMA_");
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
/* pluma version */
2011-11-07 13:46:58 -06:00
tuple = Py_BuildValue("(iii)",
2011-11-07 16:52:18 -06:00
PLUMA_MAJOR_VERSION,
PLUMA_MINOR_VERSION,
PLUMA_MICRO_VERSION);
2011-11-07 13:46:58 -06:00
PyDict_SetItemString(mdict, "version", tuple);
Py_DECREF(tuple);
2011-11-07 16:52:18 -06:00
/* Retrieve the Python type for pluma.Plugin */
PyPlumaPlugin_Type = (PyTypeObject *) PyDict_GetItemString (mdict, "Plugin");
if (PyPlumaPlugin_Type == NULL)
2011-11-07 13:46:58 -06:00
{
PyErr_Print ();
goto python_init_error;
}
2011-11-07 16:52:18 -06:00
/* import pluma.utils */
plumautils = Py_InitModule ("pluma.utils", pyplumautils_functions);
PyDict_SetItemString (mdict, "utils", plumautils);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
/* import pluma.commands */
plumacommands = Py_InitModule ("pluma.commands", pyplumacommands_functions);
PyDict_SetItemString (mdict, "commands", plumacommands);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
/* initialize empty pluma.plugins module */
plumaplugins = Py_InitModule ("pluma.plugins", NULL);
PyDict_SetItemString (mdict, "plugins", plumaplugins);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
mdict = PyModule_GetDict (plumautils);
pyplumautils_register_classes (mdict);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
mdict = PyModule_GetDict (plumacommands);
pyplumacommands_register_classes (mdict);
2011-11-07 13:46:58 -06:00
/* i18n support */
gettext = PyImport_ImportModule ("gettext");
if (gettext == NULL)
{
g_warning ("Error initializing Python interpreter: could not import gettext.");
goto python_init_error;
}
mdict = PyModule_GetDict (gettext);
install = PyDict_GetItemString (mdict, "install");
2011-11-07 16:52:18 -06:00
gettext_args = Py_BuildValue ("ss", GETTEXT_PACKAGE, PLUMA_LOCALEDIR);
2011-11-07 13:46:58 -06:00
PyObject_CallObject (install, gettext_args);
Py_DECREF (gettext_args);
/* Python has been successfully initialized */
loader->priv->init_failed = FALSE;
return TRUE;
python_init_error:
g_warning ("Please check the installation of all the Python related packages required "
2011-11-07 16:52:18 -06:00
"by pluma and try again.");
2011-11-07 13:46:58 -06:00
PyErr_Clear ();
2011-11-07 16:52:18 -06:00
pluma_python_shutdown (loader);
2011-11-07 13:46:58 -06:00
return FALSE;
}
static void
2011-11-07 16:52:18 -06:00
pluma_plugin_loader_python_finalize (GObject *object)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
PlumaPluginLoaderPython *pyloader = PLUMA_PLUGIN_LOADER_PYTHON (object);
2011-11-07 13:46:58 -06:00
g_hash_table_destroy (pyloader->priv->loaded_plugins);
2011-11-07 16:52:18 -06:00
pluma_python_shutdown (pyloader);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
G_OBJECT_CLASS (pluma_plugin_loader_python_parent_class)->finalize (object);
2011-11-07 13:46:58 -06:00
}
static void
2011-11-07 16:52:18 -06:00
pluma_plugin_loader_python_class_init (PlumaPluginLoaderPythonClass *klass)
2011-11-07 13:46:58 -06:00
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2011-11-07 16:52:18 -06:00
object_class->finalize = pluma_plugin_loader_python_finalize;
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
g_type_class_add_private (object_class, sizeof (PlumaPluginLoaderPythonPrivate));
2011-11-07 13:46:58 -06:00
}
static void
2011-11-07 16:52:18 -06:00
pluma_plugin_loader_python_class_finalize (PlumaPluginLoaderPythonClass *klass)
2011-11-07 13:46:58 -06:00
{
}
static void
destroy_python_info (PythonInfo *info)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
Py_XDECREF (info->type);
pyg_gil_state_release (state);
g_free (info->path);
g_free (info);
}
static void
2011-11-07 16:52:18 -06:00
pluma_plugin_loader_python_init (PlumaPluginLoaderPython *self)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
self->priv = PLUMA_PLUGIN_LOADER_PYTHON_GET_PRIVATE (self);
2011-11-07 13:46:58 -06:00
/* initialize python interpreter */
2011-11-07 16:52:18 -06:00
pluma_python_init (self);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
/* loaded_plugins maps PlumaPluginInfo to a PythonInfo */
2011-11-07 13:46:58 -06:00
self->priv->loaded_plugins = g_hash_table_new_full (g_direct_hash,
g_direct_equal,
NULL,
(GDestroyNotify)destroy_python_info);
}
2011-11-07 16:52:18 -06:00
PlumaPluginLoaderPython *
pluma_plugin_loader_python_new ()
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
GObject *loader = g_object_new (PLUMA_TYPE_PLUGIN_LOADER_PYTHON, NULL);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
return PLUMA_PLUGIN_LOADER_PYTHON (loader);
2011-11-07 13:46:58 -06:00
}