Imported Upstream version 3.7.3
[platform/upstream/python-gobject.git] / gi / _glib / glibmodule.c
1 /* -*- Mode: C; c-set-style: python; c-basic-offset: 4  -*-
2  * pyglib - Python bindings for GLib toolkit.
3  * Copyright (C) 1998-2003  James Henstridge
4  *               2004-2008  Johan Dahlin
5  *
6  *   glibmodule.c: wrapper for the glib 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 Street, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <Python.h>
29 #include <glib.h>
30 #include "pyglib.h"
31 #include "pyglib-private.h"
32 #include "pygoptioncontext.h"
33 #include "pygoptiongroup.h"
34 #include "pygsource.h"
35 #include "pygspawn.h"
36
37 /* ---------------- glib module functions -------------------- */
38
39 static PyObject *
40 pyglib_threads_init(PyObject *unused, PyObject *args, PyObject *kwargs)
41 {
42     if (!pyglib_enable_threads())
43         return NULL;
44
45     Py_INCREF(Py_None);
46     return Py_None;
47 }
48
49 static PyMethodDef _glib_functions[] = {
50     { "threads_init",
51       (PyCFunction) pyglib_threads_init, METH_NOARGS,
52       "threads_init()\n"
53       "Initialize GLib for use from multiple threads. If you also use GTK+\n"
54       "itself (i.e. GUI, not just PyGObject), use gtk.gdk.threads_init()\n"
55       "instead." },
56     { "spawn_async",
57       (PyCFunction)pyglib_spawn_async, METH_VARARGS|METH_KEYWORDS,
58       "spawn_async(argv, envp=None, working_directory=None,\n"
59       "            flags=0, child_setup=None, user_data=None,\n"
60       "            standard_input=None, standard_output=None,\n"
61       "            standard_error=None) -> (pid, stdin, stdout, stderr)\n"
62       "Execute a child program asynchronously within a glib.MainLoop()\n"
63       "See the reference manual for a complete reference." },
64     { NULL, NULL, 0 }
65 };
66
67 /* ----------------- glib module initialisation -------------- */
68
69 static struct _PyGLib_Functions pyglib_api = {
70     FALSE, /* threads_enabled */
71     NULL,  /* gerror_exception */
72     NULL,  /* block_threads */
73     NULL,  /* unblock_threads */
74     NULL,  /* pyg_main_context_new */
75     pyg_option_context_new,
76     pyg_option_group_new,
77 };
78
79 static void
80 pyglib_register_api(PyObject *d)
81 {
82     PyObject *o;
83
84     /* for addon libraries ... */
85     PyDict_SetItemString(d, "_PyGLib_API",
86                          o=PYGLIB_CPointer_WrapPointer(&pyglib_api,"gi._glib._PyGLib_API"));
87     Py_DECREF(o);
88     
89     pyglib_init_internal(o);
90 }
91
92 static void
93 pyglib_register_error(PyObject *d)
94 {
95     PyObject *dict;
96     PyObject *gerror_class;
97     dict = PyDict_New();
98     /* This is a hack to work around the deprecation warning of
99      * BaseException.message in Python 2.6+.
100      * GError has also an "message" attribute.
101      */
102     PyDict_SetItemString(dict, "message", Py_None);
103     gerror_class = PyErr_NewException("gi._glib.GError", PyExc_RuntimeError, dict);
104     Py_DECREF(dict);
105
106     PyDict_SetItemString(d, "GError", gerror_class);
107     pyglib_api.gerror_exception = gerror_class;
108 }
109
110 PYGLIB_MODULE_START(_glib, "_glib")
111 {
112     PyObject *d = PyModule_GetDict(module);
113
114     pyglib_register_api(d);
115     pyglib_register_error(d);
116     pyglib_source_register_types(d);
117     pyglib_spawn_register_types(d);
118     pyglib_option_context_register_types(d);
119     pyglib_option_group_register_types(d);
120 }
121 PYGLIB_MODULE_END