1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
6 * gimodule.c: wrapper for the gobject-introspection library.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
24 #include <glib-object.h>
27 #include "pyginterface.h"
28 #include "pygi-repository.h"
34 #include "pygi-error.h"
35 #include "pygi-foreign.h"
36 #include "pygi-resulttuple.h"
37 #include "pygi-source.h"
38 #include "pygi-ccallback.h"
39 #include "pygi-closure.h"
40 #include "pygi-type.h"
41 #include "pygi-boxed.h"
42 #include "pygi-info.h"
43 #include "pygi-struct.h"
45 #include <pyglib-python-compat.h>
47 PyObject *PyGIWarning;
48 PyObject *PyGIDeprecationWarning;
49 PyObject *_PyGIDefaultArgPlaceholder;
52 /* Defined by PYGLIB_MODULE_START */
53 extern PyObject *pyglib__gobject_module_create (void);
55 /* Returns a new flag/enum type or %NULL */
57 flags_enum_from_gtype (GType g_type,
58 PyObject * (add_func) (PyObject *, const char *,
62 GIRepository *repository;
64 const gchar *type_name;
66 repository = g_irepository_get_default ();
67 info = g_irepository_find_by_gtype (repository, g_type);
69 type_name = g_base_info_get_name (info);
70 new_type = add_func (NULL, type_name, NULL, g_type);
71 g_base_info_unref (info);
73 type_name = g_type_name (g_type);
74 new_type = add_func (NULL, type_name, NULL, g_type);
82 _wrap_pyg_enum_add (PyObject *self,
86 static char *kwlist[] = { "g_type", NULL };
90 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
92 kwlist, &PyGTypeWrapper_Type, &py_g_type)) {
96 g_type = pyg_type_from_object (py_g_type);
97 if (g_type == G_TYPE_INVALID) {
101 return flags_enum_from_gtype (g_type, pyg_enum_add);
105 _wrap_pyg_enum_register_new_gtype_and_add (PyObject *self,
109 static char *kwlist[] = { "info", NULL };
110 PyGIBaseInfo *py_info;
113 GEnumValue *g_enum_values;
115 const gchar *namespace;
116 const gchar *type_name;
120 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
121 "O:enum_add_make_new_gtype",
122 kwlist, (PyObject *)&py_info)) {
126 if (!GI_IS_ENUM_INFO (py_info->info) ||
127 g_base_info_get_type ((GIBaseInfo *) py_info->info) != GI_INFO_TYPE_ENUM) {
128 PyErr_SetString (PyExc_TypeError, "info must be an EnumInfo with info type GI_INFO_TYPE_ENUM");
132 info = (GIEnumInfo *)py_info->info;
133 n_values = g_enum_info_get_n_values (info);
135 /* The new memory is zero filled which fulfills the registration
136 * function requirement that the last item is zeroed out as a terminator.
138 g_enum_values = g_new0 (GEnumValue, n_values + 1);
140 for (i = 0; i < n_values; i++) {
141 GIValueInfo *value_info;
142 GEnumValue *enum_value;
144 const gchar *c_identifier;
146 value_info = g_enum_info_get_value (info, i);
147 name = g_base_info_get_name ((GIBaseInfo *) value_info);
148 c_identifier = g_base_info_get_attribute ((GIBaseInfo *) value_info,
151 enum_value = &g_enum_values[i];
152 enum_value->value_nick = g_strdup (name);
153 enum_value->value = g_value_info_get_value (value_info);
155 if (c_identifier == NULL) {
156 enum_value->value_name = enum_value->value_nick;
158 enum_value->value_name = g_strdup (c_identifier);
161 g_base_info_unref ((GIBaseInfo *) value_info);
164 /* Obfuscate the full_name by prefixing it with "Py" to avoid conflicts
165 * with real GTypes. See: https://bugzilla.gnome.org/show_bug.cgi?id=692515
167 namespace = g_base_info_get_namespace ((GIBaseInfo *) info);
168 type_name = g_base_info_get_name ((GIBaseInfo *) info);
169 full_name = g_strconcat ("Py", namespace, type_name, NULL);
171 /* If enum registration fails, free all the memory allocated
172 * for the values array. This needs to leak when successful
173 * as GObject keeps a reference to the data as specified in the docs.
175 g_type = g_enum_register_static (full_name, g_enum_values);
176 if (g_type == G_TYPE_INVALID) {
177 for (i = 0; i < n_values; i++) {
178 GEnumValue *enum_value = &g_enum_values[i];
180 /* Only free value_name if it is different from value_nick to avoid
181 * a double free. The pointer might have been is re-used in the case
182 * c_identifier was NULL in the above loop.
184 if (enum_value->value_name != enum_value->value_nick)
185 g_free ((gchar *) enum_value->value_name);
186 g_free ((gchar *) enum_value->value_nick);
189 PyErr_Format (PyExc_RuntimeError, "Unable to register enum '%s'", full_name);
191 g_free (g_enum_values);
197 return pyg_enum_add (NULL, type_name, NULL, g_type);
201 _wrap_pyg_flags_add (PyObject *self,
205 static char *kwlist[] = { "g_type", NULL };
209 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
211 kwlist, &PyGTypeWrapper_Type, &py_g_type)) {
215 g_type = pyg_type_from_object (py_g_type);
216 if (g_type == G_TYPE_INVALID) {
220 return flags_enum_from_gtype (g_type, pyg_flags_add);
224 _wrap_pyg_flags_register_new_gtype_and_add (PyObject *self,
228 static char *kwlist[] = { "info", NULL };
229 PyGIBaseInfo *py_info;
232 GFlagsValue *g_flags_values;
234 const gchar *namespace;
235 const gchar *type_name;
239 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
240 "O:flags_add_make_new_gtype",
241 kwlist, (PyObject *)&py_info)) {
245 if (!GI_IS_ENUM_INFO (py_info->info) ||
246 g_base_info_get_type ((GIBaseInfo *) py_info->info) != GI_INFO_TYPE_FLAGS) {
247 PyErr_SetString (PyExc_TypeError, "info must be an EnumInfo with info type GI_INFO_TYPE_FLAGS");
251 info = (GIEnumInfo *)py_info->info;
252 n_values = g_enum_info_get_n_values (info);
254 /* The new memory is zero filled which fulfills the registration
255 * function requirement that the last item is zeroed out as a terminator.
257 g_flags_values = g_new0 (GFlagsValue, n_values + 1);
259 for (i = 0; i < n_values; i++) {
260 GIValueInfo *value_info;
261 GFlagsValue *flags_value;
263 const gchar *c_identifier;
265 value_info = g_enum_info_get_value (info, i);
266 name = g_base_info_get_name ((GIBaseInfo *) value_info);
267 c_identifier = g_base_info_get_attribute ((GIBaseInfo *) value_info,
270 flags_value = &g_flags_values[i];
271 flags_value->value_nick = g_strdup (name);
272 flags_value->value = g_value_info_get_value (value_info);
274 if (c_identifier == NULL) {
275 flags_value->value_name = flags_value->value_nick;
277 flags_value->value_name = g_strdup (c_identifier);
280 g_base_info_unref ((GIBaseInfo *) value_info);
283 /* Obfuscate the full_name by prefixing it with "Py" to avoid conflicts
284 * with real GTypes. See: https://bugzilla.gnome.org/show_bug.cgi?id=692515
286 namespace = g_base_info_get_namespace ((GIBaseInfo *) info);
287 type_name = g_base_info_get_name ((GIBaseInfo *) info);
288 full_name = g_strconcat ("Py", namespace, type_name, NULL);
290 /* If enum registration fails, free all the memory allocated
291 * for the values array. This needs to leak when successful
292 * as GObject keeps a reference to the data as specified in the docs.
294 g_type = g_flags_register_static (full_name, g_flags_values);
295 if (g_type == G_TYPE_INVALID) {
296 for (i = 0; i < n_values; i++) {
297 GFlagsValue *flags_value = &g_flags_values[i];
299 /* Only free value_name if it is different from value_nick to avoid
300 * a double free. The pointer might have been is re-used in the case
301 * c_identifier was NULL in the above loop.
303 if (flags_value->value_name != flags_value->value_nick)
304 g_free ((gchar *) flags_value->value_name);
305 g_free ((gchar *) flags_value->value_nick);
308 PyErr_Format (PyExc_RuntimeError, "Unable to register flags '%s'", full_name);
310 g_free (g_flags_values);
316 return pyg_flags_add (NULL, type_name, NULL, g_type);
320 initialize_interface (GTypeInterface *iface, PyTypeObject *pytype)
322 /* pygobject prints a warning if interface_init is NULL */
326 _wrap_pyg_register_interface_info (PyObject *self, PyObject *args)
330 GInterfaceInfo *info;
332 if (!PyArg_ParseTuple (args, "O!:register_interface_info",
333 &PyGTypeWrapper_Type, &py_g_type)) {
337 g_type = pyg_type_from_object (py_g_type);
338 if (!g_type_is_a (g_type, G_TYPE_INTERFACE)) {
339 PyErr_SetString (PyExc_TypeError, "must be an interface");
343 info = g_new0 (GInterfaceInfo, 1);
344 info->interface_init = (GInterfaceInitFunc) initialize_interface;
346 pyg_register_interface_info (g_type, info);
352 find_vfunc_info (GIBaseInfo *vfunc_info,
353 GType implementor_gtype,
354 gpointer *implementor_class_ret,
355 gpointer *implementor_vtable_ret,
356 GIFieldInfo **field_info_ret)
358 GType ancestor_g_type = 0;
360 GIBaseInfo *ancestor_info;
361 GIStructInfo *struct_info;
362 gpointer implementor_class = NULL;
363 gboolean is_interface = FALSE;
365 ancestor_info = g_base_info_get_container (vfunc_info);
366 is_interface = g_base_info_get_type (ancestor_info) == GI_INFO_TYPE_INTERFACE;
368 ancestor_g_type = g_registered_type_info_get_g_type (
369 (GIRegisteredTypeInfo *) ancestor_info);
370 implementor_class = g_type_class_ref (implementor_gtype);
372 GTypeInstance *implementor_iface_class;
373 implementor_iface_class = g_type_interface_peek (implementor_class,
375 if (implementor_iface_class == NULL) {
376 g_type_class_unref (implementor_class);
377 PyErr_Format (PyExc_RuntimeError,
378 "Couldn't find GType of implementor of interface %s. "
379 "Forgot to set __gtype_name__?",
380 g_type_name (ancestor_g_type));
384 *implementor_vtable_ret = implementor_iface_class;
386 struct_info = g_interface_info_get_iface_struct ( (GIInterfaceInfo*) ancestor_info);
388 struct_info = g_object_info_get_class_struct ( (GIObjectInfo*) ancestor_info);
389 *implementor_vtable_ret = implementor_class;
392 *implementor_class_ret = implementor_class;
394 length = g_struct_info_get_n_fields (struct_info);
395 for (i = 0; i < length; i++) {
396 GIFieldInfo *field_info;
397 GITypeInfo *type_info;
399 field_info = g_struct_info_get_field (struct_info, i);
401 if (strcmp (g_base_info_get_name ( (GIBaseInfo*) field_info),
402 g_base_info_get_name ( (GIBaseInfo*) vfunc_info)) != 0) {
403 g_base_info_unref (field_info);
407 type_info = g_field_info_get_type (field_info);
408 if (g_type_info_get_tag (type_info) == GI_TYPE_TAG_INTERFACE) {
409 g_base_info_unref (type_info);
410 *field_info_ret = field_info;
414 g_base_info_unref (type_info);
415 g_base_info_unref (field_info);
418 g_base_info_unref (struct_info);
422 _wrap_pyg_hook_up_vfunc_implementation (PyObject *self, PyObject *args)
424 PyGIBaseInfo *py_info;
426 PyObject *py_function;
427 GType implementor_gtype = 0;
428 gpointer implementor_class = NULL;
429 gpointer implementor_vtable = NULL;
430 GIFieldInfo *field_info = NULL;
431 gpointer *method_ptr = NULL;
432 PyGICClosure *closure = NULL;
434 if (!PyArg_ParseTuple (args, "O!O!O:hook_up_vfunc_implementation",
435 &PyGIBaseInfo_Type, &py_info,
436 &PyGTypeWrapper_Type, &py_type,
440 implementor_gtype = pyg_type_from_object (py_type);
441 g_assert (G_TYPE_IS_CLASSED (implementor_gtype));
443 find_vfunc_info (py_info->info, implementor_gtype, &implementor_class, &implementor_vtable, &field_info);
444 if (field_info != NULL) {
445 GITypeInfo *type_info;
446 GIBaseInfo *interface_info;
447 GICallbackInfo *callback_info;
450 type_info = g_field_info_get_type (field_info);
452 interface_info = g_type_info_get_interface (type_info);
453 g_assert (g_base_info_get_type (interface_info) == GI_INFO_TYPE_CALLBACK);
455 callback_info = (GICallbackInfo*) interface_info;
456 offset = g_field_info_get_offset (field_info);
457 method_ptr = G_STRUCT_MEMBER_P (implementor_vtable, offset);
459 closure = _pygi_make_native_closure ( (GICallableInfo*) callback_info,
460 GI_SCOPE_TYPE_NOTIFIED, py_function, NULL);
462 *method_ptr = closure->closure;
464 g_base_info_unref (interface_info);
465 g_base_info_unref (type_info);
466 g_base_info_unref (field_info);
468 g_type_class_unref (implementor_class);
474 /* Not used, left around for future reference */
476 _wrap_pyg_has_vfunc_implementation (PyObject *self, PyObject *args)
478 PyGIBaseInfo *py_info;
481 gpointer implementor_class = NULL;
482 gpointer implementor_vtable = NULL;
483 GType implementor_gtype = 0;
484 GIFieldInfo *field_info = NULL;
486 if (!PyArg_ParseTuple (args, "O!O!:has_vfunc_implementation",
487 &PyGIBaseInfo_Type, &py_info,
488 &PyGTypeWrapper_Type, &py_type))
491 implementor_gtype = pyg_type_from_object (py_type);
492 g_assert (G_TYPE_IS_CLASSED (implementor_gtype));
495 find_vfunc_info (py_info->info, implementor_gtype, &implementor_class, &implementor_vtable, &field_info);
496 if (field_info != NULL) {
497 gpointer *method_ptr;
500 offset = g_field_info_get_offset (field_info);
501 method_ptr = G_STRUCT_MEMBER_P (implementor_vtable, offset);
502 if (*method_ptr != NULL) {
506 g_base_info_unref (field_info);
508 g_type_class_unref (implementor_class);
516 _wrap_pyg_variant_type_from_string (PyObject *self, PyObject *args)
520 PyObject *py_variant = NULL;
522 if (!PyArg_ParseTuple (args, "s:variant_type_from_string",
527 py_type = _pygi_type_import_by_name ("GLib", "VariantType");
529 /* Pass the string directly and force a boxed copy. This works because
530 * GVariantType is just a char pointer. */
531 py_variant = _pygi_boxed_new ( (PyTypeObject *) py_type, type_string,
532 TRUE, /* copy_boxed */
533 0); /* slice_allocated */
539 _wrap_pyg_source_new (PyObject *self, PyObject *args)
541 return pyg_source_new ();
544 #define CHUNK_SIZE 8192
547 pyg_channel_read(PyObject* self, PyObject *args, PyObject *kwargs)
550 PyObject *py_iochannel, *ret_obj = NULL;
551 gsize total_read = 0;
552 GError* error = NULL;
553 GIOStatus status = G_IO_STATUS_NORMAL;
554 GIOChannel *iochannel = NULL;
556 if (!PyArg_ParseTuple (args, "Oi:pyg_channel_read", &py_iochannel, &max_count)) {
559 if (!pyg_boxed_check (py_iochannel, G_TYPE_IO_CHANNEL)) {
560 PyErr_SetString(PyExc_TypeError, "first argument is not a GLib.IOChannel");
565 return PYGLIB_PyBytes_FromString("");
567 iochannel = pyg_boxed_get (py_iochannel, GIOChannel);
569 while (status == G_IO_STATUS_NORMAL
570 && (max_count == -1 || total_read < max_count)) {
576 buf_size = CHUNK_SIZE;
578 buf_size = max_count - total_read;
579 if (buf_size > CHUNK_SIZE)
580 buf_size = CHUNK_SIZE;
583 if ( ret_obj == NULL ) {
584 ret_obj = PYGLIB_PyBytes_FromStringAndSize((char *)NULL, buf_size);
588 else if (buf_size + total_read > PYGLIB_PyBytes_Size(ret_obj)) {
589 if (PYGLIB_PyBytes_Resize(&ret_obj, buf_size + total_read) == -1)
593 buf = PYGLIB_PyBytes_AsString(ret_obj) + total_read;
595 Py_BEGIN_ALLOW_THREADS;
596 status = g_io_channel_read_chars (iochannel, buf, buf_size, &single_read, &error);
597 Py_END_ALLOW_THREADS;
599 if (pygi_error_check (&error))
602 total_read += single_read;
605 if ( total_read != PYGLIB_PyBytes_Size(ret_obj) ) {
606 if (PYGLIB_PyBytes_Resize(&ret_obj, total_read) == -1)
617 static PyMethodDef _gi_functions[] = {
618 { "enum_add", (PyCFunction) _wrap_pyg_enum_add, METH_VARARGS | METH_KEYWORDS },
619 { "enum_register_new_gtype_and_add", (PyCFunction) _wrap_pyg_enum_register_new_gtype_and_add, METH_VARARGS | METH_KEYWORDS },
620 { "flags_add", (PyCFunction) _wrap_pyg_flags_add, METH_VARARGS | METH_KEYWORDS },
621 { "flags_register_new_gtype_and_add", (PyCFunction) _wrap_pyg_flags_register_new_gtype_and_add, METH_VARARGS | METH_KEYWORDS },
623 { "register_interface_info", (PyCFunction) _wrap_pyg_register_interface_info, METH_VARARGS },
624 { "hook_up_vfunc_implementation", (PyCFunction) _wrap_pyg_hook_up_vfunc_implementation, METH_VARARGS },
625 { "variant_type_from_string", (PyCFunction) _wrap_pyg_variant_type_from_string, METH_VARARGS },
626 { "source_new", (PyCFunction) _wrap_pyg_source_new, METH_NOARGS },
627 { "source_set_callback", (PyCFunction) pyg_source_set_callback, METH_VARARGS },
628 { "io_channel_read", (PyCFunction) pyg_channel_read, METH_VARARGS },
629 { "require_foreign", (PyCFunction) pygi_require_foreign, METH_VARARGS | METH_KEYWORDS },
633 static struct PyGI_API CAPI = {
634 pygi_register_foreign_struct,
637 PYGLIB_MODULE_START(_gi, "_gi")
640 PyObject *_glib_module;
641 PyObject *_gobject_module;
643 /* Always enable Python threads since we cannot predict which GI repositories
644 * might accept Python callbacks run within non-Python threads or might trigger
645 * toggle ref notifications.
646 * See: https://bugzilla.gnome.org/show_bug.cgi?id=709223
648 PyEval_InitThreads ();
650 _glib_module = pyglib__glib_module_create ();
651 if (_glib_module == NULL) {
652 return PYGLIB_MODULE_ERROR_RETURN;
654 /* In Python 2.x, pyglib_..._module_create returns a borrowed reference and
655 * PyModule_AddObject steals a reference. Ensure we don't share a reference
656 * between sys.modules and gi._gi._glib by incrementing the ref count here.
657 * Note that we don't add this to the PYGLIB_MODULE_START macro because that
658 * would cause a leak for the main module gi._gi */
659 if (PY_MAJOR_VERSION < 3) {
660 Py_INCREF (_glib_module);
662 PyModule_AddObject (module, "_glib", _glib_module);
663 PyModule_AddStringConstant(module, "__package__", "gi._gi");
665 _gobject_module = pyglib__gobject_module_create ();
666 if (_gobject_module == NULL) {
667 return PYGLIB_MODULE_ERROR_RETURN;
669 if (PY_MAJOR_VERSION < 3) {
670 Py_INCREF (_gobject_module);
672 PyModule_AddObject (module, "_gobject", _gobject_module);
673 PyModule_AddStringConstant(module, "__package__", "gi._gi");
675 pygi_foreign_init ();
676 pygi_error_register_types (module);
677 _pygi_repository_register_types (module);
678 _pygi_info_register_types (module);
679 _pygi_struct_register_types (module);
680 _pygi_boxed_register_types (module);
681 _pygi_ccallback_register_types (module);
682 pygi_resulttuple_register_types (module);
684 PyGIWarning = PyErr_NewException ("gi.PyGIWarning", PyExc_Warning, NULL);
686 /* Use RuntimeWarning as the base class of PyGIDeprecationWarning
687 * for unstable (odd minor version) and use DeprecationWarning for
688 * stable (even minor version). This is so PyGObject deprecations
689 * behave the same as regular Python deprecations in stable releases.
691 #if PYGOBJECT_MINOR_VERSION % 2
692 PyGIDeprecationWarning = PyErr_NewException("gi.PyGIDeprecationWarning",
693 PyExc_RuntimeWarning, NULL);
695 PyGIDeprecationWarning = PyErr_NewException("gi.PyGIDeprecationWarning",
696 PyExc_DeprecationWarning, NULL);
699 /* Place holder object used to fill in "from Python" argument lists
700 * for values not supplied by the caller but support a GI default.
702 _PyGIDefaultArgPlaceholder = PyObject_New(PyObject, &PyType_Type);
704 Py_INCREF (PyGIWarning);
705 PyModule_AddObject (module, "PyGIWarning", PyGIWarning);
707 Py_INCREF(PyGIDeprecationWarning);
708 PyModule_AddObject(module, "PyGIDeprecationWarning", PyGIDeprecationWarning);
710 api = PYGLIB_CPointer_WrapPointer ( (void *) &CAPI, "gi._API");
712 return PYGLIB_MODULE_ERROR_RETURN;
714 PyModule_AddObject (module, "_API", api);