Imported Upstream version 3.21.91
[platform/upstream/python-gobject.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 <glib-object.h>
25
26 #include "pyglib.h"
27 #include "pyginterface.h"
28 #include "pygi-repository.h"
29 #include "pyglib.h"
30 #include "pygtype.h"
31 #include "pygenum.h"
32 #include "pygboxed.h"
33 #include "pygflags.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"
44
45 #include <pyglib-python-compat.h>
46
47 PyObject *PyGIWarning;
48 PyObject *PyGIDeprecationWarning;
49 PyObject *_PyGIDefaultArgPlaceholder;
50
51
52 /* Defined by PYGLIB_MODULE_START */
53 extern PyObject *pyglib__gobject_module_create (void);
54
55 /* Returns a new flag/enum type or %NULL */
56 static PyObject *
57 flags_enum_from_gtype (GType g_type,
58                        PyObject * (add_func) (PyObject *, const char *,
59                                               const char *, GType))
60 {
61     PyObject *new_type;
62     GIRepository *repository;
63     GIBaseInfo *info;
64     const gchar *type_name;
65
66     repository = g_irepository_get_default ();
67     info = g_irepository_find_by_gtype (repository, g_type);
68     if (info != NULL) {
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);
72     } else {
73         type_name = g_type_name (g_type);
74         new_type = add_func (NULL, type_name, NULL, g_type);
75     }
76
77     return new_type;
78 }
79
80
81 static PyObject *
82 _wrap_pyg_enum_add (PyObject *self,
83                     PyObject *args,
84                     PyObject *kwargs)
85 {
86     static char *kwlist[] = { "g_type", NULL };
87     PyObject *py_g_type;
88     GType g_type;
89
90     if (!PyArg_ParseTupleAndKeywords (args, kwargs,
91                                       "O!:enum_add",
92                                       kwlist, &PyGTypeWrapper_Type, &py_g_type)) {
93         return NULL;
94     }
95
96     g_type = pyg_type_from_object (py_g_type);
97     if (g_type == G_TYPE_INVALID) {
98         return NULL;
99     }
100
101     return flags_enum_from_gtype (g_type, pyg_enum_add);
102 }
103
104 static PyObject *
105 _wrap_pyg_enum_register_new_gtype_and_add (PyObject *self,
106                                            PyObject *args,
107                                            PyObject *kwargs)
108 {
109     static char *kwlist[] = { "info", NULL };
110     PyGIBaseInfo *py_info;
111     GIEnumInfo *info;
112     gint n_values;
113     GEnumValue *g_enum_values;
114     int i;
115     const gchar *namespace;
116     const gchar *type_name;
117     gchar *full_name;
118     GType g_type;
119
120     if (!PyArg_ParseTupleAndKeywords (args, kwargs,
121                                       "O:enum_add_make_new_gtype",
122                                       kwlist, (PyObject *)&py_info)) {
123         return NULL;
124     }
125
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");
129         return NULL;
130     }
131
132     info = (GIEnumInfo *)py_info->info;
133     n_values = g_enum_info_get_n_values (info);
134
135     /* The new memory is zero filled which fulfills the registration
136      * function requirement that the last item is zeroed out as a terminator.
137      */
138     g_enum_values = g_new0 (GEnumValue, n_values + 1);
139
140     for (i = 0; i < n_values; i++) {
141         GIValueInfo *value_info;
142         GEnumValue *enum_value;
143         const gchar *name;
144         const gchar *c_identifier;
145
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,
149                                                   "c:identifier");
150
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);
154
155         if (c_identifier == NULL) {
156             enum_value->value_name = enum_value->value_nick;
157         } else {
158             enum_value->value_name = g_strdup (c_identifier);
159         }
160
161         g_base_info_unref ((GIBaseInfo *) value_info);
162     }
163
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
166      */
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);
170
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.
174      */
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];
179
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.
183              */
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);
187         }
188
189         PyErr_Format (PyExc_RuntimeError, "Unable to register enum '%s'", full_name);
190
191         g_free (g_enum_values);
192         g_free (full_name);
193         return NULL;
194     }
195
196     g_free (full_name);
197     return pyg_enum_add (NULL, type_name, NULL, g_type);
198 }
199
200 static PyObject *
201 _wrap_pyg_flags_add (PyObject *self,
202                      PyObject *args,
203                      PyObject *kwargs)
204 {
205     static char *kwlist[] = { "g_type", NULL };
206     PyObject *py_g_type;
207     GType g_type;
208
209     if (!PyArg_ParseTupleAndKeywords (args, kwargs,
210                                       "O!:flags_add",
211                                       kwlist, &PyGTypeWrapper_Type, &py_g_type)) {
212         return NULL;
213     }
214
215     g_type = pyg_type_from_object (py_g_type);
216     if (g_type == G_TYPE_INVALID) {
217         return NULL;
218     }
219
220     return flags_enum_from_gtype (g_type, pyg_flags_add);
221 }
222
223 static PyObject *
224 _wrap_pyg_flags_register_new_gtype_and_add (PyObject *self,
225                                             PyObject *args,
226                                             PyObject *kwargs)
227 {
228     static char *kwlist[] = { "info", NULL };
229     PyGIBaseInfo *py_info;
230     GIEnumInfo *info;
231     gint n_values;
232     GFlagsValue *g_flags_values;
233     int i;
234     const gchar *namespace;
235     const gchar *type_name;
236     gchar *full_name;
237     GType g_type;
238
239     if (!PyArg_ParseTupleAndKeywords (args, kwargs,
240                                       "O:flags_add_make_new_gtype",
241                                       kwlist, (PyObject *)&py_info)) {
242         return NULL;
243     }
244
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");
248         return NULL;
249     }
250
251     info = (GIEnumInfo *)py_info->info;
252     n_values = g_enum_info_get_n_values (info);
253
254     /* The new memory is zero filled which fulfills the registration
255      * function requirement that the last item is zeroed out as a terminator.
256      */
257     g_flags_values = g_new0 (GFlagsValue, n_values + 1);
258
259     for (i = 0; i < n_values; i++) {
260         GIValueInfo *value_info;
261         GFlagsValue *flags_value;
262         const gchar *name;
263         const gchar *c_identifier;
264
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,
268                                                   "c:identifier");
269
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);
273
274         if (c_identifier == NULL) {
275             flags_value->value_name = flags_value->value_nick;
276         } else {
277             flags_value->value_name = g_strdup (c_identifier);
278         }
279
280         g_base_info_unref ((GIBaseInfo *) value_info);
281     }
282
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
285      */
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);
289
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.
293      */
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];
298
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.
302              */
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);
306         }
307
308         PyErr_Format (PyExc_RuntimeError, "Unable to register flags '%s'", full_name);
309
310         g_free (g_flags_values);
311         g_free (full_name);
312         return NULL;
313     }
314
315     g_free (full_name);
316     return pyg_flags_add (NULL, type_name, NULL, g_type);
317 }
318
319 static void
320 initialize_interface (GTypeInterface *iface, PyTypeObject *pytype)
321 {
322     /* pygobject prints a warning if interface_init is NULL */
323 }
324
325 static PyObject *
326 _wrap_pyg_register_interface_info (PyObject *self, PyObject *args)
327 {
328     PyObject *py_g_type;
329     GType g_type;
330     GInterfaceInfo *info;
331
332     if (!PyArg_ParseTuple (args, "O!:register_interface_info",
333                            &PyGTypeWrapper_Type, &py_g_type)) {
334         return NULL;
335     }
336
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");
340         return NULL;
341     }
342
343     info = g_new0 (GInterfaceInfo, 1);
344     info->interface_init = (GInterfaceInitFunc) initialize_interface;
345
346     pyg_register_interface_info (g_type, info);
347
348     Py_RETURN_NONE;
349 }
350
351 static void
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)
357 {
358     GType ancestor_g_type = 0;
359     int length, i;
360     GIBaseInfo *ancestor_info;
361     GIStructInfo *struct_info;
362     gpointer implementor_class = NULL;
363     gboolean is_interface = FALSE;
364
365     ancestor_info = g_base_info_get_container (vfunc_info);
366     is_interface = g_base_info_get_type (ancestor_info) == GI_INFO_TYPE_INTERFACE;
367
368     ancestor_g_type = g_registered_type_info_get_g_type (
369                           (GIRegisteredTypeInfo *) ancestor_info);
370     implementor_class = g_type_class_ref (implementor_gtype);
371     if (is_interface) {
372         GTypeInstance *implementor_iface_class;
373         implementor_iface_class = g_type_interface_peek (implementor_class,
374                                                          ancestor_g_type);
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));
381             return;
382         }
383
384         *implementor_vtable_ret = implementor_iface_class;
385
386         struct_info = g_interface_info_get_iface_struct ( (GIInterfaceInfo*) ancestor_info);
387     } else {
388         struct_info = g_object_info_get_class_struct ( (GIObjectInfo*) ancestor_info);
389         *implementor_vtable_ret = implementor_class;
390     }
391
392     *implementor_class_ret = implementor_class;
393
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;
398
399         field_info = g_struct_info_get_field (struct_info, i);
400
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);
404             continue;
405         }
406
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;
411             break;
412         }
413
414         g_base_info_unref (type_info);
415         g_base_info_unref (field_info);
416     }
417
418     g_base_info_unref (struct_info);
419 }
420
421 static PyObject *
422 _wrap_pyg_hook_up_vfunc_implementation (PyObject *self, PyObject *args)
423 {
424     PyGIBaseInfo *py_info;
425     PyObject *py_type;
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;
433
434     if (!PyArg_ParseTuple (args, "O!O!O:hook_up_vfunc_implementation",
435                            &PyGIBaseInfo_Type, &py_info,
436                            &PyGTypeWrapper_Type, &py_type,
437                            &py_function))
438         return NULL;
439
440     implementor_gtype = pyg_type_from_object (py_type);
441     g_assert (G_TYPE_IS_CLASSED (implementor_gtype));
442
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;
448         gint offset;
449
450         type_info = g_field_info_get_type (field_info);
451
452         interface_info = g_type_info_get_interface (type_info);
453         g_assert (g_base_info_get_type (interface_info) == GI_INFO_TYPE_CALLBACK);
454
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);
458
459         closure = _pygi_make_native_closure ( (GICallableInfo*) callback_info,
460                                               GI_SCOPE_TYPE_NOTIFIED, py_function, NULL);
461
462         *method_ptr = closure->closure;
463
464         g_base_info_unref (interface_info);
465         g_base_info_unref (type_info);
466         g_base_info_unref (field_info);
467     }
468     g_type_class_unref (implementor_class);
469
470     Py_RETURN_NONE;
471 }
472
473 #if 0
474 /* Not used, left around for future reference */
475 static PyObject *
476 _wrap_pyg_has_vfunc_implementation (PyObject *self, PyObject *args)
477 {
478     PyGIBaseInfo *py_info;
479     PyObject *py_type;
480     PyObject *py_ret;
481     gpointer implementor_class = NULL;
482     gpointer implementor_vtable = NULL;
483     GType implementor_gtype = 0;
484     GIFieldInfo *field_info = NULL;
485
486     if (!PyArg_ParseTuple (args, "O!O!:has_vfunc_implementation",
487                            &PyGIBaseInfo_Type, &py_info,
488                            &PyGTypeWrapper_Type, &py_type))
489         return NULL;
490
491     implementor_gtype = pyg_type_from_object (py_type);
492     g_assert (G_TYPE_IS_CLASSED (implementor_gtype));
493
494     py_ret = Py_False;
495     find_vfunc_info (py_info->info, implementor_gtype, &implementor_class, &implementor_vtable, &field_info);
496     if (field_info != NULL) {
497         gpointer *method_ptr;
498         gint offset;
499
500         offset = g_field_info_get_offset (field_info);
501         method_ptr = G_STRUCT_MEMBER_P (implementor_vtable, offset);
502         if (*method_ptr != NULL) {
503             py_ret = Py_True;
504         }
505
506         g_base_info_unref (field_info);
507     }
508     g_type_class_unref (implementor_class);
509
510     Py_INCREF(py_ret);
511     return py_ret;
512 }
513 #endif
514
515 static PyObject *
516 _wrap_pyg_variant_type_from_string (PyObject *self, PyObject *args)
517 {
518     char *type_string;
519     PyObject *py_type;
520     PyObject *py_variant = NULL;
521
522     if (!PyArg_ParseTuple (args, "s:variant_type_from_string",
523                            &type_string)) {
524         return NULL;
525     }
526
527     py_type = _pygi_type_import_by_name ("GLib", "VariantType");
528
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 */
534
535     return py_variant;
536 }
537
538 static PyObject *
539 _wrap_pyg_source_new (PyObject *self, PyObject *args)
540 {
541     return pyg_source_new ();
542 }
543
544 #define CHUNK_SIZE 8192
545
546 static PyObject*
547 pyg_channel_read(PyObject* self, PyObject *args, PyObject *kwargs)
548 {
549     int max_count = -1;
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;
555
556     if (!PyArg_ParseTuple (args, "Oi:pyg_channel_read", &py_iochannel, &max_count)) {
557         return NULL;
558     }
559     if (!pyg_boxed_check (py_iochannel, G_TYPE_IO_CHANNEL)) {
560         PyErr_SetString(PyExc_TypeError, "first argument is not a GLib.IOChannel");
561         return NULL;
562     }
563         
564     if (max_count == 0)
565         return PYGLIB_PyBytes_FromString("");
566
567     iochannel = pyg_boxed_get (py_iochannel, GIOChannel);
568
569     while (status == G_IO_STATUS_NORMAL
570            && (max_count == -1 || total_read < max_count)) {
571         gsize single_read;
572         char* buf;
573         gsize buf_size;
574         
575         if (max_count == -1) 
576             buf_size = CHUNK_SIZE;
577         else {
578             buf_size = max_count - total_read;
579             if (buf_size > CHUNK_SIZE)
580                 buf_size = CHUNK_SIZE;
581         }
582         
583         if ( ret_obj == NULL ) {
584             ret_obj = PYGLIB_PyBytes_FromStringAndSize((char *)NULL, buf_size);
585             if (ret_obj == NULL)
586                 goto failure;
587         }
588         else if (buf_size + total_read > PYGLIB_PyBytes_Size(ret_obj)) {
589             if (PYGLIB_PyBytes_Resize(&ret_obj, buf_size + total_read) == -1)
590                 goto failure;
591         }
592        
593         buf = PYGLIB_PyBytes_AsString(ret_obj) + total_read;
594
595         Py_BEGIN_ALLOW_THREADS;
596         status = g_io_channel_read_chars (iochannel, buf, buf_size, &single_read, &error);
597         Py_END_ALLOW_THREADS;
598
599         if (pygi_error_check (&error))
600             goto failure;
601         
602         total_read += single_read;
603     }
604         
605     if ( total_read != PYGLIB_PyBytes_Size(ret_obj) ) {
606         if (PYGLIB_PyBytes_Resize(&ret_obj, total_read) == -1)
607             goto failure;
608     }
609
610     return ret_obj;
611
612   failure:
613     Py_XDECREF(ret_obj);
614     return NULL;
615 }
616
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 },
622
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 },
630     { NULL, NULL, 0 }
631 };
632
633 static struct PyGI_API CAPI = {
634   pygi_register_foreign_struct,
635 };
636
637 PYGLIB_MODULE_START(_gi, "_gi")
638 {
639     PyObject *api;
640     PyObject *_glib_module;
641     PyObject *_gobject_module;
642
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
647      */
648     PyEval_InitThreads ();
649
650     _glib_module = pyglib__glib_module_create ();
651     if (_glib_module == NULL) {
652         return PYGLIB_MODULE_ERROR_RETURN;
653     }
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);
661     }
662     PyModule_AddObject (module, "_glib", _glib_module);
663     PyModule_AddStringConstant(module, "__package__", "gi._gi");
664
665     _gobject_module = pyglib__gobject_module_create ();
666     if (_gobject_module == NULL) {
667         return PYGLIB_MODULE_ERROR_RETURN;
668     }
669     if (PY_MAJOR_VERSION < 3) {
670         Py_INCREF (_gobject_module);
671     }
672     PyModule_AddObject (module, "_gobject", _gobject_module);
673     PyModule_AddStringConstant(module, "__package__", "gi._gi");
674
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);
683
684     PyGIWarning = PyErr_NewException ("gi.PyGIWarning", PyExc_Warning, NULL);
685
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.
690      */
691 #if PYGOBJECT_MINOR_VERSION % 2
692     PyGIDeprecationWarning = PyErr_NewException("gi.PyGIDeprecationWarning",
693                                                 PyExc_RuntimeWarning, NULL);
694 #else
695     PyGIDeprecationWarning = PyErr_NewException("gi.PyGIDeprecationWarning",
696                                                 PyExc_DeprecationWarning, NULL);
697 #endif
698
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.
701      */
702     _PyGIDefaultArgPlaceholder = PyObject_New(PyObject, &PyType_Type);
703
704     Py_INCREF (PyGIWarning);
705     PyModule_AddObject (module, "PyGIWarning", PyGIWarning);
706
707     Py_INCREF(PyGIDeprecationWarning);
708     PyModule_AddObject(module, "PyGIDeprecationWarning", PyGIDeprecationWarning);
709
710     api = PYGLIB_CPointer_WrapPointer ( (void *) &CAPI, "gi._API");
711     if (api == NULL) {
712         return PYGLIB_MODULE_ERROR_RETURN;
713     }
714     PyModule_AddObject (module, "_API", api);
715 }
716 PYGLIB_MODULE_END