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 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
29 #include "pyglib-private.h"
30 #include "pygoptioncontext.h"
31 #include "pygoptiongroup.h"
33 static struct _PyGLib_Functions *_PyGLib_API;
34 static int pyglib_thread_state_tls_key;
35 static PyObject *exception_table = NULL;
40 PyObject *glib, *cobject;
42 glib = PyImport_ImportModule("gi._glib");
44 if (PyErr_Occurred()) {
45 PyObject *type, *value, *traceback;
46 PyObject *py_orig_exc;
47 PyErr_Fetch(&type, &value, &traceback);
48 py_orig_exc = PyObject_Repr(value);
51 Py_XDECREF(traceback);
52 PyErr_Format(PyExc_ImportError,
53 "could not import gi._glib (error was: %s)",
54 PYGLIB_PyUnicode_AsString(py_orig_exc));
55 Py_DECREF(py_orig_exc);
57 PyErr_SetString(PyExc_ImportError,
58 "could not import gi._glib (no error given)");
63 cobject = PyObject_GetAttrString(glib, "_PyGLib_API");
64 if (cobject && PYGLIB_CPointer_Check(cobject))
65 _PyGLib_API = (struct _PyGLib_Functions *) PYGLIB_CPointer_GetPointer(cobject, "gi._glib._PyGLib_API");
67 PyErr_SetString(PyExc_ImportError,
68 "could not import gi._glib (could not find _PyGLib_API object)");
75 pyglib_init_internal(PyObject *api)
77 _PyGLib_API = (struct _PyGLib_Functions *) PYGLIB_CPointer_GetPointer(api, "gi._glib._PyGLib_API");
81 pyglib_threads_enabled(void)
83 g_return_val_if_fail (_PyGLib_API != NULL, FALSE);
85 return _PyGLib_API->threads_enabled;
89 pyglib_gil_state_ensure(void)
91 g_return_val_if_fail (_PyGLib_API != NULL, PyGILState_LOCKED);
93 if (!_PyGLib_API->threads_enabled)
94 return PyGILState_LOCKED;
96 #ifdef DISABLE_THREADING
97 return PyGILState_LOCKED;
99 return PyGILState_Ensure();
104 pyglib_gil_state_release(PyGILState_STATE state)
106 g_return_if_fail (_PyGLib_API != NULL);
108 if (!_PyGLib_API->threads_enabled)
111 #ifndef DISABLE_THREADING
112 PyGILState_Release(state);
117 * pyglib_enable_threads:
119 * Returns: TRUE if threading is enabled, FALSE otherwise.
122 #ifdef DISABLE_THREADING
124 pyglib_enable_threads(void)
126 PyErr_SetString(PyExc_RuntimeError,
127 "pyglib threading disabled at compile time");
132 _pyglib_notify_on_enabling_threads(PyGLibThreadsEnabledFunc callback)
134 /* Ignore, threads cannot be enabled. */
139 static GSList *thread_enabling_callbacks = NULL;
141 /* Enable threading; note that the GIL must be held by the current
142 * thread when this function is called
145 pyglib_enable_threads(void)
149 g_return_val_if_fail (_PyGLib_API != NULL, FALSE);
151 if (_PyGLib_API->threads_enabled)
154 PyEval_InitThreads();
155 _PyGLib_API->threads_enabled = TRUE;
156 pyglib_thread_state_tls_key = PyThread_create_key();
158 for (callback = thread_enabling_callbacks; callback; callback = callback->next)
159 ((PyGLibThreadsEnabledFunc) callback->data) ();
161 g_slist_free(thread_enabling_callbacks);
166 _pyglib_notify_on_enabling_threads(PyGLibThreadsEnabledFunc callback)
168 if (callback && !pyglib_threads_enabled())
169 thread_enabling_callbacks = g_slist_append(thread_enabling_callbacks, callback);
174 * pyglib_block_threads:
178 pyglib_block_threads(void)
180 g_return_if_fail (_PyGLib_API != NULL);
182 if (_PyGLib_API->block_threads != NULL)
183 (* _PyGLib_API->block_threads)();
187 * pyglib_unblock_threads:
191 pyglib_unblock_threads(void)
193 g_return_if_fail (_PyGLib_API != NULL);
194 if (_PyGLib_API->unblock_threads != NULL)
195 (* _PyGLib_API->unblock_threads)();
199 * pyglib_set_thread_block_funcs:
201 * hooks to register handlers for getting GDK threads to cooperate
202 * with python threading
205 pyglib_set_thread_block_funcs (PyGLibThreadBlockFunc block_threads_func,
206 PyGLibThreadBlockFunc unblock_threads_func)
208 g_return_if_fail (_PyGLib_API != NULL);
210 _PyGLib_API->block_threads = block_threads_func;
211 _PyGLib_API->unblock_threads = unblock_threads_func;
215 * pyglib_error_marshal:
216 * @error: a pointer to the GError.
218 * Checks to see if @error has been set. If @error has been set, then a
219 * GLib.GError Python exception object is returned (but not raised).
221 * Returns: a GLib.GError Python exception object, or NULL.
224 pyglib_error_marshal (GError **error)
226 PyGILState_STATE state;
228 PyObject *exc_instance;
231 g_return_val_if_fail(error != NULL, NULL);
236 state = pyglib_gil_state_ensure();
238 exc_type = _PyGLib_API->gerror_exception;
239 if (exception_table != NULL)
242 item = PyDict_GetItem(exception_table, PYGLIB_PyLong_FromLong((*error)->domain));
247 exc_instance = PyObject_CallFunction(exc_type, "z", (*error)->message);
249 if ((*error)->domain) {
250 PyObject_SetAttrString(exc_instance, "domain",
251 d=PYGLIB_PyUnicode_FromString(g_quark_to_string((*error)->domain)));
255 PyObject_SetAttrString(exc_instance, "domain", Py_None);
257 PyObject_SetAttrString(exc_instance, "code",
258 d=PYGLIB_PyLong_FromLong((*error)->code));
261 if ((*error)->message) {
262 PyObject_SetAttrString(exc_instance, "message",
263 d=PYGLIB_PyUnicode_FromString((*error)->message));
266 PyObject_SetAttrString(exc_instance, "message", Py_None);
269 pyglib_gil_state_release(state);
275 * pyglib_error_check:
276 * @error: a pointer to the GError.
278 * Checks to see if the GError has been set. If the error has been
279 * set, then the glib.GError Python exception will be raised, and
280 * the GError cleared.
282 * Returns: True if an error was set.
285 pyglib_error_check(GError **error)
287 PyGILState_STATE state;
288 PyObject *exc_instance;
290 g_return_val_if_fail(error != NULL, FALSE);
294 state = pyglib_gil_state_ensure();
296 exc_instance = pyglib_error_marshal (error);
297 PyErr_SetObject(_PyGLib_API->gerror_exception, exc_instance);
298 Py_DECREF(exc_instance);
299 g_clear_error(error);
301 pyglib_gil_state_release(state);
307 * pyglib_gerror_exception_check:
308 * @error: a standard GLib GError ** output parameter
310 * Checks to see if a GError exception has been raised, and if so
311 * translates the python exception to a standard GLib GError. If the
312 * raised exception is not a GError then PyErr_Print() is called.
314 * Returns: 0 if no exception has been raised, -1 if it is a
315 * valid glib.GError, -2 otherwise.
318 pyglib_gerror_exception_check(GError **error)
320 PyObject *type, *value, *traceback;
321 PyObject *py_message, *py_domain, *py_code;
322 const char *bad_gerror_message;
324 PyErr_Fetch(&type, &value, &traceback);
327 PyErr_NormalizeException(&type, &value, &traceback);
329 PyErr_Restore(type, value, traceback);
334 !PyErr_GivenExceptionMatches(type,
335 (PyObject *) _PyGLib_API->gerror_exception)) {
336 PyErr_Restore(type, value, traceback);
341 Py_XDECREF(traceback);
343 py_message = PyObject_GetAttrString(value, "message");
344 if (!py_message || !PYGLIB_PyUnicode_Check(py_message)) {
345 bad_gerror_message = "gi._glib.GError instances must have a 'message' string attribute";
346 Py_XDECREF(py_message);
350 py_domain = PyObject_GetAttrString(value, "domain");
351 if (!py_domain || !PYGLIB_PyUnicode_Check(py_domain)) {
352 bad_gerror_message = "gi._glib.GError instances must have a 'domain' string attribute";
353 Py_DECREF(py_message);
354 Py_XDECREF(py_domain);
358 py_code = PyObject_GetAttrString(value, "code");
359 if (!py_code || !PYGLIB_PyLong_Check(py_code)) {
360 bad_gerror_message = "gi._glib.GError instances must have a 'code' int attribute";
361 Py_DECREF(py_message);
362 Py_DECREF(py_domain);
367 g_set_error(error, g_quark_from_string(PYGLIB_PyUnicode_AsString(py_domain)),
368 PYGLIB_PyLong_AsLong(py_code), "%s", PYGLIB_PyUnicode_AsString(py_message));
370 Py_DECREF(py_message);
372 Py_DECREF(py_domain);
377 g_set_error(error, g_quark_from_static_string("pyglib"), 0, "%s", bad_gerror_message);
378 PyErr_SetString(PyExc_ValueError, bad_gerror_message);
384 * pyglib_register_exception_for_domain:
385 * @name: name of the exception
386 * @error_domain: error domain
388 * Registers a new glib.GError exception subclass called #name for
389 * a specific #domain. This exception will be raised when a GError
390 * of the same domain is passed in to pyglib_error_check().
392 * Returns: the new exception
395 pyglib_register_exception_for_domain(gchar *name,
400 exception = PyErr_NewException(name, _PyGLib_API->gerror_exception, NULL);
402 if (exception_table == NULL)
403 exception_table = PyDict_New();
405 PyDict_SetItem(exception_table,
406 PYGLIB_PyLong_FromLong(error_domain),
413 * pyg_option_group_transfer_group:
414 * @group: a GOptionGroup wrapper
416 * This is used to transfer the GOptionGroup to a GOptionContext. After this
417 * is called, the calle must handle the release of the GOptionGroup.
419 * When #NULL is returned, the GOptionGroup was already transfered.
421 * Returns: Either #NULL or the wrapped GOptionGroup.
424 pyglib_option_group_transfer_group(PyObject *obj)
426 PyGOptionGroup *self = (PyGOptionGroup*)obj;
428 if (self->is_in_context)
431 self->is_in_context = TRUE;
433 /* Here we increase the reference count of the PyGOptionGroup, because now
434 * the GOptionContext holds an reference to us (it is the userdata passed
435 * to g_option_group_new().
437 * The GOptionGroup is freed with the GOptionContext.
439 * We set it here because if we would do this in the init method we would
440 * hold two references and the PyGOptionGroup would never be freed.
448 * pyglib_option_group_new:
449 * @group: a GOptionGroup
451 * The returned GOptionGroup can't be used to set any hooks, translation domains
452 * or add entries. It's only intend is, to use for GOptionContext.add_group().
454 * Returns: the GOptionGroup wrapper.
457 pyglib_option_group_new (GOptionGroup *group)
459 return _PyGLib_API->option_group_new(group);
463 * pyglib_option_context_new:
464 * @context: a GOptionContext
466 * Returns: A new GOptionContext wrapper.
469 pyglib_option_context_new (GOptionContext *context)
471 return _PyGLib_API->option_context_new(context);
475 /****** Private *****/
478 * _pyglib_destroy_notify:
479 * @user_data: a PyObject pointer.
481 * A function that can be used as a GDestroyNotify callback that will
482 * call Py_DECREF on the data.
485 _pyglib_destroy_notify(gpointer user_data)
487 PyObject *obj = (PyObject *)user_data;
488 PyGILState_STATE state;
490 g_return_if_fail (_PyGLib_API != NULL);
492 state = pyglib_gil_state_ensure();
494 pyglib_gil_state_release(state);
498 _pyglib_handler_marshal(gpointer user_data)
500 PyObject *tuple, *ret;
502 PyGILState_STATE state;
504 g_return_val_if_fail(user_data != NULL, FALSE);
506 state = pyglib_gil_state_ensure();
508 tuple = (PyObject *)user_data;
509 ret = PyObject_CallObject(PyTuple_GetItem(tuple, 0),
510 PyTuple_GetItem(tuple, 1));
515 res = PyObject_IsTrue(ret);
519 pyglib_gil_state_release(state);
525 _pyglib_generic_ptr_richcompare(void* a, void *b, int op)
532 res = (a == b) ? Py_True : Py_False;
536 res = (a != b) ? Py_True : Py_False;
540 res = (a < b) ? Py_True : Py_False;
544 res = (a <= b) ? Py_True : Py_False;
548 res = (a > b) ? Py_True : Py_False;
552 res = (a >= b) ? Py_True : Py_False;
556 res = Py_NotImplemented;
565 _pyglib_generic_long_richcompare(long a, long b, int op)
572 res = (a == b) ? Py_True : Py_False;
577 res = (a != b) ? Py_True : Py_False;
583 res = (a < b) ? Py_True : Py_False;
588 res = (a <= b) ? Py_True : Py_False;
593 res = (a > b) ? Py_True : Py_False;
598 res = (a >= b) ? Py_True : Py_False;
603 res = Py_NotImplemented;