e14b4f6a924d24d201ec60e5c4ddc8c1b45004b1
[platform/upstream/pygobject2.git] / gi / gimodule.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  *
4  * Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
5  *
6  *   gimodule.c: wrapper for the gobject-introspection library.
7  *
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.
12  *
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.
17  *
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
21  * USA
22  */
23
24 #include <Python.h>
25 #include <glib-object.h>
26
27 #include "config.h"
28 #include "pyglib.h"
29 #include "pyginterface.h"
30 #include "pygi-repository.h"
31 #include "pyglib.h"
32 #include "pygtype.h"
33 #include "pygenum.h"
34 #include "pygboxed.h"
35 #include "pygflags.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"
49 #include "pygspawn.h"
50 #include "gobjectmodule.h"
51 #include "pygparamspec.h"
52 #include "pygpointer.h"
53
54 #include <pyglib-python-compat.h>
55
56 PyObject *PyGIWarning;
57 PyObject *PyGIDeprecationWarning;
58 PyObject *_PyGIDefaultArgPlaceholder;
59
60
61 /* Defined by PYGLIB_MODULE_START */
62 extern PyObject *pyglib__gobject_module_create (void);
63
64 /* Returns a new flag/enum type or %NULL */
65 static PyObject *
66 flags_enum_from_gtype (GType g_type,
67                        PyObject * (add_func) (PyObject *, const char *,
68                                               const char *, GType))
69 {
70     PyObject *new_type;
71     GIRepository *repository;
72     GIBaseInfo *info;
73     const gchar *type_name;
74
75     repository = g_irepository_get_default ();
76     info = g_irepository_find_by_gtype (repository, g_type);
77     if (info != NULL) {
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);
81     } else {
82         type_name = g_type_name (g_type);
83         new_type = add_func (NULL, type_name, NULL, g_type);
84     }
85
86     return new_type;
87 }
88
89
90 static PyObject *
91 _wrap_pyg_enum_add (PyObject *self,
92                     PyObject *args,
93                     PyObject *kwargs)
94 {
95     static char *kwlist[] = { "g_type", NULL };
96     PyObject *py_g_type;
97     GType g_type;
98
99     if (!PyArg_ParseTupleAndKeywords (args, kwargs,
100                                       "O!:enum_add",
101                                       kwlist, &PyGTypeWrapper_Type, &py_g_type)) {
102         return NULL;
103     }
104
105     g_type = pyg_type_from_object (py_g_type);
106     if (g_type == G_TYPE_INVALID) {
107         return NULL;
108     }
109
110     return flags_enum_from_gtype (g_type, pyg_enum_add);
111 }
112
113 static PyObject *
114 _wrap_pyg_enum_register_new_gtype_and_add (PyObject *self,
115                                            PyObject *args,
116                                            PyObject *kwargs)
117 {
118     static char *kwlist[] = { "info", NULL };
119     PyGIBaseInfo *py_info;
120     GIEnumInfo *info;
121     gint n_values;
122     GEnumValue *g_enum_values;
123     int i;
124     const gchar *namespace;
125     const gchar *type_name;
126     gchar *full_name;
127     GType g_type;
128
129     if (!PyArg_ParseTupleAndKeywords (args, kwargs,
130                                       "O:enum_add_make_new_gtype",
131                                       kwlist, (PyObject *)&py_info)) {
132         return NULL;
133     }
134
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");
138         return NULL;
139     }
140
141     info = (GIEnumInfo *)py_info->info;
142     n_values = g_enum_info_get_n_values (info);
143
144     /* The new memory is zero filled which fulfills the registration
145      * function requirement that the last item is zeroed out as a terminator.
146      */
147     g_enum_values = g_new0 (GEnumValue, n_values + 1);
148
149     for (i = 0; i < n_values; i++) {
150         GIValueInfo *value_info;
151         GEnumValue *enum_value;
152         const gchar *name;
153         const gchar *c_identifier;
154
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,
158                                                   "c:identifier");
159
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);
163
164         if (c_identifier == NULL) {
165             enum_value->value_name = enum_value->value_nick;
166         } else {
167             enum_value->value_name = g_strdup (c_identifier);
168         }
169
170         g_base_info_unref ((GIBaseInfo *) value_info);
171     }
172
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
175      */
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);
179
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.
183      */
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];
188
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.
192              */
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);
196         }
197
198         PyErr_Format (PyExc_RuntimeError, "Unable to register enum '%s'", full_name);
199
200         g_free (g_enum_values);
201         g_free (full_name);
202         return NULL;
203     }
204
205     g_free (full_name);
206     return pyg_enum_add (NULL, type_name, NULL, g_type);
207 }
208
209 static PyObject *
210 _wrap_pyg_flags_add (PyObject *self,
211                      PyObject *args,
212                      PyObject *kwargs)
213 {
214     static char *kwlist[] = { "g_type", NULL };
215     PyObject *py_g_type;
216     GType g_type;
217
218     if (!PyArg_ParseTupleAndKeywords (args, kwargs,
219                                       "O!:flags_add",
220                                       kwlist, &PyGTypeWrapper_Type, &py_g_type)) {
221         return NULL;
222     }
223
224     g_type = pyg_type_from_object (py_g_type);
225     if (g_type == G_TYPE_INVALID) {
226         return NULL;
227     }
228
229     return flags_enum_from_gtype (g_type, pyg_flags_add);
230 }
231
232 static PyObject *
233 _wrap_pyg_flags_register_new_gtype_and_add (PyObject *self,
234                                             PyObject *args,
235                                             PyObject *kwargs)
236 {
237     static char *kwlist[] = { "info", NULL };
238     PyGIBaseInfo *py_info;
239     GIEnumInfo *info;
240     gint n_values;
241     GFlagsValue *g_flags_values;
242     int i;
243     const gchar *namespace;
244     const gchar *type_name;
245     gchar *full_name;
246     GType g_type;
247
248     if (!PyArg_ParseTupleAndKeywords (args, kwargs,
249                                       "O:flags_add_make_new_gtype",
250                                       kwlist, (PyObject *)&py_info)) {
251         return NULL;
252     }
253
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");
257         return NULL;
258     }
259
260     info = (GIEnumInfo *)py_info->info;
261     n_values = g_enum_info_get_n_values (info);
262
263     /* The new memory is zero filled which fulfills the registration
264      * function requirement that the last item is zeroed out as a terminator.
265      */
266     g_flags_values = g_new0 (GFlagsValue, n_values + 1);
267
268     for (i = 0; i < n_values; i++) {
269         GIValueInfo *value_info;
270         GFlagsValue *flags_value;
271         const gchar *name;
272         const gchar *c_identifier;
273
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,
277                                                   "c:identifier");
278
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);
282
283         if (c_identifier == NULL) {
284             flags_value->value_name = flags_value->value_nick;
285         } else {
286             flags_value->value_name = g_strdup (c_identifier);
287         }
288
289         g_base_info_unref ((GIBaseInfo *) value_info);
290     }
291
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
294      */
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);
298
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.
302      */
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];
307
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.
311              */
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);
315         }
316
317         PyErr_Format (PyExc_RuntimeError, "Unable to register flags '%s'", full_name);
318
319         g_free (g_flags_values);
320         g_free (full_name);
321         return NULL;
322     }
323
324     g_free (full_name);
325     return pyg_flags_add (NULL, type_name, NULL, g_type);
326 }
327
328 static void
329 initialize_interface (GTypeInterface *iface, PyTypeObject *pytype)
330 {
331     /* pygobject prints a warning if interface_init is NULL */
332 }
333
334 static PyObject *
335 _wrap_pyg_register_interface_info (PyObject *self, PyObject *args)
336 {
337     PyObject *py_g_type;
338     GType g_type;
339     GInterfaceInfo *info;
340
341     if (!PyArg_ParseTuple (args, "O!:register_interface_info",
342                            &PyGTypeWrapper_Type, &py_g_type)) {
343         return NULL;
344     }
345
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");
349         return NULL;
350     }
351
352     info = g_new0 (GInterfaceInfo, 1);
353     info->interface_init = (GInterfaceInitFunc) initialize_interface;
354
355     pyg_register_interface_info (g_type, info);
356
357     Py_RETURN_NONE;
358 }
359
360 static void
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)
366 {
367     GType ancestor_g_type = 0;
368     int length, i;
369     GIBaseInfo *ancestor_info;
370     GIStructInfo *struct_info;
371     gpointer implementor_class = NULL;
372     gboolean is_interface = FALSE;
373
374     ancestor_info = g_base_info_get_container (vfunc_info);
375     is_interface = g_base_info_get_type (ancestor_info) == GI_INFO_TYPE_INTERFACE;
376
377     ancestor_g_type = g_registered_type_info_get_g_type (
378                           (GIRegisteredTypeInfo *) ancestor_info);
379     implementor_class = g_type_class_ref (implementor_gtype);
380     if (is_interface) {
381         GTypeInstance *implementor_iface_class;
382         implementor_iface_class = g_type_interface_peek (implementor_class,
383                                                          ancestor_g_type);
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));
390             return;
391         }
392
393         *implementor_vtable_ret = implementor_iface_class;
394
395         struct_info = g_interface_info_get_iface_struct ( (GIInterfaceInfo*) ancestor_info);
396     } else {
397         struct_info = g_object_info_get_class_struct ( (GIObjectInfo*) ancestor_info);
398         *implementor_vtable_ret = implementor_class;
399     }
400
401     *implementor_class_ret = implementor_class;
402
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;
407
408         field_info = g_struct_info_get_field (struct_info, i);
409
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);
413             continue;
414         }
415
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;
420             break;
421         }
422
423         g_base_info_unref (type_info);
424         g_base_info_unref (field_info);
425     }
426
427     g_base_info_unref (struct_info);
428 }
429
430 static PyObject *
431 _wrap_pyg_hook_up_vfunc_implementation (PyObject *self, PyObject *args)
432 {
433     PyGIBaseInfo *py_info;
434     PyObject *py_type;
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;
442
443     if (!PyArg_ParseTuple (args, "O!O!O:hook_up_vfunc_implementation",
444                            &PyGIBaseInfo_Type, &py_info,
445                            &PyGTypeWrapper_Type, &py_type,
446                            &py_function))
447         return NULL;
448
449     implementor_gtype = pyg_type_from_object (py_type);
450     g_assert (G_TYPE_IS_CLASSED (implementor_gtype));
451
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;
457         gint offset;
458
459         type_info = g_field_info_get_type (field_info);
460
461         interface_info = g_type_info_get_interface (type_info);
462         g_assert (g_base_info_get_type (interface_info) == GI_INFO_TYPE_CALLBACK);
463
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);
467
468         closure = _pygi_make_native_closure ( (GICallableInfo*) callback_info,
469                                               GI_SCOPE_TYPE_NOTIFIED, py_function, NULL);
470
471         *method_ptr = closure->closure;
472
473         g_base_info_unref (interface_info);
474         g_base_info_unref (type_info);
475         g_base_info_unref (field_info);
476     }
477     g_type_class_unref (implementor_class);
478
479     Py_RETURN_NONE;
480 }
481
482 #if 0
483 /* Not used, left around for future reference */
484 static PyObject *
485 _wrap_pyg_has_vfunc_implementation (PyObject *self, PyObject *args)
486 {
487     PyGIBaseInfo *py_info;
488     PyObject *py_type;
489     PyObject *py_ret;
490     gpointer implementor_class = NULL;
491     gpointer implementor_vtable = NULL;
492     GType implementor_gtype = 0;
493     GIFieldInfo *field_info = NULL;
494
495     if (!PyArg_ParseTuple (args, "O!O!:has_vfunc_implementation",
496                            &PyGIBaseInfo_Type, &py_info,
497                            &PyGTypeWrapper_Type, &py_type))
498         return NULL;
499
500     implementor_gtype = pyg_type_from_object (py_type);
501     g_assert (G_TYPE_IS_CLASSED (implementor_gtype));
502
503     py_ret = Py_False;
504     find_vfunc_info (py_info->info, implementor_gtype, &implementor_class, &implementor_vtable, &field_info);
505     if (field_info != NULL) {
506         gpointer *method_ptr;
507         gint offset;
508
509         offset = g_field_info_get_offset (field_info);
510         method_ptr = G_STRUCT_MEMBER_P (implementor_vtable, offset);
511         if (*method_ptr != NULL) {
512             py_ret = Py_True;
513         }
514
515         g_base_info_unref (field_info);
516     }
517     g_type_class_unref (implementor_class);
518
519     Py_INCREF(py_ret);
520     return py_ret;
521 }
522 #endif
523
524 static PyObject *
525 _wrap_pyg_variant_type_from_string (PyObject *self, PyObject *args)
526 {
527     char *type_string;
528     PyObject *py_type;
529     PyObject *py_variant = NULL;
530
531     if (!PyArg_ParseTuple (args, "s:variant_type_from_string",
532                            &type_string)) {
533         return NULL;
534     }
535
536     py_type = _pygi_type_import_by_name ("GLib", "VariantType");
537
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 */
543
544     return py_variant;
545 }
546
547 static PyObject *
548 _wrap_pyg_source_new (PyObject *self, PyObject *args)
549 {
550     return pyg_source_new ();
551 }
552
553 #define CHUNK_SIZE 8192
554
555 static PyObject*
556 pyg_channel_read(PyObject* self, PyObject *args, PyObject *kwargs)
557 {
558     int max_count = -1;
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;
564
565     if (!PyArg_ParseTuple (args, "Oi:pyg_channel_read", &py_iochannel, &max_count)) {
566         return NULL;
567     }
568     if (!pyg_boxed_check (py_iochannel, G_TYPE_IO_CHANNEL)) {
569         PyErr_SetString(PyExc_TypeError, "first argument is not a GLib.IOChannel");
570         return NULL;
571     }
572         
573     if (max_count == 0)
574         return PYGLIB_PyBytes_FromString("");
575
576     iochannel = pyg_boxed_get (py_iochannel, GIOChannel);
577
578     while (status == G_IO_STATUS_NORMAL
579            && (max_count == -1 || total_read < (gsize)max_count)) {
580         gsize single_read;
581         char* buf;
582         gsize buf_size;
583         
584         if (max_count == -1) 
585             buf_size = CHUNK_SIZE;
586         else {
587             buf_size = max_count - total_read;
588             if (buf_size > CHUNK_SIZE)
589                 buf_size = CHUNK_SIZE;
590         }
591         
592         if ( ret_obj == NULL ) {
593             ret_obj = PYGLIB_PyBytes_FromStringAndSize((char *)NULL, buf_size);
594             if (ret_obj == NULL)
595                 goto failure;
596         }
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)
599                 goto failure;
600         }
601        
602         buf = PYGLIB_PyBytes_AsString(ret_obj) + total_read;
603
604         Py_BEGIN_ALLOW_THREADS;
605         status = g_io_channel_read_chars (iochannel, buf, buf_size, &single_read, &error);
606         Py_END_ALLOW_THREADS;
607
608         if (pygi_error_check (&error))
609             goto failure;
610         
611         total_read += single_read;
612     }
613         
614     if ( total_read != (gsize)PYGLIB_PyBytes_Size(ret_obj) ) {
615         if (PYGLIB_PyBytes_Resize(&ret_obj, total_read) == -1)
616             goto failure;
617     }
618
619     return ret_obj;
620
621   failure:
622     Py_XDECREF(ret_obj);
623     return NULL;
624 }
625
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 },
631
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 },
639     { "spawn_async",
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"
645       "\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 },
653     { "list_properties",
654       pyg_object_class_list_properties, METH_VARARGS },
655     { "new",
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 },
663     { "_gvalue_get",
664       (PyCFunction)pyg__gvalue_get, METH_O },
665     { "_gvalue_set",
666       (PyCFunction)pyg__gvalue_set, METH_VARARGS },
667     { NULL, NULL, 0 }
668 };
669
670 static struct PyGI_API CAPI = {
671   pygi_register_foreign_struct,
672 };
673
674 PYGLIB_MODULE_START(_gi, "_gi")
675 {
676     PyObject *api;
677     PyObject *module_dict = PyModule_GetDict (module);
678
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
683      */
684     PyEval_InitThreads ();
685
686     PyModule_AddStringConstant(module, "__package__", "gi._gi");
687
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);
696
697     pyglib_spawn_register_types (module_dict);
698     pyglib_option_context_register_types (module_dict);
699     pyglib_option_group_register_types (module_dict);
700
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);
714
715     PyGIWarning = PyErr_NewException ("gi.PyGIWarning", PyExc_Warning, NULL);
716
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.
721      */
722 #if PYGOBJECT_MINOR_VERSION % 2
723     PyGIDeprecationWarning = PyErr_NewException("gi.PyGIDeprecationWarning",
724                                                 PyExc_RuntimeWarning, NULL);
725 #else
726     PyGIDeprecationWarning = PyErr_NewException("gi.PyGIDeprecationWarning",
727                                                 PyExc_DeprecationWarning, NULL);
728 #endif
729
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.
732      */
733     _PyGIDefaultArgPlaceholder = PyObject_New(PyObject, &PyType_Type);
734
735     Py_INCREF (PyGIWarning);
736     PyModule_AddObject (module, "PyGIWarning", PyGIWarning);
737
738     Py_INCREF(PyGIDeprecationWarning);
739     PyModule_AddObject(module, "PyGIDeprecationWarning", PyGIDeprecationWarning);
740
741     api = PYGLIB_CPointer_WrapPointer ( (void *) &CAPI, "gi._API");
742     if (api == NULL) {
743         return PYGLIB_MODULE_ERROR_RETURN;
744     }
745     PyModule_AddObject (module, "_API", api);
746 }
747 PYGLIB_MODULE_END