1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 1998-2003 James Henstridge
5 * pygpointer.c: wrapper for GPointer
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.
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.
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/>.
24 #include <glib-object.h>
25 #include "pygpointer.h"
26 #include "pygi-type.h"
27 #include "pygi-type.h"
28 #include "pygi-util.h"
31 GQuark pygpointer_class_key
;
33 PYGLIB_DEFINE_TYPE("gobject.GPointer", PyGPointer_Type
, PyGPointer
);
36 pyg_pointer_dealloc(PyGPointer
*self
)
38 Py_TYPE(self
)->tp_free((PyObject
*)self
);
42 pyg_pointer_richcompare(PyObject
*self
, PyObject
*other
, int op
)
44 if (Py_TYPE(self
) == Py_TYPE(other
))
45 return pyg_ptr_richcompare (pyg_pointer_get_ptr (self
),
46 pyg_pointer_get_ptr (other
),
49 Py_INCREF(Py_NotImplemented
);
50 return Py_NotImplemented
;
54 static PYGLIB_Py_hash_t
55 pyg_pointer_hash(PyGPointer
*self
)
57 return PYGLIB_Py_hash_t_FromVoidPtr (pyg_pointer_get_ptr (self
));
61 pyg_pointer_repr(PyGPointer
*self
)
65 g_snprintf(buf
, sizeof(buf
), "<%s at 0x%" G_GUINTPTR_FORMAT
">",
66 g_type_name(self
->gtype
),
67 (guintptr
)pyg_pointer_get_ptr (self
));
68 return PYGLIB_PyUnicode_FromString(buf
);
72 pyg_pointer_init(PyGPointer
*self
, PyObject
*args
, PyObject
*kwargs
)
76 if (!PyArg_ParseTuple(args
, ":GPointer.__init__"))
79 pyg_pointer_set_ptr (self
, NULL
);
82 g_snprintf(buf
, sizeof(buf
), "%s can not be constructed",
83 Py_TYPE(self
)->tp_name
);
84 PyErr_SetString(PyExc_NotImplementedError
, buf
);
89 pyg_pointer_free(PyObject
*op
)
95 * pyg_register_pointer:
96 * @dict: the module dictionary to store the wrapper class.
97 * @class_name: the Python name for the wrapper class.
98 * @pointer_type: the GType of the pointer type being wrapped.
99 * @type: the wrapper class.
101 * Registers a wrapper for a pointer type. The wrapper class will be
102 * a subclass of gobject.GPointer, and a reference to the wrapper
103 * class will be stored in the provided module dictionary.
106 pyg_register_pointer(PyObject
*dict
, const gchar
*class_name
,
107 GType pointer_type
, PyTypeObject
*type
)
111 g_return_if_fail(dict
!= NULL
);
112 g_return_if_fail(class_name
!= NULL
);
113 g_return_if_fail(pointer_type
!= 0);
115 if (!type
->tp_dealloc
) type
->tp_dealloc
= (destructor
)pyg_pointer_dealloc
;
117 Py_TYPE(type
) = &PyType_Type
;
118 g_assert (Py_TYPE (&PyGPointer_Type
) != NULL
);
119 type
->tp_base
= &PyGPointer_Type
;
121 if (PyType_Ready(type
) < 0) {
122 g_warning("could not get type `%s' ready", type
->tp_name
);
126 PyDict_SetItemString(type
->tp_dict
, "__gtype__",
127 o
=pyg_type_wrapper_new(pointer_type
));
130 g_type_set_qdata(pointer_type
, pygpointer_class_key
, type
);
132 PyDict_SetItemString(dict
, (char *)class_name
, (PyObject
*)type
);
137 * @pointer_type: the GType of the pointer value.
138 * @pointer: the pointer value.
140 * Creates a wrapper for a pointer value. Since G_TYPE_POINTER types
141 * don't register any information about how to copy/free them, there
142 * is no guarantee that the pointer will remain valid, and there is
143 * nothing registered to release the pointer when the pointer goes out
144 * of scope. This is why we don't recommend people use these types.
146 * Returns: the boxed wrapper.
149 pyg_pointer_new(GType pointer_type
, gpointer pointer
)
151 PyGILState_STATE state
;
154 g_return_val_if_fail(pointer_type
!= 0, NULL
);
156 state
= PyGILState_Ensure();
160 PyGILState_Release(state
);
164 tp
= g_type_get_qdata(pointer_type
, pygpointer_class_key
);
167 tp
= (PyTypeObject
*)pygi_type_import_by_g_type(pointer_type
);
170 tp
= (PyTypeObject
*)&PyGPointer_Type
; /* fallback */
171 self
= PyObject_NEW(PyGPointer
, tp
);
173 PyGILState_Release(state
);
178 pyg_pointer_set_ptr (self
, pointer
);
179 self
->gtype
= pointer_type
;
181 return (PyObject
*)self
;
185 * Returns 0 on success, or -1 and sets an exception.
188 pygi_pointer_register_types(PyObject
*d
)
190 pygpointer_class_key
= g_quark_from_static_string("PyGPointer::class");
192 PyGPointer_Type
.tp_dealloc
= (destructor
)pyg_pointer_dealloc
;
193 PyGPointer_Type
.tp_richcompare
= pyg_pointer_richcompare
;
194 PyGPointer_Type
.tp_repr
= (reprfunc
)pyg_pointer_repr
;
195 PyGPointer_Type
.tp_hash
= (hashfunc
)pyg_pointer_hash
;
196 PyGPointer_Type
.tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
;
197 PyGPointer_Type
.tp_init
= (initproc
)pyg_pointer_init
;
198 PyGPointer_Type
.tp_free
= (freefunc
)pyg_pointer_free
;
199 PYGOBJECT_REGISTER_GTYPE(d
, PyGPointer_Type
, "GPointer", G_TYPE_POINTER
);