Remove a duplicate entry in gio.defs
[pygobject.git] / glib / glibmodule.c
blobf794d5dedf1700b1ab7bbed5134950d1bd3a6f4e
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
6 * glibmodule.c: wrapper for the glib 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 Street, Fifth Floor, Boston, MA 02110-1301
21 * USA
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
28 #include <Python.h>
29 #include <glib.h>
30 #include "pyglib.h"
31 #include "pyglib-private.h"
32 #include "pygiochannel.h"
33 #include "pygmaincontext.h"
34 #include "pygmainloop.h"
35 #include "pygoptioncontext.h"
36 #include "pygoptiongroup.h"
37 #include "pygsource.h"
38 #include "pygspawn.h"
40 #define PYGLIB_MAJOR_VERSION PYGOBJECT_MAJOR_VERSION
41 #define PYGLIB_MINOR_VERSION PYGOBJECT_MINOR_VERSION
42 #define PYGLIB_MICRO_VERSION PYGOBJECT_MICRO_VERSION
45 /* ---------------- glib module functions -------------------- */
47 struct _PyGChildData {
48 PyObject *func;
49 PyObject *data;
52 static gint
53 get_handler_priority(gint *priority, PyObject *kwargs)
55 Py_ssize_t len, pos;
56 PyObject *key, *val;
58 /* no keyword args? leave as default */
59 if (kwargs == NULL) return 0;
61 len = PyDict_Size(kwargs);
62 if (len == 0) return 0;
64 if (len != 1) {
65 PyErr_SetString(PyExc_TypeError,
66 "expecting at most one keyword argument");
67 return -1;
69 pos = 0;
70 PyDict_Next(kwargs, &pos, &key, &val);
71 if (!_PyUnicode_Check(key)) {
72 PyErr_SetString(PyExc_TypeError,
73 "keyword argument name is not a string");
74 return -1;
77 if (strcmp(_PyUnicode_AsString(key), "priority") != 0) {
78 PyErr_SetString(PyExc_TypeError,
79 "only 'priority' keyword argument accepted");
80 return -1;
83 *priority = _PyLong_AsLong(val);
84 if (PyErr_Occurred()) {
85 PyErr_Clear();
86 PyErr_SetString(PyExc_ValueError, "could not get priority value");
87 return -1;
89 return 0;
92 static PyObject *
93 pyglib_threads_init(PyObject *unused, PyObject *args, PyObject *kwargs)
95 if (!pyglib_enable_threads())
96 return NULL;
98 Py_INCREF(Py_None);
99 return Py_None;
102 static PyObject *
103 pyglib_idle_add(PyObject *self, PyObject *args, PyObject *kwargs)
105 PyObject *first, *callback, *cbargs = NULL, *data;
106 gint len, priority = G_PRIORITY_DEFAULT_IDLE;
107 guint handler_id;
109 len = PyTuple_Size(args);
110 if (len < 1) {
111 PyErr_SetString(PyExc_TypeError,
112 "idle_add requires at least 1 argument");
113 return NULL;
115 first = PySequence_GetSlice(args, 0, 1);
116 if (!PyArg_ParseTuple(first, "O:idle_add", &callback)) {
117 Py_DECREF(first);
118 return NULL;
120 Py_DECREF(first);
121 if (!PyCallable_Check(callback)) {
122 PyErr_SetString(PyExc_TypeError, "first argument not callable");
123 return NULL;
125 if (get_handler_priority(&priority, kwargs) < 0)
126 return NULL;
128 cbargs = PySequence_GetSlice(args, 1, len);
129 if (cbargs == NULL)
130 return NULL;
132 data = Py_BuildValue("(ON)", callback, cbargs);
133 if (data == NULL)
134 return NULL;
135 handler_id = g_idle_add_full(priority,
136 _pyglib_handler_marshal, data,
137 _pyglib_destroy_notify);
138 return _PyLong_FromLong(handler_id);
142 static PyObject *
143 pyglib_timeout_add(PyObject *self, PyObject *args, PyObject *kwargs)
145 PyObject *first, *callback, *cbargs = NULL, *data;
146 gint len, priority = G_PRIORITY_DEFAULT;
147 guint interval, handler_id;
149 len = PyTuple_Size(args);
150 if (len < 2) {
151 PyErr_SetString(PyExc_TypeError,
152 "timeout_add requires at least 2 args");
153 return NULL;
155 first = PySequence_GetSlice(args, 0, 2);
156 if (!PyArg_ParseTuple(first, "IO:timeout_add", &interval, &callback)) {
157 Py_DECREF(first);
158 return NULL;
160 Py_DECREF(first);
161 if (!PyCallable_Check(callback)) {
162 PyErr_SetString(PyExc_TypeError, "second argument not callable");
163 return NULL;
165 if (get_handler_priority(&priority, kwargs) < 0)
166 return NULL;
168 cbargs = PySequence_GetSlice(args, 2, len);
169 if (cbargs == NULL)
170 return NULL;
172 data = Py_BuildValue("(ON)", callback, cbargs);
173 if (data == NULL)
174 return NULL;
175 handler_id = g_timeout_add_full(priority, interval,
176 _pyglib_handler_marshal, data,
177 _pyglib_destroy_notify);
178 return _PyLong_FromLong(handler_id);
181 static PyObject *
182 pyglib_timeout_add_seconds(PyObject *self, PyObject *args, PyObject *kwargs)
184 PyObject *first, *callback, *cbargs = NULL, *data;
185 gint len, priority = G_PRIORITY_DEFAULT;
186 guint interval, handler_id;
188 len = PyTuple_Size(args);
189 if (len < 2) {
190 PyErr_SetString(PyExc_TypeError,
191 "timeout_add_seconds requires at least 2 args");
192 return NULL;
194 first = PySequence_GetSlice(args, 0, 2);
195 if (!PyArg_ParseTuple(first, "IO:timeout_add_seconds", &interval, &callback)) {
196 Py_DECREF(first);
197 return NULL;
199 Py_DECREF(first);
200 if (!PyCallable_Check(callback)) {
201 PyErr_SetString(PyExc_TypeError, "second argument not callable");
202 return NULL;
204 if (get_handler_priority(&priority, kwargs) < 0)
205 return NULL;
207 cbargs = PySequence_GetSlice(args, 2, len);
208 if (cbargs == NULL)
209 return NULL;
211 data = Py_BuildValue("(ON)", callback, cbargs);
212 if (data == NULL)
213 return NULL;
214 handler_id = g_timeout_add_seconds_full(priority, interval,
215 _pyglib_handler_marshal, data,
216 _pyglib_destroy_notify);
217 return _PyLong_FromLong(handler_id);
220 static gboolean
221 iowatch_marshal(GIOChannel *source,
222 GIOCondition condition,
223 gpointer user_data)
225 PyGILState_STATE state;
226 PyObject *tuple, *func, *firstargs, *args, *ret;
227 gboolean res;
229 g_return_val_if_fail(user_data != NULL, FALSE);
231 state = pyglib_gil_state_ensure();
233 tuple = (PyObject *)user_data;
234 func = PyTuple_GetItem(tuple, 0);
236 /* arg vector is (fd, condtion, *args) */
237 firstargs = Py_BuildValue("(Oi)", PyTuple_GetItem(tuple, 1), condition);
238 args = PySequence_Concat(firstargs, PyTuple_GetItem(tuple, 2));
239 Py_DECREF(firstargs);
241 ret = PyObject_CallObject(func, args);
242 Py_DECREF(args);
243 if (!ret) {
244 PyErr_Print();
245 res = FALSE;
246 } else {
247 if (ret == Py_None) {
248 if (PyErr_Warn(PyExc_Warning,
249 "glib.io_add_watch callback returned None; "
250 "should return True/False")) {
251 PyErr_Print();
254 res = PyObject_IsTrue(ret);
255 Py_DECREF(ret);
258 pyglib_gil_state_release(state);
260 return res;
263 static PyObject *
264 pyglib_io_add_watch(PyObject *self, PyObject *args, PyObject *kwargs)
266 PyObject *first, *pyfd, *callback, *cbargs = NULL, *data;
267 gint fd, priority = G_PRIORITY_DEFAULT, condition;
268 Py_ssize_t len;
269 GIOChannel *iochannel;
270 guint handler_id;
272 len = PyTuple_Size(args);
273 if (len < 3) {
274 PyErr_SetString(PyExc_TypeError,
275 "io_add_watch requires at least 3 args");
276 return NULL;
278 first = PySequence_GetSlice(args, 0, 3);
279 if (!PyArg_ParseTuple(first, "OiO:io_add_watch", &pyfd, &condition,
280 &callback)) {
281 Py_DECREF(first);
282 return NULL;
284 Py_DECREF(first);
285 fd = PyObject_AsFileDescriptor(pyfd);
286 if (fd < 0) {
287 return NULL;
289 if (!PyCallable_Check(callback)) {
290 PyErr_SetString(PyExc_TypeError, "third argument not callable");
291 return NULL;
293 if (get_handler_priority(&priority, kwargs) < 0)
294 return NULL;
296 cbargs = PySequence_GetSlice(args, 3, len);
297 if (cbargs == NULL)
298 return NULL;
299 data = Py_BuildValue("(OON)", callback, pyfd, cbargs);
300 if (data == NULL)
301 return NULL;
302 iochannel = g_io_channel_unix_new(fd);
303 handler_id = g_io_add_watch_full(iochannel, priority, condition,
304 iowatch_marshal, data,
305 (GDestroyNotify)_pyglib_destroy_notify);
306 g_io_channel_unref(iochannel);
308 return _PyLong_FromLong(handler_id);
311 static PyObject *
312 pyglib_source_remove(PyObject *self, PyObject *args)
314 guint tag;
316 if (!PyArg_ParseTuple(args, "i:source_remove", &tag))
317 return NULL;
319 return PyBool_FromLong(g_source_remove(tag));
322 static PyObject *
323 pyglib_main_context_default(PyObject *unused)
325 return pyglib_main_context_new(g_main_context_default());
328 static void
329 child_watch_func(GPid pid, gint status, gpointer data)
331 struct _PyGChildData *child_data = (struct _PyGChildData *) data;
332 PyObject *retval;
333 PyGILState_STATE gil;
335 gil = pyglib_gil_state_ensure();
336 if (child_data->data)
337 retval = PyObject_CallFunction(child_data->func, "iiO", pid, status,
338 child_data->data);
339 else
340 retval = PyObject_CallFunction(child_data->func, "ii", pid, status);
342 if (retval)
343 Py_DECREF(retval);
344 else
345 PyErr_Print();
347 pyglib_gil_state_release(gil);
350 static void
351 child_watch_dnotify(gpointer data)
353 struct _PyGChildData *child_data = (struct _PyGChildData *) data;
354 Py_DECREF(child_data->func);
355 Py_XDECREF(child_data->data);
356 g_slice_free(struct _PyGChildData, child_data);
360 static PyObject *
361 pyglib_child_watch_add(PyObject *unused, PyObject *args, PyObject *kwargs)
363 static char *kwlist[] = { "pid", "function", "data", "priority", NULL };
364 guint id;
365 gint priority = G_PRIORITY_DEFAULT;
366 int pid;
367 PyObject *func, *user_data = NULL;
368 struct _PyGChildData *child_data;
370 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
371 "iO|Oi:glib.child_watch_add", kwlist,
372 &pid, &func, &user_data, &priority))
373 return NULL;
374 if (!PyCallable_Check(func)) {
375 PyErr_SetString(PyExc_TypeError,
376 "glib.child_watch_add: second argument must be callable");
377 return NULL;
380 child_data = g_slice_new(struct _PyGChildData);
381 child_data->func = func;
382 child_data->data = user_data;
383 Py_INCREF(child_data->func);
384 if (child_data->data)
385 Py_INCREF(child_data->data);
386 id = g_child_watch_add_full(priority, pid, child_watch_func,
387 child_data, child_watch_dnotify);
388 return _PyLong_FromLong(id);
391 static PyObject *
392 pyglib_markup_escape_text(PyObject *unused, PyObject *args, PyObject *kwargs)
394 static char *kwlist[] = { "text", NULL };
395 char *text_in, *text_out;
396 Py_ssize_t text_size;
397 PyObject *retval;
399 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
400 "s#:glib.markup_escape_text", kwlist,
401 &text_in, &text_size))
402 return NULL;
404 text_out = g_markup_escape_text(text_in, text_size);
405 retval = _PyUnicode_FromString(text_out);
406 g_free(text_out);
407 return retval;
410 static PyObject *
411 pyglib_get_current_time(PyObject *unused)
413 GTimeVal timeval;
415 g_get_current_time(&timeval);
416 return pyglib_float_from_timeval(timeval);
419 static PyObject*
420 pyglib_get_user_cache_dir(PyObject *self)
422 const char *path = g_get_user_cache_dir();
424 if (path)
425 return _PyUnicode_FromString(path);
426 else {
427 Py_INCREF(Py_None);
428 return Py_None;
432 static PyObject*
433 pyglib_get_user_config_dir(PyObject *self)
435 const char *path = g_get_user_config_dir();
437 if (path)
438 return _PyUnicode_FromString(path);
439 else {
440 Py_INCREF(Py_None);
441 return Py_None;
445 static PyObject*
446 pyglib_get_user_data_dir(PyObject *self)
448 const char *path = g_get_user_data_dir();
450 if (path)
451 return _PyUnicode_FromString(path);
452 else {
453 Py_INCREF(Py_None);
454 return Py_None;
458 static PyObject *
459 pyglib_get_user_special_dir(PyObject *unused, PyObject *args, PyObject *kwargs)
461 static char *kwlist[] = { "directory", NULL };
462 guint directory;
463 const char *path;
465 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
466 "i:glib.get_user_special_dir", kwlist,
467 &directory))
468 return NULL;
470 path = g_get_user_special_dir(directory);
471 if (path)
472 return _PyUnicode_FromString(path);
473 else {
474 Py_INCREF(Py_None);
475 return Py_None;
479 static PyObject *
480 pyglib_main_depth(PyObject *unused)
482 return _PyLong_FromLong(g_main_depth());
485 static PyObject *
486 pyglib_filename_display_name(PyObject *self, PyObject *args)
488 PyObject *py_display_name;
489 char *filename, *display_name;
491 if (!PyArg_ParseTuple(args, "s:glib.filename_display_name",
492 &filename))
493 return NULL;
495 display_name = g_filename_display_name(filename);
496 py_display_name = PyUnicode_DecodeUTF8(display_name,
497 strlen(display_name), NULL);
498 g_free(display_name);
499 return py_display_name;
502 static PyObject *
503 pyglib_filename_display_basename(PyObject *self, PyObject *args)
505 PyObject *py_display_basename;
506 char *filename, *display_basename;
508 if (!PyArg_ParseTuple(args, "s:glib.filename_display_basename",
509 &filename))
510 return NULL;
512 display_basename = g_filename_display_basename(filename);
513 py_display_basename = PyUnicode_DecodeUTF8(display_basename,
514 strlen(display_basename), NULL);
515 g_free(display_basename);
516 return py_display_basename;
519 static PyObject *
520 pyglib_filename_from_utf8(PyObject *self, PyObject *args)
522 char *filename, *utf8string;
523 Py_ssize_t utf8string_len;
524 gsize bytes_written;
525 GError *error = NULL;
526 PyObject *py_filename;
528 if (!PyArg_ParseTuple(args, "s#:glib.filename_from_utf8",
529 &utf8string, &utf8string_len))
530 return NULL;
532 filename = g_filename_from_utf8(utf8string, utf8string_len,
533 NULL, &bytes_written, &error);
534 if (pyglib_error_check(&error)) {
535 g_free(filename);
536 return NULL;
538 py_filename = _PyUnicode_FromStringAndSize(filename, bytes_written);
539 g_free(filename);
540 return py_filename;
544 static PyObject*
545 pyglib_get_application_name(PyObject *self)
547 const char *name;
549 name = g_get_application_name();
550 if (!name) {
551 Py_INCREF(Py_None);
552 return Py_None;
554 return _PyUnicode_FromString(name);
557 static PyObject*
558 pyglib_set_application_name(PyObject *self, PyObject *arg)
560 if (!PyString_Check(arg)) {
561 PyErr_Format(PyExc_TypeError,
562 "first argument must be a string, not '%s'",
563 PyString_AS_STRING(PyObject_Repr(arg)));
564 return NULL;
566 g_set_application_name(PyString_AS_STRING(arg));
567 Py_INCREF(Py_None);
568 return Py_None;
571 static PyObject*
572 pyglib_get_prgname(PyObject *self)
574 char *name;
576 name = g_get_prgname();
577 if (!name) {
578 Py_INCREF(Py_None);
579 return Py_None;
581 return _PyUnicode_FromString(name);
584 static PyObject*
585 pyglib_set_prgname(PyObject *self, PyObject *arg)
587 if (!PyString_Check(arg)) {
588 PyErr_Format(PyExc_TypeError,
589 "first argument must be a string, not '%s'",
590 PyString_AS_STRING(PyObject_Repr(arg)));
591 return NULL;
593 g_set_prgname(PyString_AS_STRING(arg));
594 Py_INCREF(Py_None);
595 return Py_None;
598 static PyObject *
599 pyglib_find_program_in_path(PyObject *unused, PyObject *args, PyObject *kwargs)
601 static char *kwlist[] = { "program", NULL };
602 char *program, *ret;
603 PyObject *retval;
605 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
606 "s:glib.find_program_in_path", kwlist,
607 &program))
608 return NULL;
610 ret = g_find_program_in_path(program);
611 retval = _PyUnicode_FromString(ret);
612 g_free(ret);
613 return retval;
616 static PyMethodDef _glib_functions[] = {
617 { "threads_init",
618 (PyCFunction) pyglib_threads_init, METH_NOARGS,
619 "threads_init()\n"
620 "Initialize GLib for use from multiple threads. If you also use GTK+\n"
621 "itself (i.e. GUI, not just PyGObject), use gtk.gdk.threads_init()\n"
622 "instead." },
623 { "idle_add",
624 (PyCFunction)pyglib_idle_add, METH_VARARGS|METH_KEYWORDS,
625 "idle_add(callable, user_data=None, priority=None) -> source id\n"
626 " callable receives (user_data)\n"
627 "Adds a callable to be called whenever there are no higher priority\n"
628 "events pending to the default main loop." },
629 { "timeout_add",
630 (PyCFunction)pyglib_timeout_add, METH_VARARGS|METH_KEYWORDS,
631 "timeout_add(interval, callable, user_data=None,\n"
632 " priority=None) -> source id\n"
633 " callable receives (user_data)\n"
634 "Sets a callable be called repeatedly until it returns False." },
635 { "timeout_add_seconds",
636 (PyCFunction)pyglib_timeout_add_seconds, METH_VARARGS|METH_KEYWORDS,
637 "timeout_add(interval, callable, user_data=None,\n"
638 " priority=None) -> source_id\n"
639 " callable receives (user_data)\n"
640 "Sets a callable be called repeatedly until it returns False.\n"
641 "Use this if you want to have a timer in the \"seconds\" range\n"
642 "and do not care about the exact time of the first call of the\n"
643 "timer, use this for more efficient system power usage." },
644 { "io_add_watch",
645 (PyCFunction)pyglib_io_add_watch, METH_VARARGS|METH_KEYWORDS,
646 "io_add_watch(fd, condition, callback, user_data=None) -> source id\n"
647 " callable receives (fd, condition, user_data)\n"
648 "Arranges for the fd to be monitored by the main loop for the\n"
649 "specified condition. Condition is a combination of glib.IO_IN,\n"
650 "glib.IO_OUT, glib.IO_PRI, gio.IO_ERR and gio.IO_HUB.\n" },
651 { "child_watch_add",
652 (PyCFunction)pyglib_child_watch_add, METH_VARARGS|METH_KEYWORDS,
653 "child_watch_add(pid, callable, user_data=None,\n"
654 "priority=None) -> source id\n"
655 " callable receives (pid, condition, user_data)\n"
656 "Sets the function specified by function to be called with the user\n"
657 "data specified by data when the child indicated by pid exits.\n"
658 "Condition is a combination of glib.IO_IN, glib.IO_OUT, glib.IO_PRI,\n"
659 "gio.IO_ERR and gio.IO_HUB." },
660 { "source_remove",
661 (PyCFunction)pyglib_source_remove, METH_VARARGS,
662 "source_remove(source_id) -> True if removed\n"
663 "Removes the event source specified by source id as returned by the\n"
664 "glib.idle_add(), glib.timeout_add() or glib.io_add_watch()\n"
665 "functions." },
666 { "spawn_async",
667 (PyCFunction)pyglib_spawn_async, METH_VARARGS|METH_KEYWORDS,
668 "spawn_async(argv, envp=None, working_directory=None,\n"
669 " flags=0, child_setup=None, user_data=None,\n"
670 " standard_input=None, standard_output=None,\n"
671 " standard_error=None) -> (pid, stdin, stdout, stderr)\n"
672 "Execute a child program asynchronously within a glib.MainLoop()\n"
673 "See the reference manual for a complete reference." },
674 { "main_context_default",
675 (PyCFunction)pyglib_main_context_default, METH_NOARGS,
676 "main_context_default() -> a main context\n"
677 "Returns the default main context. This is the main context used\n"
678 "for main loop functions when a main loop is not explicitly specified." },
679 { "main_depth",
680 (PyCFunction)pyglib_main_depth, METH_NOARGS,
681 "main_depth() -> stack depth\n"
682 "Returns the depth of the stack of calls in the main context." },
683 { "filename_display_name",
684 (PyCFunction)pyglib_filename_display_name, METH_VARARGS },
685 { "filename_display_basename",
686 (PyCFunction)pyglib_filename_display_basename, METH_VARARGS },
687 { "filename_from_utf8",
688 (PyCFunction)pyglib_filename_from_utf8, METH_VARARGS },
689 { "get_application_name",
690 (PyCFunction)pyglib_get_application_name, METH_NOARGS },
691 { "set_application_name",
692 (PyCFunction)pyglib_set_application_name, METH_O },
693 { "get_prgname",
694 (PyCFunction)pyglib_get_prgname, METH_NOARGS },
695 { "set_prgname",
696 (PyCFunction)pyglib_set_prgname, METH_O },
697 { "get_current_time",
698 (PyCFunction)pyglib_get_current_time, METH_NOARGS },
699 { "get_user_cache_dir",
700 (PyCFunction)pyglib_get_user_cache_dir, METH_NOARGS },
701 { "get_user_config_dir",
702 (PyCFunction)pyglib_get_user_config_dir, METH_NOARGS },
703 { "get_user_data_dir",
704 (PyCFunction)pyglib_get_user_data_dir, METH_NOARGS },
705 { "get_user_special_dir",
706 (PyCFunction)pyglib_get_user_special_dir, METH_VARARGS|METH_KEYWORDS },
707 { "markup_escape_text",
708 (PyCFunction)pyglib_markup_escape_text, METH_VARARGS|METH_KEYWORDS },
709 { "find_program_in_path",
710 (PyCFunction)pyglib_find_program_in_path, METH_VARARGS|METH_KEYWORDS },
711 { NULL, NULL, 0 }
714 /* ----------------- glib module initialisation -------------- */
716 static struct _PyGLib_Functions pyglib_api = {
717 FALSE, /* threads_enabled */
718 NULL, /* gerror_exception */
719 NULL, /* block_threads */
720 NULL /* unblock_threads */
723 static void
724 pyglib_register_api(PyObject *d)
726 PyObject *o;
728 /* for addon libraries ... */
729 PyDict_SetItemString(d, "_PyGLib_API",
730 o=PyCObject_FromVoidPtr(&pyglib_api,NULL));
731 Py_DECREF(o);
733 pyglib_init_internal(o);
736 static void
737 pyglib_register_error(PyObject *d)
739 PyObject *dict;
740 PyObject *gerror_class;
741 dict = PyDict_New();
742 /* This is a hack to work around the deprecation warning of
743 * BaseException.message in Python 2.6+.
744 * GError has also an "message" attribute.
746 PyDict_SetItemString(dict, "message", Py_None);
747 gerror_class = PyErr_NewException("glib.GError", PyExc_RuntimeError, dict);
748 Py_DECREF(dict);
750 PyDict_SetItemString(d, "GError", gerror_class);
751 pyglib_api.gerror_exception = gerror_class;
754 static void
755 pyglib_register_version_tuples(PyObject *d)
757 PyObject *o;
759 /* glib version */
760 o = Py_BuildValue("(iii)", glib_major_version, glib_minor_version,
761 glib_micro_version);
762 PyDict_SetItemString(d, "glib_version", o);
763 Py_DECREF(o);
765 /* pyglib version */
766 o = Py_BuildValue("(iii)",
767 PYGLIB_MAJOR_VERSION,
768 PYGLIB_MINOR_VERSION,
769 PYGLIB_MICRO_VERSION);
770 PyDict_SetItemString(d, "pyglib_version", o);
771 Py_DECREF(o);
774 static void
775 pyglib_register_constants(PyObject *m)
777 PyModule_AddIntConstant(m, "SPAWN_LEAVE_DESCRIPTORS_OPEN",
778 G_SPAWN_LEAVE_DESCRIPTORS_OPEN);
779 PyModule_AddIntConstant(m, "SPAWN_DO_NOT_REAP_CHILD",
780 G_SPAWN_DO_NOT_REAP_CHILD);
781 PyModule_AddIntConstant(m, "SPAWN_SEARCH_PATH",
782 G_SPAWN_SEARCH_PATH);
783 PyModule_AddIntConstant(m, "SPAWN_STDOUT_TO_DEV_NULL",
784 G_SPAWN_STDOUT_TO_DEV_NULL);
785 PyModule_AddIntConstant(m, "SPAWN_STDERR_TO_DEV_NULL",
786 G_SPAWN_STDERR_TO_DEV_NULL);
787 PyModule_AddIntConstant(m, "SPAWN_CHILD_INHERITS_STDIN",
788 G_SPAWN_CHILD_INHERITS_STDIN);
789 PyModule_AddIntConstant(m, "SPAWN_FILE_AND_ARGV_ZERO",
790 G_SPAWN_FILE_AND_ARGV_ZERO);
792 PyModule_AddIntConstant(m, "PRIORITY_HIGH",
793 G_PRIORITY_HIGH);
794 PyModule_AddIntConstant(m, "PRIORITY_DEFAULT",
795 G_PRIORITY_DEFAULT);
796 PyModule_AddIntConstant(m, "PRIORITY_HIGH_IDLE",
797 G_PRIORITY_HIGH_IDLE);
798 PyModule_AddIntConstant(m, "PRIORITY_DEFAULT_IDLE",
799 G_PRIORITY_DEFAULT_IDLE);
800 PyModule_AddIntConstant(m, "PRIORITY_LOW",
801 G_PRIORITY_LOW);
803 PyModule_AddIntConstant(m, "IO_IN", G_IO_IN);
804 PyModule_AddIntConstant(m, "IO_OUT", G_IO_OUT);
805 PyModule_AddIntConstant(m, "IO_PRI", G_IO_PRI);
806 PyModule_AddIntConstant(m, "IO_ERR", G_IO_ERR);
807 PyModule_AddIntConstant(m, "IO_HUP", G_IO_HUP);
808 PyModule_AddIntConstant(m, "IO_NVAL", G_IO_NVAL);
810 PyModule_AddIntConstant(m, "IO_STATUS_ERROR",
811 G_IO_STATUS_ERROR);
812 PyModule_AddIntConstant(m, "IO_STATUS_NORMAL",
813 G_IO_STATUS_NORMAL);
814 PyModule_AddIntConstant(m, "IO_STATUS_EOF",
815 G_IO_STATUS_EOF);
816 PyModule_AddIntConstant(m, "IO_STATUS_AGAIN",
817 G_IO_STATUS_AGAIN);
818 PyModule_AddIntConstant(m, "IO_FLAG_APPEND",
819 G_IO_FLAG_APPEND);
820 PyModule_AddIntConstant(m, "IO_FLAG_NONBLOCK",
821 G_IO_FLAG_NONBLOCK);
822 PyModule_AddIntConstant(m, "IO_FLAG_IS_READABLE",
823 G_IO_FLAG_IS_READABLE);
824 PyModule_AddIntConstant(m, "IO_FLAG_IS_WRITEABLE",
825 G_IO_FLAG_IS_WRITEABLE);
826 PyModule_AddIntConstant(m, "IO_FLAG_IS_SEEKABLE",
827 G_IO_FLAG_IS_SEEKABLE);
828 PyModule_AddIntConstant(m, "IO_FLAG_MASK",
829 G_IO_FLAG_MASK);
830 PyModule_AddIntConstant(m, "IO_FLAG_GET_MASK",
831 G_IO_FLAG_GET_MASK);
832 PyModule_AddIntConstant(m, "IO_FLAG_SET_MASK",
833 G_IO_FLAG_SET_MASK);
835 PyModule_AddIntConstant(m, "OPTION_FLAG_HIDDEN",
836 G_OPTION_FLAG_HIDDEN);
837 PyModule_AddIntConstant(m, "OPTION_FLAG_IN_MAIN",
838 G_OPTION_FLAG_IN_MAIN);
839 PyModule_AddIntConstant(m, "OPTION_FLAG_REVERSE",
840 G_OPTION_FLAG_REVERSE);
841 PyModule_AddIntConstant(m, "OPTION_FLAG_NO_ARG",
842 G_OPTION_FLAG_NO_ARG);
843 PyModule_AddIntConstant(m, "OPTION_FLAG_FILENAME",
844 G_OPTION_FLAG_FILENAME);
845 PyModule_AddIntConstant(m, "OPTION_FLAG_OPTIONAL_ARG",
846 G_OPTION_FLAG_OPTIONAL_ARG);
847 PyModule_AddIntConstant(m, "OPTION_FLAG_NOALIAS",
848 G_OPTION_FLAG_NOALIAS);
850 PyModule_AddIntConstant(m, "OPTION_ERROR_UNKNOWN_OPTION",
851 G_OPTION_ERROR_UNKNOWN_OPTION);
852 PyModule_AddIntConstant(m, "OPTION_ERROR_BAD_VALUE",
853 G_OPTION_ERROR_BAD_VALUE);
854 PyModule_AddIntConstant(m, "OPTION_ERROR_FAILED",
855 G_OPTION_ERROR_FAILED);
857 PyModule_AddIntConstant(m, "USER_DIRECTORY_DESKTOP",
858 G_USER_DIRECTORY_DESKTOP);
859 PyModule_AddIntConstant(m, "USER_DIRECTORY_DOCUMENTS",
860 G_USER_DIRECTORY_DOCUMENTS);
861 PyModule_AddIntConstant(m, "USER_DIRECTORY_DOWNLOAD",
862 G_USER_DIRECTORY_DOWNLOAD);
863 PyModule_AddIntConstant(m, "USER_DIRECTORY_MUSIC",
864 G_USER_DIRECTORY_MUSIC);
865 PyModule_AddIntConstant(m, "USER_DIRECTORY_PICTURES",
866 G_USER_DIRECTORY_PICTURES);
867 PyModule_AddIntConstant(m, "USER_DIRECTORY_PUBLIC_SHARE",
868 G_USER_DIRECTORY_PUBLIC_SHARE);
869 PyModule_AddIntConstant(m, "USER_DIRECTORY_TEMPLATES",
870 G_USER_DIRECTORY_TEMPLATES);
871 PyModule_AddIntConstant(m, "USER_DIRECTORY_VIDEOS",
872 G_USER_DIRECTORY_VIDEOS);
874 PyModule_AddStringConstant(m, "OPTION_REMAINING",
875 G_OPTION_REMAINING);
876 PyModule_AddStringConstant(m, "OPTION_ERROR",
877 (char*) g_quark_to_string(G_OPTION_ERROR));
880 PYGLIB_MODULE_START(_glib, "glib._glib")
882 PyObject *d = PyModule_GetDict(module);
884 pyglib_register_constants(module);
885 pyglib_register_api(d);
886 pyglib_register_error(d);
887 pyglib_register_version_tuples(d);
888 pyglib_iochannel_register_types(d);
889 pyglib_mainloop_register_types(d);
890 pyglib_maincontext_register_types(d);
891 pyglib_source_register_types(d);
892 pyglib_spawn_register_types(d);
893 pyglib_option_context_register_types(d);
894 pyglib_option_group_register_types(d);
896 PYGLIB_MODULE_END