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
|
#include <Python.h>
#include <structmember.h>
#include <lasso/lasso.h>
#include <lasso_config.h>
GQuark lasso_wrapper_key;
PyMODINIT_FUNC init_lasso(void);
static PyObject* get_pystring_from_xml_node(xmlNode *xmlnode);
static xmlNode* get_xml_node_from_pystring(PyObject *string);
static PyObject* get_dict_from_hashtable_of_strings(GHashTable *value);
static PyObject* get_dict_from_hashtable_of_objects(GHashTable *value);
static PyObject* PyGObjectPtr_New(GObject *obj);
/* utility functions */
#if (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 14)
/* copy of private struct and g_hash_table_get_keys from GLib internals
* (as this function is useful but new in 2.14) */
typedef struct _GHashNode GHashNode;
struct _GHashNode
{
gpointer key;
gpointer value;
GHashNode *next;
guint key_hash;
};
struct _GHashTable
{
gint size;
gint nnodes;
GHashNode **nodes;
GHashFunc hash_func;
GEqualFunc key_equal_func;
volatile gint ref_count;
GDestroyNotify key_destroy_func;
GDestroyNotify value_destroy_func;
};
static GList *
g_hash_table_get_keys (GHashTable *hash_table)
{
GHashNode *node;
gint i;
GList *retval;
g_return_val_if_fail (hash_table != NULL, NULL);
retval = NULL;
for (i = 0; i < hash_table->size; i++)
for (node = hash_table->nodes[i]; node; node = node->next)
retval = g_list_prepend (retval, node->key);
return retval;
}
#endif
static PyObject*
get_dict_from_hashtable_of_strings(GHashTable *value)
{
GList *keys;
PyObject *dict;
char *item_value;
PyObject *item;
dict = PyDict_New();
keys = g_hash_table_get_keys(value);
for (; keys; keys = g_list_next(keys)) {
item_value = g_hash_table_lookup(value, keys->data);
if (item_value) {
item = PyString_FromString(item_value);
PyDict_SetItemString(dict, (char*)keys->data, item);
Py_DECREF(item);
} else {
PyDict_SetItemString(dict, (char*)keys->data, Py_None);
}
}
g_list_free(keys);
return PyDictProxy_New(dict);
}
static PyObject*
get_dict_from_hashtable_of_objects(GHashTable *value)
{
GList *keys;
PyObject *dict;
GObject *item_value;
PyObject *item;
dict = PyDict_New();
keys = g_hash_table_get_keys(value);
for (; keys; keys = g_list_next(keys)) {
item_value = g_hash_table_lookup(value, keys->data);
if (item_value) {
item = PyGObjectPtr_New(G_OBJECT(item_value));
PyDict_SetItemString(dict, (char*)keys->data, item);
Py_DECREF(item);
} else {
PyDict_SetItemString(dict, (char*)keys->data, Py_None);
}
}
g_list_free(keys);
return PyDictProxy_New(dict);
}
static PyObject*
get_pystring_from_xml_node(xmlNode *xmlnode)
{
xmlOutputBufferPtr buf;
PyObject *pystring = NULL;
if (xmlnode == NULL) {
return NULL;
}
buf = xmlAllocOutputBuffer(NULL);
if (buf == NULL) {
pystring = NULL;
} else {
xmlNodeDumpOutput(buf, NULL, xmlnode, 0, 1, NULL);
xmlOutputBufferFlush(buf);
if (buf->conv == NULL) {
pystring = PyString_FromString((char*)buf->buffer->content);
} else {
pystring = PyString_FromString((char*)buf->conv->content);
}
xmlOutputBufferClose(buf);
}
return pystring;
}
static xmlNode*
get_xml_node_from_pystring(PyObject *string) {
xmlDoc *doc;
xmlNode *node;
doc = xmlReadDoc((xmlChar*)PyString_AsString(string), NULL, NULL, XML_PARSE_NONET);
node = xmlDocGetRootElement(doc);
if (node != NULL) {
node = xmlCopyNode(node, 1);
}
xmlFreeDoc(doc);
return node;
}
/* wrapper around GObject */
typedef struct {
PyObject_HEAD
GObject *obj;
PyObject *typename;
} PyGObjectPtr;
static PyTypeObject PyGObjectPtrType;
static void
PyGObjectPtr_dealloc(PyGObjectPtr *self)
{
#ifdef LASSO_DEBUG
fprintf(stderr, "dealloc (%p ptr to %p (type:%s, rc:%d))\n",
self, self->obj,
G_OBJECT_TYPE_NAME(self->obj),
self->obj->ref_count);
#endif
g_object_set_qdata_full(self->obj, lasso_wrapper_key, NULL, NULL);
g_object_unref(self->obj);
Py_XDECREF(self->typename);
self->ob_type->tp_free((PyObject*)self);
}
static PyObject*
PyGObjectPtr_New(GObject *obj)
{
PyGObjectPtr *self;
if (obj == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
self = (PyGObjectPtr*)g_object_get_qdata(obj, lasso_wrapper_key);
if (self != NULL) {
Py_INCREF(self);
} else {
self = (PyGObjectPtr*)PyObject_NEW(PyGObjectPtr, &PyGObjectPtrType);
g_object_set_qdata_full(obj, lasso_wrapper_key, self, NULL);
self->obj = obj;
self->typename = PyString_FromString(G_OBJECT_TYPE_NAME(obj)+5);
}
return (PyObject*)self;
}
static PyObject *
PyGObjectPtr_repr(PyGObjectPtr *obj)
{
return PyString_FromFormat("<PyGObjectPtr to %p (type: %s, refcount: %d)>",
obj->obj,
G_OBJECT_TYPE_NAME(obj->obj),
obj->obj->ref_count);
}
static PyMemberDef PyGObjectPtr_members[] = {
{"typename", T_OBJECT, offsetof(PyGObjectPtr, typename), 0, "typename"},
{NULL}
};
static PyObject* PyGObjectPtr_get_refcount(PyGObjectPtr *self, void *closure)
{
PyObject *refcount;
refcount = PyInt_FromLong(self->obj->ref_count);
Py_INCREF(refcount);
return refcount;
}
static PyGetSetDef PyGObjectPtr_getseters[] = {
{"refcount", (getter)PyGObjectPtr_get_refcount, NULL,
"reference count of intern GObject*", NULL},
{NULL} /* Sentinel */
};
static PyTypeObject PyGObjectPtrType = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"_lasso.PyGObjectPtr", /* tp_name */
sizeof(PyGObjectPtr), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)PyGObjectPtr_dealloc, /* tp_dealloc */
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
(reprfunc)PyGObjectPtr_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"PyGObjectPtr objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
PyGObjectPtr_members, /* tp_members */
PyGObjectPtr_getseters, /* tp_getset */
};
|