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
25 #include <glib-object.h>
29 #include "pyginterface.h"
30 #include "pygi-repository.h"
36 #include "pygi-error.h"
37 #include "pygi-foreign.h"
38 #include "pygi-resulttuple.h"
39 #include "pygi-source.h"
40 #include "pygi-ccallback.h"
41 #include "pygi-closure.h"
42 #include "pygi-type.h"
43 #include "pygi-boxed.h"
44 #include "pygi-info.h"
45 #include "pygi-struct.h"
47 #include <pyglib-python-compat.h>
49 PyObject
*PyGIWarning
;
50 PyObject
*PyGIDeprecationWarning
;
51 PyObject
*_PyGIDefaultArgPlaceholder
;
54 /* Defined by PYGLIB_MODULE_START */
55 extern PyObject
*pyglib__gobject_module_create (void);
57 /* Returns a new flag/enum type or %NULL */
59 flags_enum_from_gtype (GType g_type
,
60 PyObject
* (add_func
) (PyObject
*, const char *,
64 GIRepository
*repository
;
66 const gchar
*type_name
;
68 repository
= g_irepository_get_default ();
69 info
= g_irepository_find_by_gtype (repository
, g_type
);
71 type_name
= g_base_info_get_name (info
);
72 new_type
= add_func (NULL
, type_name
, NULL
, g_type
);
73 g_base_info_unref (info
);
75 type_name
= g_type_name (g_type
);
76 new_type
= add_func (NULL
, type_name
, NULL
, g_type
);
84 _wrap_pyg_enum_add (PyObject
*self
,
88 static char *kwlist
[] = { "g_type", NULL
};
92 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
,
94 kwlist
, &PyGTypeWrapper_Type
, &py_g_type
)) {
98 g_type
= pyg_type_from_object (py_g_type
);
99 if (g_type
== G_TYPE_INVALID
) {
103 return flags_enum_from_gtype (g_type
, pyg_enum_add
);
107 _wrap_pyg_enum_register_new_gtype_and_add (PyObject
*self
,
111 static char *kwlist
[] = { "info", NULL
};
112 PyGIBaseInfo
*py_info
;
115 GEnumValue
*g_enum_values
;
117 const gchar
*namespace;
118 const gchar
*type_name
;
122 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
,
123 "O:enum_add_make_new_gtype",
124 kwlist
, (PyObject
*)&py_info
)) {
128 if (!GI_IS_ENUM_INFO (py_info
->info
) ||
129 g_base_info_get_type ((GIBaseInfo
*) py_info
->info
) != GI_INFO_TYPE_ENUM
) {
130 PyErr_SetString (PyExc_TypeError
, "info must be an EnumInfo with info type GI_INFO_TYPE_ENUM");
134 info
= (GIEnumInfo
*)py_info
->info
;
135 n_values
= g_enum_info_get_n_values (info
);
137 /* The new memory is zero filled which fulfills the registration
138 * function requirement that the last item is zeroed out as a terminator.
140 g_enum_values
= g_new0 (GEnumValue
, n_values
+ 1);
142 for (i
= 0; i
< n_values
; i
++) {
143 GIValueInfo
*value_info
;
144 GEnumValue
*enum_value
;
146 const gchar
*c_identifier
;
148 value_info
= g_enum_info_get_value (info
, i
);
149 name
= g_base_info_get_name ((GIBaseInfo
*) value_info
);
150 c_identifier
= g_base_info_get_attribute ((GIBaseInfo
*) value_info
,
153 enum_value
= &g_enum_values
[i
];
154 enum_value
->value_nick
= g_strdup (name
);
155 enum_value
->value
= g_value_info_get_value (value_info
);
157 if (c_identifier
== NULL
) {
158 enum_value
->value_name
= enum_value
->value_nick
;
160 enum_value
->value_name
= g_strdup (c_identifier
);
163 g_base_info_unref ((GIBaseInfo
*) value_info
);
166 /* Obfuscate the full_name by prefixing it with "Py" to avoid conflicts
167 * with real GTypes. See: https://bugzilla.gnome.org/show_bug.cgi?id=692515
169 namespace = g_base_info_get_namespace ((GIBaseInfo
*) info
);
170 type_name
= g_base_info_get_name ((GIBaseInfo
*) info
);
171 full_name
= g_strconcat ("Py", namespace, type_name
, NULL
);
173 /* If enum registration fails, free all the memory allocated
174 * for the values array. This needs to leak when successful
175 * as GObject keeps a reference to the data as specified in the docs.
177 g_type
= g_enum_register_static (full_name
, g_enum_values
);
178 if (g_type
== G_TYPE_INVALID
) {
179 for (i
= 0; i
< n_values
; i
++) {
180 GEnumValue
*enum_value
= &g_enum_values
[i
];
182 /* Only free value_name if it is different from value_nick to avoid
183 * a double free. The pointer might have been is re-used in the case
184 * c_identifier was NULL in the above loop.
186 if (enum_value
->value_name
!= enum_value
->value_nick
)
187 g_free ((gchar
*) enum_value
->value_name
);
188 g_free ((gchar
*) enum_value
->value_nick
);
191 PyErr_Format (PyExc_RuntimeError
, "Unable to register enum '%s'", full_name
);
193 g_free (g_enum_values
);
199 return pyg_enum_add (NULL
, type_name
, NULL
, g_type
);
203 _wrap_pyg_flags_add (PyObject
*self
,
207 static char *kwlist
[] = { "g_type", NULL
};
211 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
,
213 kwlist
, &PyGTypeWrapper_Type
, &py_g_type
)) {
217 g_type
= pyg_type_from_object (py_g_type
);
218 if (g_type
== G_TYPE_INVALID
) {
222 return flags_enum_from_gtype (g_type
, pyg_flags_add
);
226 _wrap_pyg_flags_register_new_gtype_and_add (PyObject
*self
,
230 static char *kwlist
[] = { "info", NULL
};
231 PyGIBaseInfo
*py_info
;
234 GFlagsValue
*g_flags_values
;
236 const gchar
*namespace;
237 const gchar
*type_name
;
241 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
,
242 "O:flags_add_make_new_gtype",
243 kwlist
, (PyObject
*)&py_info
)) {
247 if (!GI_IS_ENUM_INFO (py_info
->info
) ||
248 g_base_info_get_type ((GIBaseInfo
*) py_info
->info
) != GI_INFO_TYPE_FLAGS
) {
249 PyErr_SetString (PyExc_TypeError
, "info must be an EnumInfo with info type GI_INFO_TYPE_FLAGS");
253 info
= (GIEnumInfo
*)py_info
->info
;
254 n_values
= g_enum_info_get_n_values (info
);
256 /* The new memory is zero filled which fulfills the registration
257 * function requirement that the last item is zeroed out as a terminator.
259 g_flags_values
= g_new0 (GFlagsValue
, n_values
+ 1);
261 for (i
= 0; i
< n_values
; i
++) {
262 GIValueInfo
*value_info
;
263 GFlagsValue
*flags_value
;
265 const gchar
*c_identifier
;
267 value_info
= g_enum_info_get_value (info
, i
);
268 name
= g_base_info_get_name ((GIBaseInfo
*) value_info
);
269 c_identifier
= g_base_info_get_attribute ((GIBaseInfo
*) value_info
,
272 flags_value
= &g_flags_values
[i
];
273 flags_value
->value_nick
= g_strdup (name
);
274 flags_value
->value
= g_value_info_get_value (value_info
);
276 if (c_identifier
== NULL
) {
277 flags_value
->value_name
= flags_value
->value_nick
;
279 flags_value
->value_name
= g_strdup (c_identifier
);
282 g_base_info_unref ((GIBaseInfo
*) value_info
);
285 /* Obfuscate the full_name by prefixing it with "Py" to avoid conflicts
286 * with real GTypes. See: https://bugzilla.gnome.org/show_bug.cgi?id=692515
288 namespace = g_base_info_get_namespace ((GIBaseInfo
*) info
);
289 type_name
= g_base_info_get_name ((GIBaseInfo
*) info
);
290 full_name
= g_strconcat ("Py", namespace, type_name
, NULL
);
292 /* If enum registration fails, free all the memory allocated
293 * for the values array. This needs to leak when successful
294 * as GObject keeps a reference to the data as specified in the docs.
296 g_type
= g_flags_register_static (full_name
, g_flags_values
);
297 if (g_type
== G_TYPE_INVALID
) {
298 for (i
= 0; i
< n_values
; i
++) {
299 GFlagsValue
*flags_value
= &g_flags_values
[i
];
301 /* Only free value_name if it is different from value_nick to avoid
302 * a double free. The pointer might have been is re-used in the case
303 * c_identifier was NULL in the above loop.
305 if (flags_value
->value_name
!= flags_value
->value_nick
)
306 g_free ((gchar
*) flags_value
->value_name
);
307 g_free ((gchar
*) flags_value
->value_nick
);
310 PyErr_Format (PyExc_RuntimeError
, "Unable to register flags '%s'", full_name
);
312 g_free (g_flags_values
);
318 return pyg_flags_add (NULL
, type_name
, NULL
, g_type
);
322 initialize_interface (GTypeInterface
*iface
, PyTypeObject
*pytype
)
324 /* pygobject prints a warning if interface_init is NULL */
328 _wrap_pyg_register_interface_info (PyObject
*self
, PyObject
*args
)
332 GInterfaceInfo
*info
;
334 if (!PyArg_ParseTuple (args
, "O!:register_interface_info",
335 &PyGTypeWrapper_Type
, &py_g_type
)) {
339 g_type
= pyg_type_from_object (py_g_type
);
340 if (!g_type_is_a (g_type
, G_TYPE_INTERFACE
)) {
341 PyErr_SetString (PyExc_TypeError
, "must be an interface");
345 info
= g_new0 (GInterfaceInfo
, 1);
346 info
->interface_init
= (GInterfaceInitFunc
) initialize_interface
;
348 pyg_register_interface_info (g_type
, info
);
354 find_vfunc_info (GIBaseInfo
*vfunc_info
,
355 GType implementor_gtype
,
356 gpointer
*implementor_class_ret
,
357 gpointer
*implementor_vtable_ret
,
358 GIFieldInfo
**field_info_ret
)
360 GType ancestor_g_type
= 0;
362 GIBaseInfo
*ancestor_info
;
363 GIStructInfo
*struct_info
;
364 gpointer implementor_class
= NULL
;
365 gboolean is_interface
= FALSE
;
367 ancestor_info
= g_base_info_get_container (vfunc_info
);
368 is_interface
= g_base_info_get_type (ancestor_info
) == GI_INFO_TYPE_INTERFACE
;
370 ancestor_g_type
= g_registered_type_info_get_g_type (
371 (GIRegisteredTypeInfo
*) ancestor_info
);
372 implementor_class
= g_type_class_ref (implementor_gtype
);
374 GTypeInstance
*implementor_iface_class
;
375 implementor_iface_class
= g_type_interface_peek (implementor_class
,
377 if (implementor_iface_class
== NULL
) {
378 g_type_class_unref (implementor_class
);
379 PyErr_Format (PyExc_RuntimeError
,
380 "Couldn't find GType of implementor of interface %s. "
381 "Forgot to set __gtype_name__?",
382 g_type_name (ancestor_g_type
));
386 *implementor_vtable_ret
= implementor_iface_class
;
388 struct_info
= g_interface_info_get_iface_struct ( (GIInterfaceInfo
*) ancestor_info
);
390 struct_info
= g_object_info_get_class_struct ( (GIObjectInfo
*) ancestor_info
);
391 *implementor_vtable_ret
= implementor_class
;
394 *implementor_class_ret
= implementor_class
;
396 length
= g_struct_info_get_n_fields (struct_info
);
397 for (i
= 0; i
< length
; i
++) {
398 GIFieldInfo
*field_info
;
399 GITypeInfo
*type_info
;
401 field_info
= g_struct_info_get_field (struct_info
, i
);
403 if (strcmp (g_base_info_get_name ( (GIBaseInfo
*) field_info
),
404 g_base_info_get_name ( (GIBaseInfo
*) vfunc_info
)) != 0) {
405 g_base_info_unref (field_info
);
409 type_info
= g_field_info_get_type (field_info
);
410 if (g_type_info_get_tag (type_info
) == GI_TYPE_TAG_INTERFACE
) {
411 g_base_info_unref (type_info
);
412 *field_info_ret
= field_info
;
416 g_base_info_unref (type_info
);
417 g_base_info_unref (field_info
);
420 g_base_info_unref (struct_info
);
424 _wrap_pyg_hook_up_vfunc_implementation (PyObject
*self
, PyObject
*args
)
426 PyGIBaseInfo
*py_info
;
428 PyObject
*py_function
;
429 GType implementor_gtype
= 0;
430 gpointer implementor_class
= NULL
;
431 gpointer implementor_vtable
= NULL
;
432 GIFieldInfo
*field_info
= NULL
;
433 gpointer
*method_ptr
= NULL
;
434 PyGICClosure
*closure
= NULL
;
436 if (!PyArg_ParseTuple (args
, "O!O!O:hook_up_vfunc_implementation",
437 &PyGIBaseInfo_Type
, &py_info
,
438 &PyGTypeWrapper_Type
, &py_type
,
442 implementor_gtype
= pyg_type_from_object (py_type
);
443 g_assert (G_TYPE_IS_CLASSED (implementor_gtype
));
445 find_vfunc_info (py_info
->info
, implementor_gtype
, &implementor_class
, &implementor_vtable
, &field_info
);
446 if (field_info
!= NULL
) {
447 GITypeInfo
*type_info
;
448 GIBaseInfo
*interface_info
;
449 GICallbackInfo
*callback_info
;
452 type_info
= g_field_info_get_type (field_info
);
454 interface_info
= g_type_info_get_interface (type_info
);
455 g_assert (g_base_info_get_type (interface_info
) == GI_INFO_TYPE_CALLBACK
);
457 callback_info
= (GICallbackInfo
*) interface_info
;
458 offset
= g_field_info_get_offset (field_info
);
459 method_ptr
= G_STRUCT_MEMBER_P (implementor_vtable
, offset
);
461 closure
= _pygi_make_native_closure ( (GICallableInfo
*) callback_info
,
462 GI_SCOPE_TYPE_NOTIFIED
, py_function
, NULL
);
464 *method_ptr
= closure
->closure
;
466 g_base_info_unref (interface_info
);
467 g_base_info_unref (type_info
);
468 g_base_info_unref (field_info
);
470 g_type_class_unref (implementor_class
);
476 /* Not used, left around for future reference */
478 _wrap_pyg_has_vfunc_implementation (PyObject
*self
, PyObject
*args
)
480 PyGIBaseInfo
*py_info
;
483 gpointer implementor_class
= NULL
;
484 gpointer implementor_vtable
= NULL
;
485 GType implementor_gtype
= 0;
486 GIFieldInfo
*field_info
= NULL
;
488 if (!PyArg_ParseTuple (args
, "O!O!:has_vfunc_implementation",
489 &PyGIBaseInfo_Type
, &py_info
,
490 &PyGTypeWrapper_Type
, &py_type
))
493 implementor_gtype
= pyg_type_from_object (py_type
);
494 g_assert (G_TYPE_IS_CLASSED (implementor_gtype
));
497 find_vfunc_info (py_info
->info
, implementor_gtype
, &implementor_class
, &implementor_vtable
, &field_info
);
498 if (field_info
!= NULL
) {
499 gpointer
*method_ptr
;
502 offset
= g_field_info_get_offset (field_info
);
503 method_ptr
= G_STRUCT_MEMBER_P (implementor_vtable
, offset
);
504 if (*method_ptr
!= NULL
) {
508 g_base_info_unref (field_info
);
510 g_type_class_unref (implementor_class
);
518 _wrap_pyg_variant_type_from_string (PyObject
*self
, PyObject
*args
)
522 PyObject
*py_variant
= NULL
;
524 if (!PyArg_ParseTuple (args
, "s:variant_type_from_string",
529 py_type
= _pygi_type_import_by_name ("GLib", "VariantType");
531 /* Pass the string directly and force a boxed copy. This works because
532 * GVariantType is just a char pointer. */
533 py_variant
= _pygi_boxed_new ( (PyTypeObject
*) py_type
, type_string
,
534 TRUE
, /* copy_boxed */
535 0); /* slice_allocated */
541 _wrap_pyg_source_new (PyObject
*self
, PyObject
*args
)
543 return pyg_source_new ();
546 #define CHUNK_SIZE 8192
549 pyg_channel_read(PyObject
* self
, PyObject
*args
, PyObject
*kwargs
)
552 PyObject
*py_iochannel
, *ret_obj
= NULL
;
553 gsize total_read
= 0;
554 GError
* error
= NULL
;
555 GIOStatus status
= G_IO_STATUS_NORMAL
;
556 GIOChannel
*iochannel
= NULL
;
558 if (!PyArg_ParseTuple (args
, "Oi:pyg_channel_read", &py_iochannel
, &max_count
)) {
561 if (!pyg_boxed_check (py_iochannel
, G_TYPE_IO_CHANNEL
)) {
562 PyErr_SetString(PyExc_TypeError
, "first argument is not a GLib.IOChannel");
567 return PYGLIB_PyBytes_FromString("");
569 iochannel
= pyg_boxed_get (py_iochannel
, GIOChannel
);
571 while (status
== G_IO_STATUS_NORMAL
572 && (max_count
== -1 || total_read
< (gsize
)max_count
)) {
578 buf_size
= CHUNK_SIZE
;
580 buf_size
= max_count
- total_read
;
581 if (buf_size
> CHUNK_SIZE
)
582 buf_size
= CHUNK_SIZE
;
585 if ( ret_obj
== NULL
) {
586 ret_obj
= PYGLIB_PyBytes_FromStringAndSize((char *)NULL
, buf_size
);
590 else if (buf_size
+ total_read
> (gsize
)PYGLIB_PyBytes_Size(ret_obj
)) {
591 if (PYGLIB_PyBytes_Resize(&ret_obj
, buf_size
+ total_read
) == -1)
595 buf
= PYGLIB_PyBytes_AsString(ret_obj
) + total_read
;
597 Py_BEGIN_ALLOW_THREADS
;
598 status
= g_io_channel_read_chars (iochannel
, buf
, buf_size
, &single_read
, &error
);
599 Py_END_ALLOW_THREADS
;
601 if (pygi_error_check (&error
))
604 total_read
+= single_read
;
607 if ( total_read
!= (gsize
)PYGLIB_PyBytes_Size(ret_obj
) ) {
608 if (PYGLIB_PyBytes_Resize(&ret_obj
, total_read
) == -1)
619 static PyMethodDef _gi_functions
[] = {
620 { "enum_add", (PyCFunction
) _wrap_pyg_enum_add
, METH_VARARGS
| METH_KEYWORDS
},
621 { "enum_register_new_gtype_and_add", (PyCFunction
) _wrap_pyg_enum_register_new_gtype_and_add
, METH_VARARGS
| METH_KEYWORDS
},
622 { "flags_add", (PyCFunction
) _wrap_pyg_flags_add
, METH_VARARGS
| METH_KEYWORDS
},
623 { "flags_register_new_gtype_and_add", (PyCFunction
) _wrap_pyg_flags_register_new_gtype_and_add
, METH_VARARGS
| METH_KEYWORDS
},
625 { "register_interface_info", (PyCFunction
) _wrap_pyg_register_interface_info
, METH_VARARGS
},
626 { "hook_up_vfunc_implementation", (PyCFunction
) _wrap_pyg_hook_up_vfunc_implementation
, METH_VARARGS
},
627 { "variant_type_from_string", (PyCFunction
) _wrap_pyg_variant_type_from_string
, METH_VARARGS
},
628 { "source_new", (PyCFunction
) _wrap_pyg_source_new
, METH_NOARGS
},
629 { "source_set_callback", (PyCFunction
) pyg_source_set_callback
, METH_VARARGS
},
630 { "io_channel_read", (PyCFunction
) pyg_channel_read
, METH_VARARGS
},
631 { "require_foreign", (PyCFunction
) pygi_require_foreign
, METH_VARARGS
| METH_KEYWORDS
},
635 static struct PyGI_API CAPI
= {
636 pygi_register_foreign_struct
,
639 PYGLIB_MODULE_START(_gi
, "_gi")
642 PyObject
*_glib_module
;
643 PyObject
*_gobject_module
;
645 /* Always enable Python threads since we cannot predict which GI repositories
646 * might accept Python callbacks run within non-Python threads or might trigger
647 * toggle ref notifications.
648 * See: https://bugzilla.gnome.org/show_bug.cgi?id=709223
650 PyEval_InitThreads ();
652 _glib_module
= pyglib__glib_module_create ();
653 if (_glib_module
== NULL
) {
654 return PYGLIB_MODULE_ERROR_RETURN
;
656 /* In Python 2.x, pyglib_..._module_create returns a borrowed reference and
657 * PyModule_AddObject steals a reference. Ensure we don't share a reference
658 * between sys.modules and gi._gi._glib by incrementing the ref count here.
659 * Note that we don't add this to the PYGLIB_MODULE_START macro because that
660 * would cause a leak for the main module gi._gi */
661 if (PY_MAJOR_VERSION
< 3) {
662 Py_INCREF (_glib_module
);
664 PyModule_AddObject (module
, "_glib", _glib_module
);
665 PyModule_AddStringConstant(module
, "__package__", "gi._gi");
667 _gobject_module
= pyglib__gobject_module_create ();
668 if (_gobject_module
== NULL
) {
669 return PYGLIB_MODULE_ERROR_RETURN
;
671 if (PY_MAJOR_VERSION
< 3) {
672 Py_INCREF (_gobject_module
);
674 PyModule_AddObject (module
, "_gobject", _gobject_module
);
675 PyModule_AddStringConstant(module
, "__package__", "gi._gi");
677 pygi_foreign_init ();
678 pygi_error_register_types (module
);
679 _pygi_repository_register_types (module
);
680 _pygi_info_register_types (module
);
681 _pygi_struct_register_types (module
);
682 _pygi_boxed_register_types (module
);
683 _pygi_ccallback_register_types (module
);
684 pygi_resulttuple_register_types (module
);
686 PyGIWarning
= PyErr_NewException ("gi.PyGIWarning", PyExc_Warning
, NULL
);
688 /* Use RuntimeWarning as the base class of PyGIDeprecationWarning
689 * for unstable (odd minor version) and use DeprecationWarning for
690 * stable (even minor version). This is so PyGObject deprecations
691 * behave the same as regular Python deprecations in stable releases.
693 #if PYGOBJECT_MINOR_VERSION % 2
694 PyGIDeprecationWarning
= PyErr_NewException("gi.PyGIDeprecationWarning",
695 PyExc_RuntimeWarning
, NULL
);
697 PyGIDeprecationWarning
= PyErr_NewException("gi.PyGIDeprecationWarning",
698 PyExc_DeprecationWarning
, NULL
);
701 /* Place holder object used to fill in "from Python" argument lists
702 * for values not supplied by the caller but support a GI default.
704 _PyGIDefaultArgPlaceholder
= PyObject_New(PyObject
, &PyType_Type
);
706 Py_INCREF (PyGIWarning
);
707 PyModule_AddObject (module
, "PyGIWarning", PyGIWarning
);
709 Py_INCREF(PyGIDeprecationWarning
);
710 PyModule_AddObject(module
, "PyGIDeprecationWarning", PyGIDeprecationWarning
);
712 api
= PYGLIB_CPointer_WrapPointer ( (void *) &CAPI
, "gi._API");
714 return PYGLIB_MODULE_ERROR_RETURN
;
716 PyModule_AddObject (module
, "_API", api
);