[libpeas/proxys: 10/25] Implement PeasExtensionPython
- From: Steve Frécinaux <sfre src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libpeas/proxys: 10/25] Implement PeasExtensionPython
- Date: Sun, 23 May 2010 14:00:07 +0000 (UTC)
commit d3cd5190a83ac2b7b6a3c185424df90414049aff
Author: Steve Frécinaux <code istique net>
Date: Tue May 18 08:18:03 2010 +0200
Implement PeasExtensionPython
loaders/python/Makefile.am | 2 +
loaders/python/peas-extension-python.c | 99 ++++++++++++++++++++++
loaders/python/peas-extension-python.h | 59 +++++++++++++
loaders/python/peas-plugin-loader-python.c | 125 ++++++++++++++--------------
4 files changed, 222 insertions(+), 63 deletions(-)
---
diff --git a/loaders/python/Makefile.am b/loaders/python/Makefile.am
index a8a7077..ae8e187 100644
--- a/loaders/python/Makefile.am
+++ b/loaders/python/Makefile.am
@@ -17,6 +17,8 @@ INCLUDES = \
loader_LTLIBRARIES = libpythonloader.la
libpythonloader_la_SOURCES = \
+ peas-extension-python.c \
+ peas-extension-python.h \
peas-plugin-loader-python.c \
peas-plugin-loader-python.h
diff --git a/loaders/python/peas-extension-python.c b/loaders/python/peas-extension-python.c
new file mode 100644
index 0000000..af39098
--- /dev/null
+++ b/loaders/python/peas-extension-python.c
@@ -0,0 +1,99 @@
+/*
+ * peas-extension-python.c
+ * This file is part of libpeas
+ *
+ * Copyright (C) 2010 - Steve Frécinaux
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library 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 Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include <girepository.h>
+/* _POSIX_C_SOURCE is defined in Python.h and in limits.h included by
+ * girepository.h, so we unset it here to avoid a warning. Yep, that's bad. */
+#undef _POSIX_C_SOURCE
+#include <Python.h>
+#include <pygobject.h>
+#include <libpeas/peas-introspection.h>
+#include "peas-extension-python.h"
+
+G_DEFINE_DYNAMIC_TYPE (PeasExtensionPython, peas_extension_python, PEAS_TYPE_EXTENSION);
+
+void
+peas_extension_python_register (GTypeModule *module)
+{
+ peas_extension_python_register_type (module);
+}
+
+static void
+peas_extension_python_init (PeasExtensionPython *pyexten)
+{
+}
+
+static gboolean
+peas_extension_python_call (PeasExtension *exten,
+ const gchar *method_name,
+ va_list args)
+{
+ PeasExtensionPython *pyexten = PEAS_EXTENSION_PYTHON (exten);
+ GObject *instance;
+
+ instance = pygobject_get (pyexten->instance);
+
+ return peas_method_apply_valist (instance, pyexten->gtype, method_name, args);
+}
+
+static void
+peas_extension_python_finalize (GObject *object)
+{
+ PeasExtensionPython *pyexten = PEAS_EXTENSION_PYTHON (object);
+
+ if (pyexten->instance)
+ {
+ Py_DECREF (pyexten->instance);
+ }
+
+ G_OBJECT_CLASS (peas_extension_python_parent_class)->finalize (object);
+}
+
+static void
+peas_extension_python_class_init (PeasExtensionPythonClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ PeasExtensionClass *extension_class = PEAS_EXTENSION_CLASS (klass);
+
+ object_class->finalize = peas_extension_python_finalize;
+
+ extension_class->call = peas_extension_python_call;
+}
+
+static void
+peas_extension_python_class_finalize (PeasExtensionPythonClass *klass)
+{
+}
+
+PeasExtension *
+peas_extension_python_new (GType gtype,
+ PyObject *instance)
+{
+ PeasExtensionPython *pyexten;
+
+ pyexten = PEAS_EXTENSION_PYTHON (g_object_new (PEAS_TYPE_EXTENSION_PYTHON, NULL));
+ pyexten->gtype = gtype;
+ pyexten->instance = instance;
+ Py_INCREF (instance);
+
+ return PEAS_EXTENSION (pyexten);
+}
diff --git a/loaders/python/peas-extension-python.h b/loaders/python/peas-extension-python.h
new file mode 100644
index 0000000..c6d06f4
--- /dev/null
+++ b/loaders/python/peas-extension-python.h
@@ -0,0 +1,59 @@
+/*
+ * peas-extension-python.h
+ * This file is part of libpeas
+ *
+ * Copyright (C) 2010 - Steve Frécinaux
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library 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 Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __PEAS_EXTENSION_PYTHON_H__
+#define __PEAS_EXTENSION_PYTHON_H__
+
+#include <libpeas/peas-extension.h>
+#include <Python.h>
+
+G_BEGIN_DECLS
+
+#define PEAS_TYPE_EXTENSION_PYTHON (peas_extension_python_get_type ())
+#define PEAS_EXTENSION_PYTHON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PEAS_TYPE_EXTENSION_PYTHON, PeasExtensionPython))
+#define PEAS_EXTENSION_PYTHON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PEAS_TYPE_EXTENSION_PYTHON, PeasExtensionPythonClass))
+#define PEAS_IS_EXTENSION_PYTHON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PEAS_TYPE_EXTENSION_PYTHON))
+#define PEAS_IS_EXTENSION_PYTHON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PEAS_TYPE_EXTENSION_PYTHON))
+#define PEAS_EXTENSION_PYTHON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PEAS_TYPE_EXTENSION_PYTHON, PeasExtensionPythonClass))
+
+typedef struct _PeasExtensionPython PeasExtensionPython;
+typedef struct _PeasExtensionPythonClass PeasExtensionPythonClass;
+
+struct _PeasExtensionPython {
+ PeasExtension parent;
+
+ GType gtype;
+ PyObject *instance;
+};
+
+struct _PeasExtensionPythonClass {
+ PeasExtensionClass parent_class;
+};
+
+GType peas_extension_python_get_type (void) G_GNUC_CONST;
+void peas_extension_python_register (GTypeModule *module);
+
+PeasExtension *peas_extension_python_new (GType gtype,
+ PyObject *instance);
+
+G_END_DECLS
+
+#endif /* __PEAS_EXTENSION_PYTHON_H__ */
diff --git a/loaders/python/peas-plugin-loader-python.c b/loaders/python/peas-plugin-loader-python.c
index 5c9beb2..24698ec 100644
--- a/loaders/python/peas-plugin-loader-python.c
+++ b/loaders/python/peas-plugin-loader-python.c
@@ -20,6 +20,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
+#include "peas-extension-python.h"
#include "peas-plugin-loader-python.h"
#if 0
@@ -27,14 +28,13 @@
#define NO_IMPORT_PYGTK
#endif
-/* _POSIX_C_SOURCE is defined in Python.h and in limits.h included by
- * glib-object.h, so we unset it here to avoid a warning. Yep, that's bad. */
-#undef _POSIX_C_SOURCE
#include <Python.h>
#include <pygobject.h>
#include <signal.h>
#include "config.h"
+extern PyTypeObject PyGObject_Type;
+
#if PY_VERSION_HEX < 0x02050000
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
@@ -48,24 +48,22 @@ struct _PeasPluginLoaderPythonPrivate {
};
typedef struct {
- PyObject *type;
- PyObject *instance;
+ PyObject *module;
} PythonInfo;
static gboolean peas_plugin_loader_python_add_module_path (PeasPluginLoaderPython *self,
const gchar *module_path);
-/* We retreive this to check for correct class hierarchy */
-static PyTypeObject *PyPeasPlugin_Type;
-
PEAS_PLUGIN_LOADER_REGISTER_TYPE (PeasPluginLoaderPython, peas_plugin_loader_python);
-static PyObject *
-find_python_plugin_type (PeasPluginInfo *info,
- PyObject *pymodule)
+static PyTypeObject *
+find_python_extension_type (PeasPluginInfo *info,
+ GType exten_type,
+ PyObject *pymodule)
{
PyObject *locals, *key, *value;
Py_ssize_t pos = 0;
+ GObject *object;
locals = PyModule_GetDict (pymodule);
@@ -74,34 +72,55 @@ find_python_plugin_type (PeasPluginInfo *info,
if (!PyType_Check (value))
continue;
- if (PyObject_IsSubclass (value, (PyObject *) PyPeasPlugin_Type))
- return value;
+ if (!PyObject_IsSubclass (value, (PyObject *) &PyGObject_Type))
+ continue;
+
+ object = pygobject_get (value);
+ if (G_TYPE_CHECK_INSTANCE_TYPE (object, exten_type))
+ return (PyTypeObject *) value;
}
- g_warning ("No PeasPlugin derivative found in Python plugin '%s'",
- peas_plugin_info_get_name (info));
+ g_debug ("No %s derivative found in Python plugin '%s'",
+ g_type_name (exten_type), peas_plugin_info_get_name (info));
return NULL;
}
-static PeasPlugin *
-new_plugin_from_info (PeasPluginLoaderPython *loader,
- PeasPluginInfo *info)
+static gboolean
+peas_plugin_loader_python_provides_extension (PeasPluginLoader *loader,
+ PeasPluginInfo *info,
+ GType exten_type)
{
+ PeasPluginLoaderPython *pyloader = PEAS_PLUGIN_LOADER_PYTHON (loader);
+ PythonInfo *pyinfo;
+
+ pyinfo = (PythonInfo *) g_hash_table_lookup (pyloader->priv->loaded_plugins, info);
+
+ if (pyinfo == NULL)
+ return FALSE;
+
+ return find_python_extension_type (info, exten_type, pyinfo->module) != NULL;
+}
+
+static PeasExtension *
+peas_plugin_loader_python_get_extension (PeasPluginLoader *loader,
+ PeasPluginInfo *info,
+ GType exten_type)
+{
+ PeasPluginLoaderPython *pyloader = PEAS_PLUGIN_LOADER_PYTHON (loader);
PythonInfo *pyinfo;
PyTypeObject *pytype;
PyObject *pyobject;
PyGObject *pygobject;
- PeasPlugin *instance;
PyObject *emptyarg;
- pyinfo = (PythonInfo *) g_hash_table_lookup (loader->priv->loaded_plugins, info);
+ pyinfo = (PythonInfo *) g_hash_table_lookup (pyloader->priv->loaded_plugins, info);
if (pyinfo == NULL)
return NULL;
- pytype = (PyTypeObject *) pyinfo->type;
+ pytype = find_python_extension_type (info, exten_type, pyinfo->module);
- if (pytype->tp_new == NULL)
+ if (pytype == NULL || pytype->tp_new == NULL)
return NULL;
emptyarg = PyTuple_New (0);
@@ -130,8 +149,8 @@ new_plugin_from_info (PeasPluginLoaderPython *loader,
if (pygobject->obj == NULL)
{
- g_error ("Could not create instance for %s (GObject not constructed).",
- peas_plugin_info_get_name (info));
+ g_error ("Could not create %s instance for %s (GObject not constructed).",
+ g_type_name (exten_type), peas_plugin_info_get_name (info));
Py_DECREF (pyobject);
return NULL;
@@ -146,29 +165,21 @@ new_plugin_from_info (PeasPluginLoaderPython *loader,
Py_DECREF (emptyarg);
}
- instance = PEAS_PLUGIN (pygobject->obj);
- pyinfo->instance = (PyObject *) pygobject;
-
- /* we return a reference here because the other is owned by python */
- return PEAS_PLUGIN (g_object_ref (instance));
+ return peas_extension_python_new (exten_type, pyobject);
}
-static PeasPlugin *
+static void
add_python_info (PeasPluginLoaderPython *loader,
PeasPluginInfo *info,
- PyObject *module,
- PyObject *type)
+ PyObject *module)
{
PythonInfo *pyinfo;
pyinfo = g_new (PythonInfo, 1);
- pyinfo->type = type;
-
- Py_INCREF (pyinfo->type);
+ pyinfo->module = module;
+ Py_INCREF (pyinfo->module);
g_hash_table_insert (loader->priv->loaded_plugins, info, pyinfo);
-
- return new_plugin_from_info (loader, info);
}
static void
@@ -181,35 +192,32 @@ peas_plugin_loader_python_add_module_directory (PeasPluginLoader *loader,
peas_plugin_loader_python_add_module_path (pyloader, module_dir);
}
-static PeasPlugin *
+static gboolean
peas_plugin_loader_python_load (PeasPluginLoader *loader,
PeasPluginInfo *info)
{
PeasPluginLoaderPython *pyloader = PEAS_PLUGIN_LOADER_PYTHON (loader);
- PyObject *main_module, *main_locals, *pytype;
+ PyObject *main_module, *main_locals;
PyObject *pymodule, *fromlist;
gchar *module_name;
- PeasPlugin *result;
if (pyloader->priv->init_failed)
{
g_warning ("Cannot load python plugin Python '%s' since libpeas was"
"not able to initialize the Python interpreter.",
peas_plugin_info_get_name (info));
- return NULL;
+ return FALSE;
}
/* see if py definition for the plugin is already loaded */
- result = new_plugin_from_info (pyloader, info);
-
- if (result != NULL)
- return result;
+ if (g_hash_table_lookup (pyloader->priv->loaded_plugins, info))
+ return TRUE;
main_module = PyImport_AddModule ("libpeas.plugins");
if (main_module == NULL)
{
g_warning ("Could not get libpeas.plugins.");
- return NULL;
+ return FALSE;
}
/* If we have a special path, we register it */
@@ -234,18 +242,15 @@ peas_plugin_loader_python_load (PeasPluginLoader *loader,
{
g_free (module_name);
PyErr_Print ();
- return NULL;
+ return FALSE;
}
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, pytype);
+ add_python_info (pyloader, info, pymodule);
- return NULL;
+ return TRUE;
}
static void
@@ -262,10 +267,10 @@ peas_plugin_loader_python_unload (PeasPluginLoader *loader,
return;
state = pyg_gil_state_ensure ();
- Py_XDECREF (pyinfo->instance);
+ Py_XDECREF (pyinfo->module);
pyg_gil_state_release (state);
- pyinfo->instance = NULL;
+ pyinfo->module = NULL;
}
static gboolean
@@ -385,14 +390,6 @@ peas_init_libpeas (void)
Py_DECREF (required_version);
- /* Retrieve the Python type for libpeas.Plugin */
- PyPeasPlugin_Type = (PyTypeObject *) PyDict_GetItemString (mdict, "Plugin");
- if (PyPeasPlugin_Type == NULL)
- {
- PyErr_SetString (PyExc_ImportError, "could not find libpeas.Plugin");
- return;
- }
-
/* initialize empty libpeas.plugins module */
pluginsmodule = Py_InitModule ("libpeas.plugins", NULL);
PyDict_SetItemString (mdict, "plugins", pluginsmodule);
@@ -523,7 +520,7 @@ static void
destroy_python_info (PythonInfo *info)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- Py_XDECREF (info->type);
+ Py_XDECREF (info->module);
pyg_gil_state_release (state);
g_free (info);
@@ -568,6 +565,8 @@ peas_plugin_loader_python_class_init (PeasPluginLoaderPythonClass *klass)
loader_class->add_module_directory = peas_plugin_loader_python_add_module_directory;
loader_class->load = peas_plugin_loader_python_load;
loader_class->unload = peas_plugin_loader_python_unload;
+ loader_class->get_extension = peas_plugin_loader_python_get_extension;
+ loader_class->provides_extension = peas_plugin_loader_python_provides_extension;
loader_class->garbage_collect = peas_plugin_loader_python_garbage_collect;
g_type_class_add_private (object_class,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]