summaryrefslogtreecommitdiff
path: root/gi/pygboxed.c
blob: 1f355be308eb9828870658d6fc055e9fa88932bd (plain)
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
/* -*- Mode: C; c-basic-offset: 4 -*-
 * pygtk- Python bindings for the GTK toolkit.
 * Copyright (C) 1998-2003  James Henstridge
 *
 *   pygboxed.c: wrapper for GBoxed
 *
 * This library 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.
 *
 * This library 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, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <Python.h>
#include <glib-object.h>

#include "pygboxed.h"
#include "pygi-type.h"
#include "pygi-type.h"
#include "pygi-util.h"

GQuark pygboxed_type_key;

PYGLIB_DEFINE_TYPE("gobject.GBoxed", PyGBoxed_Type, PyGBoxed);

static void
gboxed_dealloc(PyGBoxed *self)
{
    if (self->free_on_dealloc && pyg_boxed_get_ptr (self)) {
	PyGILState_STATE state = PyGILState_Ensure();
	g_boxed_free (self->gtype, pyg_boxed_get_ptr (self));
	PyGILState_Release(state);
    }

    Py_TYPE(self)->tp_free((PyObject *)self);
}

static PyObject*
gboxed_richcompare(PyObject *self, PyObject *other, int op)
{
    if (Py_TYPE(self) == Py_TYPE(other) &&
        PyObject_IsInstance(self, (PyObject*)&PyGBoxed_Type))
        return pyg_ptr_richcompare (pyg_boxed_get_ptr (self),
                                    pyg_boxed_get_ptr (other),
                                    op);
    else {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }
}

static PYGLIB_Py_hash_t
gboxed_hash(PyGBoxed *self)
{
    return PYGLIB_Py_hash_t_FromVoidPtr (pyg_boxed_get_ptr (self));
}

static PyObject *
gboxed_repr(PyGBoxed *boxed)
{
    PyObject *module, *repr, *self = (PyObject *)boxed;
    gchar *module_str, *namespace;

    module = PyObject_GetAttrString (self, "__module__");
    if (module == NULL)
        return NULL;

    if (!PYGLIB_PyUnicode_Check (module)) {
        Py_DECREF (module);
        return NULL;
    }

    module_str = PYGLIB_PyUnicode_AsString (module);
    namespace = g_strrstr (module_str, ".");
    if (namespace == NULL) {
        namespace = module_str;
    } else {
        namespace += 1;
    }

    repr = PYGLIB_PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
                                        namespace, Py_TYPE (self)->tp_name,
                                        self, g_type_name (boxed->gtype),
                                        pyg_boxed_get_ptr (boxed));
    Py_DECREF (module);
    return repr;
}

static int
gboxed_init(PyGBoxed *self, PyObject *args, PyObject *kwargs)
{
    gchar buf[512];

    if (!PyArg_ParseTuple(args, ":GBoxed.__init__"))
	return -1;

    pyg_boxed_set_ptr (self, NULL);
    self->gtype = 0;
    self->free_on_dealloc = FALSE;

    g_snprintf(buf, sizeof(buf), "%s can not be constructed",
	       Py_TYPE(self)->tp_name);
    PyErr_SetString(PyExc_NotImplementedError, buf);
    return -1;
}

static void
gboxed_free(PyObject *op)
{
  PyObject_FREE(op);
}

static PyObject *
gboxed_copy(PyGBoxed *self)
{
    return pygi_gboxed_new (self->gtype, pyg_boxed_get_ptr (self), TRUE, TRUE);
}

static PyMethodDef pygboxed_methods[] = {
    { "copy", (PyCFunction) gboxed_copy, METH_NOARGS },
    { NULL, NULL, 0 }
};


/**
 * pygi_register_gboxed:
 * @dict: the module dictionary to store the wrapper class.
 * @class_name: the Python name for the wrapper class.
 * @boxed_type: the GType of the boxed type being wrapped.
 * @type: the wrapper class.
 *
 * Registers a wrapper for a boxed type.  The wrapper class will be a
 * subclass of gobject.GBoxed, and a reference to the wrapper class
 * will be stored in the provided module dictionary.
 */
void
pygi_register_gboxed (PyObject *dict, const gchar *class_name,
                      GType boxed_type, PyTypeObject *type)
{
    PyObject *o;

    g_return_if_fail(dict != NULL);
    g_return_if_fail(class_name != NULL);
    g_return_if_fail(boxed_type != 0);

    if (!type->tp_dealloc)  type->tp_dealloc  = (destructor)gboxed_dealloc;

    Py_TYPE(type) = &PyType_Type;
    g_assert (Py_TYPE (&PyGBoxed_Type) != NULL);
    type->tp_base = &PyGBoxed_Type;

    if (PyType_Ready(type) < 0) {
	g_warning("could not get type `%s' ready", type->tp_name);
	return;
    }

    PyDict_SetItemString(type->tp_dict, "__gtype__",
			 o=pyg_type_wrapper_new(boxed_type));
    Py_DECREF(o);

    g_type_set_qdata(boxed_type, pygboxed_type_key, type);

    PyDict_SetItemString(dict, (char *)class_name, (PyObject *)type);
}

/**
 * pygi_gboxed_new:
 * @boxed_type: the GType of the boxed value.
 * @boxed: the boxed value.
 * @copy_boxed: whether the new boxed wrapper should hold a copy of the value.
 * @own_ref: whether the boxed wrapper should own the boxed value.
 *
 * Creates a wrapper for a boxed value.  If @copy_boxed is set to
 * True, the wrapper will hold a copy of the value, instead of the
 * value itself.  If @own_ref is True, then the value held by the
 * wrapper will be freed when the wrapper is deallocated.  If
 * @copy_boxed is True, then @own_ref must also be True.
 *
 * Returns: the boxed wrapper or %NULL and sets an exception.
 */
PyObject *
pygi_gboxed_new (GType boxed_type, gpointer boxed, gboolean copy_boxed,
                 gboolean own_ref)
{
    PyGILState_STATE state;
    PyGBoxed *self;
    PyTypeObject *tp;

    g_return_val_if_fail(boxed_type != 0, NULL);
    g_return_val_if_fail(!copy_boxed || (copy_boxed && own_ref), NULL);

    state = PyGILState_Ensure();

    if (!boxed) {
	Py_INCREF(Py_None);
	PyGILState_Release(state);
	return Py_None;
    }

    tp = g_type_get_qdata(boxed_type, pygboxed_type_key);

    if (!tp)
        tp = (PyTypeObject *)pygi_type_import_by_g_type(boxed_type);

    if (!tp)
	tp = (PyTypeObject *)&PyGBoxed_Type; /* fallback */

    if (!PyType_IsSubtype (tp, &PyGBoxed_Type)) {
        PyErr_Format (PyExc_RuntimeError, "%s isn't a GBoxed", tp->tp_name);
        PyGILState_Release (state);
        return NULL;
    }

    self = (PyGBoxed *)tp->tp_alloc(tp, 0);

    if (self == NULL) {
	PyGILState_Release(state);
        return NULL;
    }

    if (copy_boxed)
	boxed = g_boxed_copy(boxed_type, boxed);
    pyg_boxed_set_ptr (self, boxed);
    self->gtype = boxed_type;
    self->free_on_dealloc = own_ref;

    PyGILState_Release(state);
    
    return (PyObject *)self;
}

/**
 * Returns 0 on success, or -1 and sets an exception.
 */
int
pygi_gboxed_register_types(PyObject *d)
{
    pygboxed_type_key        = g_quark_from_static_string("PyGBoxed::class");

    PyGBoxed_Type.tp_dealloc = (destructor)gboxed_dealloc;
    PyGBoxed_Type.tp_richcompare = gboxed_richcompare;
    PyGBoxed_Type.tp_repr = (reprfunc)gboxed_repr;
    PyGBoxed_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
    PyGBoxed_Type.tp_methods = pygboxed_methods;
    PyGBoxed_Type.tp_init = (initproc)gboxed_init;
    PyGBoxed_Type.tp_free = (freefunc)gboxed_free;
    PyGBoxed_Type.tp_hash = (hashfunc)gboxed_hash;
    
    PYGOBJECT_REGISTER_GTYPE(d, PyGBoxed_Type, "GBoxed", G_TYPE_BOXED);

    return 0;
}