| 1 |
/* -*- Mode: C; c-basic-offset: 4 -*- |
| 2 |
* pygtk- Python bindings for the GTK toolkit. |
| 3 |
* Copyright (C) 1998-2003 James Henstridge |
| 4 |
* |
| 5 |
* pygboxed.c: wrapper for GBoxed |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "pygi-type.h" |
| 22 |
#include "pygi-util.h" |
| 23 |
#include "pygboxed.h" |
| 24 |
|
| 25 |
GQuark pygboxed_type_key; |
| 26 |
|
| 27 |
PYGI_DEFINE_TYPE ("gobject.GBoxed", PyGBoxed_Type, PyGBoxed); |
| 28 |
|
| 29 |
static void |
| 30 |
gboxed_dealloc (PyGBoxed *self) |
| 31 |
{ |
| 32 |
if (self->free_on_dealloc && pyg_boxed_get_ptr (self)) { |
| 33 |
PyGILState_STATE state = PyGILState_Ensure (); |
| 34 |
g_boxed_free (self->gtype, pyg_boxed_get_ptr (self)); |
| 35 |
PyGILState_Release (state); |
| 36 |
} |
| 37 |
|
| 38 |
Py_TYPE (self)->tp_free ((PyObject *)self); |
| 39 |
} |
| 40 |
|
| 41 |
static PyObject * |
| 42 |
gboxed_richcompare (PyObject *self, PyObject *other, int op) |
| 43 |
{ |
| 44 |
if (Py_TYPE (self) == Py_TYPE (other) |
| 45 |
&& PyObject_IsInstance (self, (PyObject *)&PyGBoxed_Type)) |
| 46 |
return pyg_ptr_richcompare (pyg_boxed_get_ptr (self), |
| 47 |
pyg_boxed_get_ptr (other), op); |
| 48 |
else { |
| 49 |
Py_RETURN_NOTIMPLEMENTED; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static Py_hash_t |
| 54 |
gboxed_hash (PyGBoxed *self) |
| 55 |
{ |
| 56 |
return (Py_hash_t)(gintptr)(pyg_boxed_get_ptr (self)); |
| 57 |
} |
| 58 |
|
| 59 |
static PyObject * |
| 60 |
gboxed_repr (PyGBoxed *boxed) |
| 61 |
{ |
| 62 |
PyObject *module, *repr, *self = (PyObject *)boxed; |
| 63 |
gchar *module_str, *namespace; |
| 64 |
|
| 65 |
module = PyObject_GetAttrString (self, "__module__"); |
| 66 |
if (module == NULL) return NULL; |
| 67 |
|
| 68 |
if (!PyUnicode_Check (module)) { |
| 69 |
Py_DECREF (module); |
| 70 |
return NULL; |
| 71 |
} |
| 72 |
|
| 73 |
module_str = PyUnicode_AsUTF8 (module); |
| 74 |
namespace = g_strrstr (module_str, "."); |
| 75 |
if (namespace == NULL) { |
| 76 |
namespace = module_str; |
| 77 |
} else { |
| 78 |
namespace += 1; |
| 79 |
} |
| 80 |
|
| 81 |
repr = PyUnicode_FromFormat ( |
| 82 |
"<%s.%s object at %p (%s at %p)>", namespace, Py_TYPE (self)->tp_name, |
| 83 |
self, g_type_name (boxed->gtype), pyg_boxed_get_ptr (boxed)); |
| 84 |
Py_DECREF (module); |
| 85 |
return repr; |
| 86 |
} |
| 87 |
|
| 88 |
static int |
| 89 |
gboxed_init (PyGBoxed *self, PyObject *args, PyObject *kwargs) |
| 90 |
{ |
| 91 |
gchar buf[512]; |
| 92 |
|
| 93 |
if (!PyArg_ParseTuple (args, ":GBoxed.__init__")) return -1; |
| 94 |
|
| 95 |
pyg_boxed_set_ptr (self, NULL); |
| 96 |
self->gtype = 0; |
| 97 |
self->free_on_dealloc = FALSE; |
| 98 |
|
| 99 |
g_snprintf (buf, sizeof (buf), "%s can not be constructed", |
| 100 |
Py_TYPE (self)->tp_name); |
| 101 |
PyErr_SetString (PyExc_NotImplementedError, buf); |
| 102 |
return -1; |
| 103 |
} |
| 104 |
|
| 105 |
static void |
| 106 |
gboxed_free (PyObject *op) |
| 107 |
{ |
| 108 |
PyObject_Free (op); |
| 109 |
} |
| 110 |
|
| 111 |
static PyObject * |
| 112 |
gboxed_copy (PyGBoxed *self) |
| 113 |
{ |
| 114 |
return pygi_gboxed_new (self->gtype, pyg_boxed_get_ptr (self), TRUE, TRUE); |
| 115 |
} |
| 116 |
|
| 117 |
static PyMethodDef pygboxed_methods[] = { |
| 118 |
{ "copy", (PyCFunction)gboxed_copy, METH_NOARGS }, |
| 119 |
{ NULL, NULL, 0 }, |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* pygi_register_gboxed: |
| 125 |
* @dict: the module dictionary to store the wrapper class. |
| 126 |
* @class_name: the Python name for the wrapper class. |
| 127 |
* @boxed_type: the GType of the boxed type being wrapped. |
| 128 |
* @type: the wrapper class. |
| 129 |
* |
| 130 |
* Registers a wrapper for a boxed type. The wrapper class will be a |
| 131 |
* subclass of gobject.GBoxed, and a reference to the wrapper class |
| 132 |
* will be stored in the provided module dictionary. |
| 133 |
*/ |
| 134 |
void |
| 135 |
pygi_register_gboxed (PyObject *dict, const gchar *class_name, |
| 136 |
GType boxed_type, PyTypeObject *type) |
| 137 |
{ |
| 138 |
PyObject *o; |
| 139 |
|
| 140 |
g_return_if_fail (dict != NULL); |
| 141 |
g_return_if_fail (class_name != NULL); |
| 142 |
g_return_if_fail (boxed_type != 0); |
| 143 |
|
| 144 |
if (!type->tp_dealloc) type->tp_dealloc = (destructor)gboxed_dealloc; |
| 145 |
|
| 146 |
Py_SET_TYPE (type, &PyType_Type); |
| 147 |
g_assert (Py_TYPE (&PyGBoxed_Type) != NULL); |
| 148 |
type->tp_base = &PyGBoxed_Type; |
| 149 |
|
| 150 |
if (PyType_Ready (type) < 0) { |
| 151 |
g_warning ("could not get type `%s' ready", type->tp_name); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
PyDict_SetItemString (type->tp_dict, "__gtype__", |
| 156 |
o = pyg_type_wrapper_new (boxed_type)); |
| 157 |
Py_DECREF (o); |
| 158 |
|
| 159 |
g_type_set_qdata (boxed_type, pygboxed_type_key, type); |
| 160 |
|
| 161 |
PyDict_SetItemString (dict, (char *)class_name, (PyObject *)type); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* pygi_gboxed_new: |
| 166 |
* @boxed_type: the GType of the boxed value. |
| 167 |
* @boxed: the boxed value. |
| 168 |
* @copy_boxed: whether the new boxed wrapper should hold a copy of the value. |
| 169 |
* @own_ref: whether the boxed wrapper should own the boxed value. |
| 170 |
* |
| 171 |
* Creates a wrapper for a boxed value. If @copy_boxed is set to |
| 172 |
* True, the wrapper will hold a copy of the value, instead of the |
| 173 |
* value itself. If @own_ref is True, then the value held by the |
| 174 |
* wrapper will be freed when the wrapper is deallocated. If |
| 175 |
* @copy_boxed is True, then @own_ref must also be True. |
| 176 |
* |
| 177 |
* Returns: the boxed wrapper or %NULL and sets an exception. |
| 178 |
*/ |
| 179 |
PyObject * |
| 180 |
pygi_gboxed_new (GType boxed_type, gpointer boxed, gboolean copy_boxed, |
| 181 |
gboolean own_ref) |
| 182 |
{ |
| 183 |
PyGILState_STATE state; |
| 184 |
PyGBoxed *self; |
| 185 |
PyTypeObject *tp; |
| 186 |
|
| 187 |
g_return_val_if_fail (boxed_type != 0, NULL); |
| 188 |
g_return_val_if_fail (!copy_boxed || (copy_boxed && own_ref), NULL); |
| 189 |
|
| 190 |
state = PyGILState_Ensure (); |
| 191 |
|
| 192 |
if (!boxed) { |
| 193 |
Py_INCREF (Py_None); |
| 194 |
PyGILState_Release (state); |
| 195 |
return Py_None; |
| 196 |
} |
| 197 |
|
| 198 |
tp = g_type_get_qdata (boxed_type, pygboxed_type_key); |
| 199 |
|
| 200 |
if (!tp) tp = (PyTypeObject *)pygi_type_import_by_g_type (boxed_type); |
| 201 |
|
| 202 |
if (!tp) tp = (PyTypeObject *)&PyGBoxed_Type; /* fallback */ |
| 203 |
|
| 204 |
if (!PyType_IsSubtype (tp, &PyGBoxed_Type)) { |
| 205 |
PyErr_Format (PyExc_RuntimeError, "%s isn't a GBoxed", tp->tp_name); |
| 206 |
PyGILState_Release (state); |
| 207 |
return NULL; |
| 208 |
} |
| 209 |
|
| 210 |
self = (PyGBoxed *)tp->tp_alloc (tp, 0); |
| 211 |
|
| 212 |
if (self == NULL) { |
| 213 |
PyGILState_Release (state); |
| 214 |
return NULL; |
| 215 |
} |
| 216 |
|
| 217 |
if (copy_boxed) boxed = g_boxed_copy (boxed_type, boxed); |
| 218 |
pyg_boxed_set_ptr (self, boxed); |
| 219 |
self->gtype = boxed_type; |
| 220 |
self->free_on_dealloc = own_ref; |
| 221 |
|
| 222 |
PyGILState_Release (state); |
| 223 |
|
| 224 |
return (PyObject *)self; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Returns 0 on success, or -1 and sets an exception. |
| 229 |
*/ |
| 230 |
int |
| 231 |
pygi_gboxed_register_types (PyObject *d) |
| 232 |
{ |
| 233 |
PyObject *pygtype; |
| 234 |
|
| 235 |
pygboxed_type_key = g_quark_from_static_string ("PyGBoxed::class"); |
| 236 |
|
| 237 |
PyGBoxed_Type.tp_dealloc = (destructor)gboxed_dealloc; |
| 238 |
PyGBoxed_Type.tp_richcompare = gboxed_richcompare; |
| 239 |
PyGBoxed_Type.tp_repr = (reprfunc)gboxed_repr; |
| 240 |
PyGBoxed_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; |
| 241 |
PyGBoxed_Type.tp_methods = pygboxed_methods; |
| 242 |
PyGBoxed_Type.tp_init = (initproc)gboxed_init; |
| 243 |
PyGBoxed_Type.tp_free = (freefunc)gboxed_free; |
| 244 |
PyGBoxed_Type.tp_hash = (hashfunc)gboxed_hash; |
| 245 |
PyGBoxed_Type.tp_alloc = PyType_GenericAlloc; |
| 246 |
PyGBoxed_Type.tp_new = PyType_GenericNew; |
| 247 |
if (PyType_Ready (&PyGBoxed_Type)) return -1; |
| 248 |
|
| 249 |
pygtype = pyg_type_wrapper_new (G_TYPE_POINTER); |
| 250 |
PyDict_SetItemString (PyGBoxed_Type.tp_dict, "__gtype__", pygtype); |
| 251 |
Py_DECREF (pygtype); |
| 252 |
|
| 253 |
PyDict_SetItemString (d, "GBoxed", (PyObject *)&PyGBoxed_Type); |
| 254 |
|
| 255 |
return 0; |
| 256 |
} |