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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
|
/*
* pluma-plugin-loader-python.c
* This file is part of pluma
*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "pluma-plugin-loader-python.h"
#include "pluma-plugin-python.h"
#include <pluma/pluma-object-module.h>
#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
#define PLUMA_PLUGIN_LOADER_PYTHON_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), PLUMA_TYPE_PLUGIN_LOADER_PYTHON, PlumaPluginLoaderPythonPrivate))
struct _PlumaPluginLoaderPythonPrivate
{
GHashTable *loaded_plugins;
guint idle_gc;
gboolean init_failed;
};
typedef struct
{
PyObject *type;
PyObject *instance;
gchar *path;
} PythonInfo;
static void pluma_plugin_loader_iface_init (gpointer g_iface, gpointer iface_data);
/* Exported by pypluma module */
void pypluma_register_classes (PyObject *d);
void pypluma_add_constants (PyObject *module, const gchar *strip_prefix);
extern PyMethodDef pypluma_functions[];
/* Exported by pyplumautils module */
void pyplumautils_register_classes (PyObject *d);
extern PyMethodDef pyplumautils_functions[];
/* Exported by pyplumacommands module */
void pyplumacommands_register_classes (PyObject *d);
extern PyMethodDef pyplumacommands_functions[];
/* We retreive this to check for correct class hierarchy */
static PyTypeObject *PyPlumaPlugin_Type;
PLUMA_PLUGIN_LOADER_REGISTER_TYPE (PlumaPluginLoaderPython, pluma_plugin_loader_python, G_TYPE_OBJECT, pluma_plugin_loader_iface_init);
static PyObject *
find_python_plugin_type (PlumaPluginInfo *info,
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;
if (PyObject_IsSubclass (value, (PyObject*) PyPlumaPlugin_Type))
return value;
}
g_warning ("No PlumaPlugin derivative found in Python plugin '%s'",
pluma_plugin_info_get_name (info));
return NULL;
}
static PlumaPlugin *
new_plugin_from_info (PlumaPluginLoaderPython *loader,
PlumaPluginInfo *info)
{
PythonInfo *pyinfo;
PyTypeObject *pytype;
PyObject *pyobject;
PyGObject *pygobject;
PlumaPlugin *instance;
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)
{
g_error ("Could not create instance for %s.", pluma_plugin_info_get_name (info));
return NULL;
}
pygobject = (PyGObject *)pyobject;
if (pygobject->obj != NULL)
{
Py_DECREF (pyobject);
g_error ("Could not create instance for %s (GObject already initialized).", pluma_plugin_info_get_name (info));
return NULL;
}
pygobject_construct (pygobject,
"install-dir", pyinfo->path,
"data-dir-name", pluma_plugin_info_get_module_name (info),
NULL);
if (pygobject->obj == NULL)
{
g_error ("Could not create instance for %s (GObject not constructed).", pluma_plugin_info_get_name (info));
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);
}
instance = PLUMA_PLUGIN (pygobject->obj);
pyinfo->instance = (PyObject *)pygobject;
/* 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);
/* we return a reference here because the other is owned by python */
return PLUMA_PLUGIN (g_object_ref (instance));
}
static PlumaPlugin *
add_python_info (PlumaPluginLoaderPython *loader,
PlumaPluginInfo *info,
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 *
pluma_plugin_loader_iface_get_id (void)
{
return "Python";
}
static PlumaPlugin *
pluma_plugin_loader_iface_load (PlumaPluginLoader *loader,
PlumaPluginInfo *info,
const gchar *path)
{
PlumaPluginLoaderPython *pyloader = PLUMA_PLUGIN_LOADER_PYTHON (loader);
PyObject *main_module, *main_locals, *pytype;
PyObject *pymodule, *fromlist;
gchar *module_name;
PlumaPlugin *result;
if (pyloader->priv->init_failed)
{
g_warning ("Cannot load python plugin Python '%s' since pluma was"
"not able to initialize the Python interpreter.",
pluma_plugin_info_get_name (info));
return NULL;
}
/* see if py definition for the plugin is already loaded */
result = new_plugin_from_info (pyloader, info);
if (result != NULL)
return result;
main_module = PyImport_AddModule ("pluma.plugins");
if (main_module == NULL)
{
g_warning ("Could not get pluma.plugins.");
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);
module_name = g_strdup (pluma_plugin_info_get_module_name (info));
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
pluma_plugin_loader_iface_unload (PlumaPluginLoader *loader,
PlumaPluginInfo *info)
{
PlumaPluginLoaderPython *pyloader = PLUMA_PLUGIN_LOADER_PYTHON (loader);
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
run_gc (PlumaPluginLoaderPython *loader)
{
while (PyGC_Collect ())
;
loader->priv->idle_gc = 0;
return FALSE;
}
static void
pluma_plugin_loader_iface_garbage_collect (PlumaPluginLoader *loader)
{
PlumaPluginLoaderPython *pyloader;
if (!Py_IsInitialized())
return;
pyloader = PLUMA_PLUGIN_LOADER_PYTHON (loader);
/*
* 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
pluma_plugin_loader_iface_init (gpointer g_iface,
gpointer iface_data)
{
PlumaPluginLoaderInterface *iface = (PlumaPluginLoaderInterface *)g_iface;
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;
}
static void
pluma_python_shutdown (PlumaPluginLoaderPython *loader)
{
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
pluma_check_pygtk2 (void)
{
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
pluma_init_pygobject (void)
{
init_pygobject_check (2, 11, 5); /* FIXME: get from config */
}
static void
pluma_init_pygtk (void)
{
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
pluma_init_pygtksourceview (void)
{
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
pluma_python_init (PlumaPluginLoaderPython *loader)
{
PyObject *mdict, *tuple;
PyObject *pluma, *plumautils, *plumacommands, *plumaplugins;
PyObject *gettext, *install, *gettext_args;
//char *argv[] = { "pluma", NULL };
char *argv[] = { PLUMA_PLUGINS_LIBS_DIR, NULL };
#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);
if (!pluma_check_pygtk2 ())
{
/* Warning message already printed in check_pygtk2 */
goto python_init_error;
}
/* import gobject */
pluma_init_pygobject ();
if (PyErr_Occurred ())
{
g_warning ("Error initializing Python interpreter: could not import pygobject.");
goto python_init_error;
}
/* import gtk */
pluma_init_pygtk ();
if (PyErr_Occurred ())
{
g_warning ("Error initializing Python interpreter: could not import pygtk.");
goto python_init_error;
}
/* import gtksourceview */
pluma_init_pygtksourceview ();
if (PyErr_Occurred ())
{
PyErr_Print ();
g_warning ("Error initializing Python interpreter: could not import pygtksourceview.");
goto python_init_error;
}
/* import pluma */
pluma = Py_InitModule ("pluma", pypluma_functions);
mdict = PyModule_GetDict (pluma);
pypluma_register_classes (mdict);
pypluma_add_constants (pluma, "PLUMA_");
/* pluma version */
tuple = Py_BuildValue("(iii)",
PLUMA_MAJOR_VERSION,
PLUMA_MINOR_VERSION,
PLUMA_MICRO_VERSION);
PyDict_SetItemString(mdict, "version", tuple);
Py_DECREF(tuple);
/* Retrieve the Python type for pluma.Plugin */
PyPlumaPlugin_Type = (PyTypeObject *) PyDict_GetItemString (mdict, "Plugin");
if (PyPlumaPlugin_Type == NULL)
{
PyErr_Print ();
goto python_init_error;
}
/* import pluma.utils */
plumautils = Py_InitModule ("pluma.utils", pyplumautils_functions);
PyDict_SetItemString (mdict, "utils", plumautils);
/* import pluma.commands */
plumacommands = Py_InitModule ("pluma.commands", pyplumacommands_functions);
PyDict_SetItemString (mdict, "commands", plumacommands);
/* initialize empty pluma.plugins module */
plumaplugins = Py_InitModule ("pluma.plugins", NULL);
PyDict_SetItemString (mdict, "plugins", plumaplugins);
mdict = PyModule_GetDict (plumautils);
pyplumautils_register_classes (mdict);
mdict = PyModule_GetDict (plumacommands);
pyplumacommands_register_classes (mdict);
/* 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");
gettext_args = Py_BuildValue ("ss", GETTEXT_PACKAGE, PLUMA_LOCALEDIR);
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 "
"by pluma and try again.");
PyErr_Clear ();
pluma_python_shutdown (loader);
return FALSE;
}
static void
pluma_plugin_loader_python_finalize (GObject *object)
{
PlumaPluginLoaderPython *pyloader = PLUMA_PLUGIN_LOADER_PYTHON (object);
g_hash_table_destroy (pyloader->priv->loaded_plugins);
pluma_python_shutdown (pyloader);
G_OBJECT_CLASS (pluma_plugin_loader_python_parent_class)->finalize (object);
}
static void
pluma_plugin_loader_python_class_init (PlumaPluginLoaderPythonClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = pluma_plugin_loader_python_finalize;
g_type_class_add_private (object_class, sizeof (PlumaPluginLoaderPythonPrivate));
}
static void
pluma_plugin_loader_python_class_finalize (PlumaPluginLoaderPythonClass *klass)
{
}
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
pluma_plugin_loader_python_init (PlumaPluginLoaderPython *self)
{
self->priv = PLUMA_PLUGIN_LOADER_PYTHON_GET_PRIVATE (self);
/* initialize python interpreter */
pluma_python_init (self);
/* loaded_plugins maps PlumaPluginInfo to a PythonInfo */
self->priv->loaded_plugins = g_hash_table_new_full (g_direct_hash,
g_direct_equal,
NULL,
(GDestroyNotify)destroy_python_info);
}
PlumaPluginLoaderPython *
pluma_plugin_loader_python_new ()
{
GObject *loader = g_object_new (PLUMA_TYPE_PLUGIN_LOADER_PYTHON, NULL);
return PLUMA_PLUGIN_LOADER_PYTHON (loader);
}
|