1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*
* peas-plugin-loader-python.c
* This file is part of libpeas
*
* Copyright (C) 2008 - Jesse van den Kieboom
* Copyright (C) 2009 - Steve Frécinaux
*
* libpeas is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* libpeas 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include "peas-plugin-loader-python.h"
#include "peas-python-internal.h"
#include "libpeas/peas-plugin-info-priv.h"
/* _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 <pygobject.h>
struct _PeasPluginLoaderPython {
PeasPluginLoader parent_instance;
PyThreadState *py_thread_state;
guint n_loaded_plugins;
guint init_failed : 1;
guint must_finalize_python : 1;
};
G_DEFINE_FINAL_TYPE (PeasPluginLoaderPython, peas_plugin_loader_python, PEAS_TYPE_PLUGIN_LOADER)
static GQuark quark_extension_type = 0;
G_MODULE_EXPORT void
peas_register_types (PeasObjectModule *module)
{
peas_object_module_register_extension_type (module,
PEAS_TYPE_PLUGIN_LOADER,
PEAS_TYPE_PLUGIN_LOADER_PYTHON);
}
static GType
find_python_extension_type (GType exten_type,
PyObject *pymodule)
{
PyObject *pyexten_type, *pytype;
GType the_type = G_TYPE_INVALID;
pyexten_type = pyg_type_wrapper_new (exten_type);
pytype = peas_python_internal_call ("find_extension_type",
&PyType_Type, "(OO)",
pyexten_type, pymodule);
Py_DECREF (pyexten_type);
if (pytype != NULL)
{
the_type = pyg_type_from_object (pytype);
Py_DECREF (pytype);
g_return_val_if_fail (g_type_is_a (the_type, exten_type),
G_TYPE_INVALID);
}
return the_type;
}
static gboolean
peas_plugin_loader_python_provides_extension (PeasPluginLoader *loader,
PeasPluginInfo *info,
GType exten_type)
{
PyObject *pymodule = info->loader_data;
GType the_type;
PyGILState_STATE state = PyGILState_Ensure ();
the_type = find_python_extension_type (exten_type, pymodule);
PyGILState_Release (state);
return the_type != G_TYPE_INVALID;
}
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
static GObject *
peas_plugin_loader_python_create_extension (PeasPluginLoader *loader,
PeasPluginInfo *info,
GType exten_type,
guint n_parameters,
GParameter *parameters)
{
PyObject *pymodule = info->loader_data;
GType the_type;
GObject *object = NULL;
PyObject *pyobject;
PyObject *pyplinfo;
PyGILState_STATE state = PyGILState_Ensure ();
the_type = find_python_extension_type (exten_type, pymodule);
if (the_type == G_TYPE_INVALID)
goto out;
object = g_object_newv (the_type, n_parameters, parameters);
if (object == NULL)
goto out;
/* Sink floating references if necessary */
if (g_object_is_floating (object))
g_object_ref_sink (object);
/* We have to remember which interface we are instantiating
* for the deprecated peas_extension_get_extension_type().
*/
g_object_set_qdata (object, quark_extension_type,
GSIZE_TO_POINTER (exten_type));
pyobject = pygobject_new (object);
pyplinfo = pygobject_new (G_OBJECT (info));
/* Set the plugin info as an attribute of the instance */
if (PyObject_SetAttrString (pyobject, "plugin_info", pyplinfo) != 0)
{
g_warning ("Failed to set 'plugin_info' for '%s'",
g_type_name (the_type));
if (PyErr_Occurred ())
PyErr_Print ();
g_clear_object (&object);
}
Py_DECREF (pyplinfo);
Py_DECREF (pyobject);
out:
PyGILState_Release (state);
return object;
}
G_GNUC_END_IGNORE_DEPRECATIONS
static gboolean
peas_plugin_loader_python_load (PeasPluginLoader *loader,
PeasPluginInfo *info)
{
PeasPluginLoaderPython *pyloader = PEAS_PLUGIN_LOADER_PYTHON (loader);
const char *module_dir, *module_name;
PyObject *pymodule;
PyGILState_STATE state = PyGILState_Ensure ();
module_dir = peas_plugin_info_get_module_dir (info);
module_name = peas_plugin_info_get_module_name (info);
pymodule = peas_python_internal_call ("load", &PyModule_Type, "(sss)",
info->filename,
module_dir, module_name);
if (pymodule != NULL)
{
info->loader_data = pymodule;
pyloader->n_loaded_plugins += 1;
}
PyGILState_Release (state);
return pymodule != NULL;
}
static void
peas_plugin_loader_python_unload (PeasPluginLoader *loader,
PeasPluginInfo *info)
{
PeasPluginLoaderPython *pyloader = PEAS_PLUGIN_LOADER_PYTHON (loader);
PyGILState_STATE state = PyGILState_Ensure ();
/* We have to use this as a hook as the
* loader will not be finalized by applications
*/
if (--pyloader->n_loaded_plugins == 0)
peas_python_internal_call ("all_plugins_unloaded", NULL, NULL);
Py_CLEAR (info->loader_data);
PyGILState_Release (state);
}
static void
peas_plugin_loader_python_garbage_collect (PeasPluginLoader *loader)
{
PyGILState_STATE state = PyGILState_Ensure ();
peas_python_internal_call ("garbage_collect", NULL, NULL);
PyGILState_Release (state);
}
static gboolean
peas_plugin_loader_python_initialize (PeasPluginLoader *loader)
{
PeasPluginLoaderPython *pyloader = PEAS_PLUGIN_LOADER_PYTHON (loader);
PyGILState_STATE state = 0;
long hexversion;
/* We can't support multiple Python interpreter states:
* https://bugzilla.gnome.org/show_bug.cgi?id=677091
*/
/* Python initialization */
if (Py_IsInitialized ())
{
state = PyGILState_Ensure ();
}
else
{
Py_InitializeEx (FALSE);
pyloader->must_finalize_python = TRUE;
}
hexversion = PyLong_AsLong (PySys_GetObject ((char *) "hexversion"));
#if PY_VERSION_HEX < 0x03000000
if (hexversion >= 0x03000000)
#else
if (hexversion < 0x03000000)
#endif
{
g_critical ("Attempting to mix incompatible Python versions");
goto python_init_error;
}
/* Initialize PyGObject */
pygobject_init (PYGOBJECT_MAJOR_VERSION,
PYGOBJECT_MINOR_VERSION,
PYGOBJECT_MICRO_VERSION);
if (PyErr_Occurred ())
{
g_warning ("Error initializing Python Plugin Loader: "
"PyGObject initialization failed");
goto python_init_error;
}
/* Initialize support for threads */
pyg_enable_threads ();
/* Only redirect warnings when python was not already initialized */
if (!pyloader->must_finalize_python)
pyg_disable_warning_redirections ();
/* Must be done last, finalize() depends on init_failed */
if (!peas_python_internal_setup (!pyloader->must_finalize_python))
{
/* Already warned */
goto python_init_error;
}
if (!pyloader->must_finalize_python)
PyGILState_Release (state);
else
pyloader->py_thread_state = PyEval_SaveThread ();
return TRUE;
python_init_error:
if (PyErr_Occurred ())
PyErr_Print ();
g_warning ("Please check the installation of all the Python "
"related packages required by libpeas and try again");
if (!pyloader->must_finalize_python)
PyGILState_Release (state);
pyloader->init_failed = TRUE;
return FALSE;
}
static void
peas_plugin_loader_python_init (PeasPluginLoaderPython *pyloader)
{
}
static void
peas_plugin_loader_python_finalize (GObject *object)
{
PeasPluginLoaderPython *pyloader = PEAS_PLUGIN_LOADER_PYTHON (object);
PyGILState_STATE state;
if (!Py_IsInitialized ())
goto out;
g_warn_if_fail (pyloader->n_loaded_plugins == 0);
if (!pyloader->init_failed)
{
state = PyGILState_Ensure ();
peas_python_internal_shutdown ();
PyGILState_Release (state);
}
if (pyloader->py_thread_state)
PyEval_RestoreThread (pyloader->py_thread_state);
if (pyloader->must_finalize_python)
{
if (!pyloader->init_failed)
PyGILState_Ensure ();
Py_Finalize ();
}
out:
G_OBJECT_CLASS (peas_plugin_loader_python_parent_class)->finalize (object);
}
static void
peas_plugin_loader_python_class_init (PeasPluginLoaderPythonClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
PeasPluginLoaderClass *loader_class = PEAS_PLUGIN_LOADER_CLASS (klass);
quark_extension_type = g_quark_from_static_string ("peas-extension-type");
object_class->finalize = peas_plugin_loader_python_finalize;
loader_class->initialize = peas_plugin_loader_python_initialize;
loader_class->load = peas_plugin_loader_python_load;
loader_class->unload = peas_plugin_loader_python_unload;
loader_class->create_extension = peas_plugin_loader_python_create_extension;
loader_class->provides_extension = peas_plugin_loader_python_provides_extension;
loader_class->garbage_collect = peas_plugin_loader_python_garbage_collect;
}
|