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"
46 #include "pygobject-object.h"
47 #include "pygoptioncontext.h"
48 #include "pygoptiongroup.h"
50 #include "gobjectmodule.h"
51 #include "pygparamspec.h"
52 #include "pygpointer.h"
54 #include <pyglib-python-compat.h>
56 PyObject *PyGIWarning;
57 PyObject *PyGIDeprecationWarning;
58 PyObject *_PyGIDefaultArgPlaceholder;
61 /* Defined by PYGLIB_MODULE_START */
62 extern PyObject *pyglib__gobject_module_create (void);
64 /* Returns a new flag/enum type or %NULL */
66 flags_enum_from_gtype (GType g_type,
67 PyObject * (add_func) (PyObject *, const char *,
71 GIRepository *repository;
73 const gchar *type_name;
75 repository = g_irepository_get_default ();
76 info = g_irepository_find_by_gtype (repository, g_type);
78 type_name = g_base_info_get_name (info);
79 new_type = add_func (NULL, type_name, NULL, g_type);
80 g_base_info_unref (info);
82 type_name = g_type_name (g_type);
83 new_type = add_func (NULL, type_name, NULL, g_type);
91 _wrap_pyg_enum_add (PyObject *self,
95 static char *kwlist[] = { "g_type", NULL };
99 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
101 kwlist, &PyGTypeWrapper_Type, &py_g_type)) {
105 g_type = pyg_type_from_object (py_g_type);
106 if (g_type == G_TYPE_INVALID) {
110 return flags_enum_from_gtype (g_type, pyg_enum_add);
114 _wrap_pyg_enum_register_new_gtype_and_add (PyObject *self,
118 static char *kwlist[] = { "info", NULL };
119 PyGIBaseInfo *py_info;
122 GEnumValue *g_enum_values;
124 const gchar *namespace;
125 const gchar *type_name;
129 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
130 "O:enum_add_make_new_gtype",
131 kwlist, (PyObject *)&py_info)) {
135 if (!GI_IS_ENUM_INFO (py_info->info) ||
136 g_base_info_get_type ((GIBaseInfo *) py_info->info) != GI_INFO_TYPE_ENUM) {
137 PyErr_SetString (PyExc_TypeError, "info must be an EnumInfo with info type GI_INFO_TYPE_ENUM");
141 info = (GIEnumInfo *)py_info->info;
142 n_values = g_enum_info_get_n_values (info);
144 /* The new memory is zero filled which fulfills the registration
145 * function requirement that the last item is zeroed out as a terminator.
147 g_enum_values = g_new0 (GEnumValue, n_values + 1);
149 for (i = 0; i < n_values; i++) {
150 GIValueInfo *value_info;
151 GEnumValue *enum_value;
153 const gchar *c_identifier;
155 value_info = g_enum_info_get_value (info, i);
156 name = g_base_info_get_name ((GIBaseInfo *) value_info);
157 c_identifier = g_base_info_get_attribute ((GIBaseInfo *) value_info,
160 enum_value = &g_enum_values[i];
161 enum_value->value_nick = g_strdup (name);
162 enum_value->value = g_value_info_get_value (value_info);
164 if (c_identifier == NULL) {
165 enum_value->value_name = enum_value->value_nick;
167 enum_value->value_name = g_strdup (c_identifier);
170 g_base_info_unref ((GIBaseInfo *) value_info);
173 /* Obfuscate the full_name by prefixing it with "Py" to avoid conflicts
174 * with real GTypes. See: https://bugzilla.gnome.org/show_bug.cgi?id=692515
176 namespace = g_base_info_get_namespace ((GIBaseInfo *) info);
177 type_name = g_base_info_get_name ((GIBaseInfo *) info);
178 full_name = g_strconcat ("Py", namespace, type_name, NULL);
180 /* If enum registration fails, free all the memory allocated
181 * for the values array. This needs to leak when successful
182 * as GObject keeps a reference to the data as specified in the docs.
184 g_type = g_enum_register_static (full_name, g_enum_values);
185 if (g_type == G_TYPE_INVALID) {
186 for (i = 0; i < n_values; i++) {
187 GEnumValue *enum_value = &g_enum_values[i];
189 /* Only free value_name if it is different from value_nick to avoid
190 * a double free. The pointer might have been is re-used in the case
191 * c_identifier was NULL in the above loop.
193 if (enum_value->value_name != enum_value->value_nick)
194 g_free ((gchar *) enum_value->value_name);
195 g_free ((gchar *) enum_value->value_nick);
198 PyErr_Format (PyExc_RuntimeError, "Unable to register enum '%s'", full_name);
200 g_free (g_enum_values);
206 return pyg_enum_add (NULL, type_name, NULL, g_type);
210 _wrap_pyg_flags_add (PyObject *self,
214 static char *kwlist[] = { "g_type", NULL };
218 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
220 kwlist, &PyGTypeWrapper_Type, &py_g_type)) {
224 g_type = pyg_type_from_object (py_g_type);
225 if (g_type == G_TYPE_INVALID) {
229 return flags_enum_from_gtype (g_type, pyg_flags_add);
233 _wrap_pyg_flags_register_new_gtype_and_add (PyObject *self,
237 static char *kwlist[] = { "info", NULL };
238 PyGIBaseInfo *py_info;
241 GFlagsValue *g_flags_values;
243 const gchar *namespace;
244 const gchar *type_name;
248 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
249 "O:flags_add_make_new_gtype",
250 kwlist, (PyObject *)&py_info)) {
254 if (!GI_IS_ENUM_INFO (py_info->info) ||
255 g_base_info_get_type ((GIBaseInfo *) py_info->info) != GI_INFO_TYPE_FLAGS) {
256 PyErr_SetString (PyExc_TypeError, "info must be an EnumInfo with info type GI_INFO_TYPE_FLAGS");
260 info = (GIEnumInfo *)py_info->info;
261 n_values = g_enum_info_get_n_values (info);
263 /* The new memory is zero filled which fulfills the registration
264 * function requirement that the last item is zeroed out as a terminator.
266 g_flags_values = g_new0 (GFlagsValue, n_values + 1);
268 for (i = 0; i < n_values; i++) {
269 GIValueInfo *value_info;
270 GFlagsValue *flags_value;
272 const gchar *c_identifier;
274 value_info = g_enum_info_get_value (info, i);
275 name = g_base_info_get_name ((GIBaseInfo *) value_info);
276 c_identifier = g_base_info_get_attribute ((GIBaseInfo *) value_info,
279 flags_value = &g_flags_values[i];
280 flags_value->value_nick = g_strdup (name);
281 flags_value->value = g_value_info_get_value (value_info);
283 if (c_identifier == NULL) {
284 flags_value->value_name = flags_value->value_nick;
286 flags_value->value_name = g_strdup (c_identifier);
289 g_base_info_unref ((GIBaseInfo *) value_info);
292 /* Obfuscate the full_name by prefixing it with "Py" to avoid conflicts
293 * with real GTypes. See: https://bugzilla.gnome.org/show_bug.cgi?id=692515
295 namespace = g_base_info_get_namespace ((GIBaseInfo *) info);
296 type_name = g_base_info_get_name ((GIBaseInfo *) info);
297 full_name = g_strconcat ("Py", namespace, type_name, NULL);
299 /* If enum registration fails, free all the memory allocated
300 * for the values array. This needs to leak when successful
301 * as GObject keeps a reference to the data as specified in the docs.
303 g_type = g_flags_register_static (full_name, g_flags_values);
304 if (g_type == G_TYPE_INVALID) {
305 for (i = 0; i < n_values; i++) {
306 GFlagsValue *flags_value = &g_flags_values[i];
308 /* Only free value_name if it is different from value_nick to avoid
309 * a double free. The pointer might have been is re-used in the case
310 * c_identifier was NULL in the above loop.
312 if (flags_value->value_name != flags_value->value_nick)
313 g_free ((gchar *) flags_value->value_name);
314 g_free ((gchar *) flags_value->value_nick);
317 PyErr_Format (PyExc_RuntimeError, "Unable to register flags '%s'", full_name);
319 g_free (g_flags_values);
325 return pyg_flags_add (NULL, type_name, NULL, g_type);
329 initialize_interface (GTypeInterface *iface, PyTypeObject *pytype)
331 /* pygobject prints a warning if interface_init is NULL */
335 _wrap_pyg_register_interface_info (PyObject *self, PyObject *args)
339 GInterfaceInfo *info;
341 if (!PyArg_ParseTuple (args, "O!:register_interface_info",
342 &PyGTypeWrapper_Type, &py_g_type)) {
346 g_type = pyg_type_from_object (py_g_type);
347 if (!g_type_is_a (g_type, G_TYPE_INTERFACE)) {
348 PyErr_SetString (PyExc_TypeError, "must be an interface");
352 info = g_new0 (GInterfaceInfo, 1);
353 info->interface_init = (GInterfaceInitFunc) initialize_interface;
355 pyg_register_interface_info (g_type, info);
361 find_vfunc_info (GIBaseInfo *vfunc_info,
362 GType implementor_gtype,
363 gpointer *implementor_class_ret,
364 gpointer *implementor_vtable_ret,
365 GIFieldInfo **field_info_ret)
367 GType ancestor_g_type = 0;
369 GIBaseInfo *ancestor_info;
370 GIStructInfo *struct_info;
371 gpointer implementor_class = NULL;
372 gboolean is_interface = FALSE;
374 ancestor_info = g_base_info_get_container (vfunc_info);
375 is_interface = g_base_info_get_type (ancestor_info) == GI_INFO_TYPE_INTERFACE;
377 ancestor_g_type = g_registered_type_info_get_g_type (
378 (GIRegisteredTypeInfo *) ancestor_info);
379 implementor_class = g_type_class_ref (implementor_gtype);
381 GTypeInstance *implementor_iface_class;
382 implementor_iface_class = g_type_interface_peek (implementor_class,
384 if (implementor_iface_class == NULL) {
385 g_type_class_unref (implementor_class);
386 PyErr_Format (PyExc_RuntimeError,
387 "Couldn't find GType of implementor of interface %s. "
388 "Forgot to set __gtype_name__?",
389 g_type_name (ancestor_g_type));
393 *implementor_vtable_ret = implementor_iface_class;
395 struct_info = g_interface_info_get_iface_struct ( (GIInterfaceInfo*) ancestor_info);
397 struct_info = g_object_info_get_class_struct ( (GIObjectInfo*) ancestor_info);
398 *implementor_vtable_ret = implementor_class;
401 *implementor_class_ret = implementor_class;
403 length = g_struct_info_get_n_fields (struct_info);
404 for (i = 0; i < length; i++) {
405 GIFieldInfo *field_info;
406 GITypeInfo *type_info;
408 field_info = g_struct_info_get_field (struct_info, i);
410 if (strcmp (g_base_info_get_name ( (GIBaseInfo*) field_info),
411 g_base_info_get_name ( (GIBaseInfo*) vfunc_info)) != 0) {
412 g_base_info_unref (field_info);
416 type_info = g_field_info_get_type (field_info);
417 if (g_type_info_get_tag (type_info) == GI_TYPE_TAG_INTERFACE) {
418 g_base_info_unref (type_info);
419 *field_info_ret = field_info;
423 g_base_info_unref (type_info);
424 g_base_info_unref (field_info);
427 g_base_info_unref (struct_info);
431 _wrap_pyg_hook_up_vfunc_implementation (PyObject *self, PyObject *args)
433 PyGIBaseInfo *py_info;
435 PyObject *py_function;
436 GType implementor_gtype = 0;
437 gpointer implementor_class = NULL;
438 gpointer implementor_vtable = NULL;
439 GIFieldInfo *field_info = NULL;
440 gpointer *method_ptr = NULL;
441 PyGICClosure *closure = NULL;
443 if (!PyArg_ParseTuple (args, "O!O!O:hook_up_vfunc_implementation",
444 &PyGIBaseInfo_Type, &py_info,
445 &PyGTypeWrapper_Type, &py_type,
449 implementor_gtype = pyg_type_from_object (py_type);
450 g_assert (G_TYPE_IS_CLASSED (implementor_gtype));
452 find_vfunc_info (py_info->info, implementor_gtype, &implementor_class, &implementor_vtable, &field_info);
453 if (field_info != NULL) {
454 GITypeInfo *type_info;
455 GIBaseInfo *interface_info;
456 GICallbackInfo *callback_info;
459 type_info = g_field_info_get_type (field_info);
461 interface_info = g_type_info_get_interface (type_info);
462 g_assert (g_base_info_get_type (interface_info) == GI_INFO_TYPE_CALLBACK);
464 callback_info = (GICallbackInfo*) interface_info;
465 offset = g_field_info_get_offset (field_info);
466 method_ptr = G_STRUCT_MEMBER_P (implementor_vtable, offset);
468 closure = _pygi_make_native_closure ( (GICallableInfo*) callback_info,
469 GI_SCOPE_TYPE_NOTIFIED, py_function, NULL);
471 *method_ptr = closure->closure;
473 g_base_info_unref (interface_info);
474 g_base_info_unref (type_info);
475 g_base_info_unref (field_info);
477 g_type_class_unref (implementor_class);
483 /* Not used, left around for future reference */
485 _wrap_pyg_has_vfunc_implementation (PyObject *self, PyObject *args)
487 PyGIBaseInfo *py_info;
490 gpointer implementor_class = NULL;
491 gpointer implementor_vtable = NULL;
492 GType implementor_gtype = 0;
493 GIFieldInfo *field_info = NULL;
495 if (!PyArg_ParseTuple (args, "O!O!:has_vfunc_implementation",
496 &PyGIBaseInfo_Type, &py_info,
497 &PyGTypeWrapper_Type, &py_type))
500 implementor_gtype = pyg_type_from_object (py_type);
501 g_assert (G_TYPE_IS_CLASSED (implementor_gtype));
504 find_vfunc_info (py_info->info, implementor_gtype, &implementor_class, &implementor_vtable, &field_info);
505 if (field_info != NULL) {
506 gpointer *method_ptr;
509 offset = g_field_info_get_offset (field_info);
510 method_ptr = G_STRUCT_MEMBER_P (implementor_vtable, offset);
511 if (*method_ptr != NULL) {
515 g_base_info_unref (field_info);
517 g_type_class_unref (implementor_class);
525 _wrap_pyg_variant_type_from_string (PyObject *self, PyObject *args)
529 PyObject *py_variant = NULL;
531 if (!PyArg_ParseTuple (args, "s:variant_type_from_string",
536 py_type = _pygi_type_import_by_name ("GLib", "VariantType");
538 /* Pass the string directly and force a boxed copy. This works because
539 * GVariantType is just a char pointer. */
540 py_variant = _pygi_boxed_new ( (PyTypeObject *) py_type, type_string,
541 TRUE, /* copy_boxed */
542 0); /* slice_allocated */
548 _wrap_pyg_source_new (PyObject *self, PyObject *args)
550 return pyg_source_new ();
553 #define CHUNK_SIZE 8192
556 pyg_channel_read(PyObject* self, PyObject *args, PyObject *kwargs)
559 PyObject *py_iochannel, *ret_obj = NULL;
560 gsize total_read = 0;
561 GError* error = NULL;
562 GIOStatus status = G_IO_STATUS_NORMAL;
563 GIOChannel *iochannel = NULL;
565 if (!PyArg_ParseTuple (args, "Oi:pyg_channel_read", &py_iochannel, &max_count)) {
568 if (!pyg_boxed_check (py_iochannel, G_TYPE_IO_CHANNEL)) {
569 PyErr_SetString(PyExc_TypeError, "first argument is not a GLib.IOChannel");
574 return PYGLIB_PyBytes_FromString("");
576 iochannel = pyg_boxed_get (py_iochannel, GIOChannel);
578 while (status == G_IO_STATUS_NORMAL
579 && (max_count == -1 || total_read < (gsize)max_count)) {
585 buf_size = CHUNK_SIZE;
587 buf_size = max_count - total_read;
588 if (buf_size > CHUNK_SIZE)
589 buf_size = CHUNK_SIZE;
592 if ( ret_obj == NULL ) {
593 ret_obj = PYGLIB_PyBytes_FromStringAndSize((char *)NULL, buf_size);
597 else if (buf_size + total_read > (gsize)PYGLIB_PyBytes_Size(ret_obj)) {
598 if (PYGLIB_PyBytes_Resize(&ret_obj, buf_size + total_read) == -1)
602 buf = PYGLIB_PyBytes_AsString(ret_obj) + total_read;
604 Py_BEGIN_ALLOW_THREADS;
605 status = g_io_channel_read_chars (iochannel, buf, buf_size, &single_read, &error);
606 Py_END_ALLOW_THREADS;
608 if (pygi_error_check (&error))
611 total_read += single_read;
614 if ( total_read != (gsize)PYGLIB_PyBytes_Size(ret_obj) ) {
615 if (PYGLIB_PyBytes_Resize(&ret_obj, total_read) == -1)
626 static PyMethodDef _gi_functions[] = {
627 { "enum_add", (PyCFunction) _wrap_pyg_enum_add, METH_VARARGS | METH_KEYWORDS },
628 { "enum_register_new_gtype_and_add", (PyCFunction) _wrap_pyg_enum_register_new_gtype_and_add, METH_VARARGS | METH_KEYWORDS },
629 { "flags_add", (PyCFunction) _wrap_pyg_flags_add, METH_VARARGS | METH_KEYWORDS },
630 { "flags_register_new_gtype_and_add", (PyCFunction) _wrap_pyg_flags_register_new_gtype_and_add, METH_VARARGS | METH_KEYWORDS },
632 { "register_interface_info", (PyCFunction) _wrap_pyg_register_interface_info, METH_VARARGS },
633 { "hook_up_vfunc_implementation", (PyCFunction) _wrap_pyg_hook_up_vfunc_implementation, METH_VARARGS },
634 { "variant_type_from_string", (PyCFunction) _wrap_pyg_variant_type_from_string, METH_VARARGS },
635 { "source_new", (PyCFunction) _wrap_pyg_source_new, METH_NOARGS },
636 { "source_set_callback", (PyCFunction) pyg_source_set_callback, METH_VARARGS },
637 { "io_channel_read", (PyCFunction) pyg_channel_read, METH_VARARGS },
638 { "require_foreign", (PyCFunction) pygi_require_foreign, METH_VARARGS | METH_KEYWORDS },
640 (PyCFunction)pyglib_spawn_async, METH_VARARGS|METH_KEYWORDS,
641 "spawn_async(argv, envp=None, working_directory=None,\n"
642 " flags=0, child_setup=None, user_data=None,\n"
643 " standard_input=None, standard_output=None,\n"
644 " standard_error=None) -> (pid, stdin, stdout, stderr)\n"
646 "Execute a child program asynchronously within a glib.MainLoop()\n"
647 "See the reference manual for a complete reference.\n" },
648 { "type_name", pyg_type_name, METH_VARARGS },
649 { "type_from_name", pyg_type_from_name, METH_VARARGS },
650 { "type_is_a", pyg_type_is_a, METH_VARARGS },
651 { "type_register", _wrap_pyg_type_register, METH_VARARGS },
652 { "signal_new", pyg_signal_new, METH_VARARGS },
654 pyg_object_class_list_properties, METH_VARARGS },
656 (PyCFunction)pyg_object_new, METH_VARARGS|METH_KEYWORDS },
657 { "signal_accumulator_true_handled",
658 (PyCFunction)pyg_signal_accumulator_true_handled, METH_VARARGS },
659 { "add_emission_hook",
660 (PyCFunction)pyg_add_emission_hook, METH_VARARGS },
661 { "_install_metaclass",
662 (PyCFunction)pyg__install_metaclass, METH_O },
664 (PyCFunction)pyg__gvalue_get, METH_O },
666 (PyCFunction)pyg__gvalue_set, METH_VARARGS },
670 static struct PyGI_API CAPI = {
671 pygi_register_foreign_struct,
674 PYGLIB_MODULE_START(_gi, "_gi")
677 PyObject *module_dict = PyModule_GetDict (module);
679 /* Always enable Python threads since we cannot predict which GI repositories
680 * might accept Python callbacks run within non-Python threads or might trigger
681 * toggle ref notifications.
682 * See: https://bugzilla.gnome.org/show_bug.cgi?id=709223
684 PyEval_InitThreads ();
686 PyModule_AddStringConstant(module, "__package__", "gi._gi");
688 pygi_foreign_init ();
689 pygi_error_register_types (module);
690 _pygi_repository_register_types (module);
691 _pygi_info_register_types (module);
692 _pygi_struct_register_types (module);
693 _pygi_boxed_register_types (module);
694 _pygi_ccallback_register_types (module);
695 pygi_resulttuple_register_types (module);
697 pyglib_spawn_register_types (module_dict);
698 pyglib_option_context_register_types (module_dict);
699 pyglib_option_group_register_types (module_dict);
701 pygobject_register_api (module_dict);
702 pygobject_register_constants (module);
703 pygobject_register_features (module_dict);
704 pygobject_register_version_tuples (module_dict);
705 pygobject_register_warnings (module_dict);
706 pygobject_type_register_types (module_dict);
707 pygobject_object_register_types (module_dict);
708 pygobject_interface_register_types (module_dict);
709 pygobject_paramspec_register_types (module_dict);
710 pygobject_boxed_register_types (module_dict);
711 pygobject_pointer_register_types (module_dict);
712 pygobject_enum_register_types (module_dict);
713 pygobject_flags_register_types (module_dict);
715 PyGIWarning = PyErr_NewException ("gi.PyGIWarning", PyExc_Warning, NULL);
717 /* Use RuntimeWarning as the base class of PyGIDeprecationWarning
718 * for unstable (odd minor version) and use DeprecationWarning for
719 * stable (even minor version). This is so PyGObject deprecations
720 * behave the same as regular Python deprecations in stable releases.
722 #if PYGOBJECT_MINOR_VERSION % 2
723 PyGIDeprecationWarning = PyErr_NewException("gi.PyGIDeprecationWarning",
724 PyExc_RuntimeWarning, NULL);
726 PyGIDeprecationWarning = PyErr_NewException("gi.PyGIDeprecationWarning",
727 PyExc_DeprecationWarning, NULL);
730 /* Place holder object used to fill in "from Python" argument lists
731 * for values not supplied by the caller but support a GI default.
733 _PyGIDefaultArgPlaceholder = PyObject_New(PyObject, &PyType_Type);
735 Py_INCREF (PyGIWarning);
736 PyModule_AddObject (module, "PyGIWarning", PyGIWarning);
738 Py_INCREF(PyGIDeprecationWarning);
739 PyModule_AddObject(module, "PyGIDeprecationWarning", PyGIDeprecationWarning);
741 api = PYGLIB_CPointer_WrapPointer ( (void *) &CAPI, "gi._API");
743 return PYGLIB_MODULE_ERROR_RETURN;
745 PyModule_AddObject (module, "_API", api);