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
|
/*
* Copyright © 2004, 2005 Jean-François Rameau
* Copyright © 2004, 2005 Adam Hooper
* Copyright © 2004, 2005 Crispin Flowerday
* Copyright © 2004, 2005 Christian Persch
*
* 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, 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $Id$
*/
#include <Python.h>
#include "config.h"
#include "ephy-python.h"
#include "ephy-debug.h"
#include <pygobject.h>
#include <pygtk/pygtk.h>
void pyepiphany_register_classes (PyObject *d);
void pyepiphany_add_constants (PyObject *module, const gchar *strip_prefix);
extern PyMethodDef pyepiphany_functions[];
static guint idle_gc_handler = 0;
static guint idle_shutdown_handler = 0;
static gboolean python_initialised = FALSE;
void
ephy_python_init (void)
{
char *argv[1];
PyObject *m, *d;
Py_Initialize();
python_initialised = TRUE;
argv[0] = g_get_prgname ();
PySys_SetArgv (1, argv);
init_pygobject ();
init_pygtk ();
m = Py_InitModule ("epiphany", pyepiphany_functions);
d = PyModule_GetDict (m);
pyepiphany_register_classes (d);
pyepiphany_add_constants (m, "EPHY_");
}
static gboolean
idle_shutdown (void)
{
g_return_val_if_fail (idle_gc_handler == 0, FALSE);
Py_Finalize ();
idle_shutdown_handler = 0;
return FALSE;
}
void
ephy_python_shutdown (void)
{
if (!python_initialised) return;
g_return_if_fail (idle_shutdown_handler == 0);
LOG ("EphyPython shutdown with %s GC scheduled",
idle_gc_handler != 0 ? "a" : "no");
if (idle_gc_handler != 0)
{
/* Process remaining GCs now */
while (PyGC_Collect ()) ;
g_source_remove (idle_gc_handler);
idle_gc_handler = 0;
/* IMPORTANT! We get here while running PyGC_Collect from idle!
* Don't do Py_Finalize while inside python!
*/
idle_shutdown_handler = g_idle_add ((GSourceFunc) idle_shutdown, NULL);
}
else
{
Py_Finalize();
}
}
static gboolean
idle_gc (void)
{
long value;
/* LOG ("Running GC from idle"); */
/* FIXME what does the return value of PyGC_Collect mean? */
value = PyGC_Collect ();
/* LOG ("Idle GC returned %ld", value); */
if (value == 0)
{
idle_gc_handler = 0;
}
return value != 0;
}
void
ephy_python_schedule_gc (void)
{
/* LOG ("Scheduling a GC with %s GC already scheduled", idle_gc_handler != 0 ? "a" : "no"); */
if (python_initialised && idle_gc_handler == 0)
{
idle_gc_handler = g_idle_add ((GSourceFunc) idle_gc, NULL);
}
}
|