1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 1998-2003 James Henstridge
5 * gobjectmodule.c: wrapper for the gobject library.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include <gobject/gvaluecollector.h>
27 #include <girepository.h>
30 #include "pygobject-private.h"
34 #include "pyginterface.h"
35 #include "pygparamspec.h"
36 #include "pygpointer.h"
38 #include "pygoptiongroup.h"
40 #include "pygi-value.h"
41 #include "pygi-error.h"
42 #include "pygi-property.h"
44 static GHashTable *log_handlers = NULL;
45 static gboolean log_handlers_disabled = FALSE;
47 static void pyg_flags_add_constants(PyObject *module, GType flags_type,
48 const gchar *strip_prefix);
51 /* -------------- GDK threading hooks ---------------------------- */
54 * pyg_set_thread_block_funcs:
55 * Deprecated, only available for ABI compatibility.
58 _pyg_set_thread_block_funcs (PyGThreadBlockFunc block_threads_func,
59 PyGThreadBlockFunc unblock_threads_func)
61 PyGILState_STATE state = pyglib_gil_state_ensure ();
62 PyErr_Warn (PyExc_DeprecationWarning,
63 "Using pyg_set_thread_block_funcs is not longer needed. "
64 "PyGObject always uses Py_BLOCK/UNBLOCK_THREADS.");
65 pyglib_gil_state_release (state);
70 * @user_data: a PyObject pointer.
72 * A function that can be used as a GDestroyNotify callback that will
73 * call Py_DECREF on the data.
76 pyg_destroy_notify(gpointer user_data)
78 PyObject *obj = (PyObject *)user_data;
79 PyGILState_STATE state;
81 state = pyglib_gil_state_ensure();
83 pyglib_gil_state_release(state);
87 /* ---------------- gobject module functions -------------------- */
90 pyg_type_name (PyObject *self, PyObject *args)
97 if (PyErr_Warn(PyExc_DeprecationWarning,
98 "gobject.type_name is deprecated; "
99 "use GType.name instead"))
103 if (!PyArg_ParseTuple(args, "O:gobject.type_name", >ype))
105 if ((type = pyg_type_from_object(gtype)) == 0)
107 name = g_type_name(type);
109 return PYGLIB_PyUnicode_FromString(name);
110 PyErr_SetString(PyExc_RuntimeError, "unknown typecode");
115 pyg_type_from_name (PyObject *self, PyObject *args)
119 PyObject *repr = NULL;
121 if (PyErr_Warn(PyExc_DeprecationWarning,
122 "gobject.type_from_name is deprecated; "
123 "use GType.from_name instead"))
126 if (!PyArg_ParseTuple(args, "s:gobject.type_from_name", &name))
128 type = g_type_from_name(name);
130 return pyg_type_wrapper_new(type);
131 repr = PyObject_Repr((PyObject*)self);
132 PyErr_Format(PyExc_RuntimeError, "%s: unknown type name: %s",
133 PYGLIB_PyUnicode_AsString(repr),
140 pyg_type_is_a (PyObject *self, PyObject *args)
142 PyObject *gtype, *gparent;
145 if (PyErr_Warn(PyExc_DeprecationWarning,
146 "gobject.type_is_a is deprecated; "
147 "use GType.is_a instead"))
150 if (!PyArg_ParseTuple(args, "OO:gobject.type_is_a", >ype, &gparent))
152 if ((type = pyg_type_from_object(gtype)) == 0)
154 if ((parent = pyg_type_from_object(gparent)) == 0)
156 return PyBool_FromLong(g_type_is_a(type, parent));
160 pyg_object_set_property (GObject *object, guint property_id,
161 const GValue *value, GParamSpec *pspec)
163 PyObject *object_wrapper, *retval;
164 PyObject *py_pspec, *py_value;
165 PyGILState_STATE state;
167 state = pyglib_gil_state_ensure();
169 object_wrapper = pygobject_new(object);
171 if (object_wrapper == NULL) {
172 pyglib_gil_state_release(state);
176 py_pspec = pyg_param_spec_new(pspec);
177 py_value = pyg_value_as_pyobject (value, TRUE);
179 retval = PyObject_CallMethod(object_wrapper, "do_set_property",
180 "OO", py_pspec, py_value);
187 Py_DECREF(object_wrapper);
191 pyglib_gil_state_release(state);
195 pyg_object_get_property (GObject *object, guint property_id,
196 GValue *value, GParamSpec *pspec)
198 PyObject *object_wrapper, *retval;
199 PyGILState_STATE state;
201 state = pyglib_gil_state_ensure();
203 object_wrapper = pygobject_new(object);
205 if (object_wrapper == NULL) {
206 pyglib_gil_state_release(state);
210 retval = pygi_call_do_get_property (object_wrapper, pspec);
211 if (retval && pyg_value_from_pyobject (value, retval) < 0) {
214 Py_DECREF(object_wrapper);
217 pyglib_gil_state_release(state);
220 typedef struct _PyGSignalAccumulatorData {
223 } PyGSignalAccumulatorData;
226 _pyg_signal_accumulator(GSignalInvocationHint *ihint,
228 const GValue *handler_return,
231 PyObject *py_ihint, *py_return_accu, *py_handler_return, *py_detail;
233 gboolean retval = FALSE;
234 PyGSignalAccumulatorData *data = _data;
235 PyGILState_STATE state;
237 state = pyglib_gil_state_ensure();
239 py_detail = PYGLIB_PyUnicode_FromString(g_quark_to_string(ihint->detail));
245 py_ihint = Py_BuildValue("lNi", (long int) ihint->signal_id,
246 py_detail, ihint->run_type);
247 py_handler_return = pyg_value_as_pyobject(handler_return, TRUE);
248 py_return_accu = pyg_value_as_pyobject(return_accu, FALSE);
250 py_retval = PyObject_CallFunction(data->callable, "NNNO", py_ihint,
251 py_return_accu, py_handler_return,
254 py_retval = PyObject_CallFunction(data->callable, "NNN", py_ihint,
255 py_return_accu, py_handler_return);
259 if (!PyTuple_Check(py_retval) || PyTuple_Size(py_retval) != 2) {
260 PyErr_SetString(PyExc_TypeError, "accumulator function must return"
261 " a (bool, object) tuple");
264 retval = PyObject_IsTrue(PyTuple_GET_ITEM(py_retval, 0));
265 if (pyg_value_from_pyobject(return_accu, PyTuple_GET_ITEM(py_retval, 1))) {
269 Py_DECREF(py_retval);
271 pyglib_gil_state_release(state);
276 create_signal (GType instance_type, const gchar *signal_name, PyObject *tuple)
278 GSignalFlags signal_flags;
279 PyObject *py_return_type, *py_param_types;
284 GSignalAccumulator accumulator = NULL;
285 PyGSignalAccumulatorData *accum_data = NULL;
286 PyObject *py_accum = NULL, *py_accum_data = NULL;
288 if (!PyArg_ParseTuple(tuple, "iOO|OO", &signal_flags, &py_return_type,
289 &py_param_types, &py_accum, &py_accum_data))
294 g_snprintf(buf, sizeof(buf),
295 "value for __gsignals__['%s'] not in correct format", signal_name);
296 PyErr_SetString(PyExc_TypeError, buf);
300 if (py_accum && py_accum != Py_None && !PyCallable_Check(py_accum))
304 g_snprintf(buf, sizeof(buf),
305 "accumulator for __gsignals__['%s'] must be callable", signal_name);
306 PyErr_SetString(PyExc_TypeError, buf);
310 return_type = pyg_type_from_object(py_return_type);
313 if (!PySequence_Check(py_param_types)) {
316 g_snprintf(buf, sizeof(buf),
317 "third element of __gsignals__['%s'] tuple must be a sequence", signal_name);
318 PyErr_SetString(PyExc_TypeError, buf);
321 n_params = PySequence_Length(py_param_types);
322 param_types = g_new(GType, n_params);
323 for (i = 0; i < n_params; i++) {
324 PyObject *item = PySequence_GetItem(py_param_types, i);
326 param_types[i] = pyg_type_from_object(item);
327 if (param_types[i] == 0) {
335 if (py_accum != NULL && py_accum != Py_None) {
336 accum_data = g_new(PyGSignalAccumulatorData, 1);
337 accum_data->callable = py_accum;
339 accum_data->user_data = py_accum_data;
340 Py_XINCREF(py_accum_data);
341 accumulator = _pyg_signal_accumulator;
344 signal_id = g_signal_newv(signal_name, instance_type, signal_flags,
345 pyg_signal_class_closure_get(),
346 accumulator, accum_data,
347 gi_cclosure_marshal_generic,
348 return_type, n_params, param_types);
351 if (signal_id == 0) {
354 g_snprintf(buf, sizeof(buf), "could not create signal for %s",
356 PyErr_SetString(PyExc_RuntimeError, buf);
363 override_signal(GType instance_type, const gchar *signal_name)
367 signal_id = g_signal_lookup(signal_name, instance_type);
371 g_snprintf(buf, sizeof(buf), "could not look up %s", signal_name);
372 PyErr_SetString(PyExc_TypeError, buf);
375 g_signal_override_class_closure(signal_id, instance_type,
376 pyg_signal_class_closure_get());
381 add_signals (GObjectClass *klass, PyObject *signals)
385 PyObject *key, *value, *overridden_signals = NULL;
386 GType instance_type = G_OBJECT_CLASS_TYPE (klass);
388 overridden_signals = PyDict_New();
389 while (PyDict_Next(signals, &pos, &key, &value)) {
390 const gchar *signal_name;
391 gchar *signal_name_canon, *c;
393 if (!PYGLIB_PyUnicode_Check(key)) {
394 PyErr_SetString(PyExc_TypeError,
395 "__gsignals__ keys must be strings");
399 signal_name = PYGLIB_PyUnicode_AsString (key);
401 if (value == Py_None ||
402 (PYGLIB_PyUnicode_Check(value) &&
403 !strcmp(PYGLIB_PyUnicode_AsString(value), "override")))
405 /* canonicalize signal name, replacing '-' with '_' */
406 signal_name_canon = g_strdup(signal_name);
407 for (c = signal_name_canon; *c; ++c)
410 if (PyDict_SetItemString(overridden_signals,
411 signal_name_canon, key)) {
412 g_free(signal_name_canon);
416 g_free(signal_name_canon);
418 ret = override_signal(instance_type, signal_name);
420 ret = create_signal(instance_type, signal_name, value);
427 return overridden_signals;
429 Py_XDECREF(overridden_signals);
435 create_property (const gchar *prop_name,
442 GParamSpec *pspec = NULL;
444 switch (G_TYPE_FUNDAMENTAL(prop_type)) {
447 gchar minimum, maximum, default_value;
449 if (!PyArg_ParseTuple(args, "ccc", &minimum, &maximum,
452 pspec = g_param_spec_char (prop_name, nick, blurb, minimum,
453 maximum, default_value, flags);
458 gchar minimum, maximum, default_value;
460 if (!PyArg_ParseTuple(args, "ccc", &minimum, &maximum,
463 pspec = g_param_spec_uchar (prop_name, nick, blurb, minimum,
464 maximum, default_value, flags);
469 gboolean default_value;
471 if (!PyArg_ParseTuple(args, "i", &default_value))
473 pspec = g_param_spec_boolean (prop_name, nick, blurb,
474 default_value, flags);
479 gint minimum, maximum, default_value;
481 if (!PyArg_ParseTuple(args, "iii", &minimum, &maximum,
484 pspec = g_param_spec_int (prop_name, nick, blurb, minimum,
485 maximum, default_value, flags);
490 guint minimum, maximum, default_value;
492 if (!PyArg_ParseTuple(args, "III", &minimum, &maximum,
495 pspec = g_param_spec_uint (prop_name, nick, blurb, minimum,
496 maximum, default_value, flags);
501 glong minimum, maximum, default_value;
503 if (!PyArg_ParseTuple(args, "lll", &minimum, &maximum,
506 pspec = g_param_spec_long (prop_name, nick, blurb, minimum,
507 maximum, default_value, flags);
512 gulong minimum, maximum, default_value;
514 if (!PyArg_ParseTuple(args, "kkk", &minimum, &maximum,
517 pspec = g_param_spec_ulong (prop_name, nick, blurb, minimum,
518 maximum, default_value, flags);
523 gint64 minimum, maximum, default_value;
525 if (!PyArg_ParseTuple(args, "LLL", &minimum, &maximum,
528 pspec = g_param_spec_int64 (prop_name, nick, blurb, minimum,
529 maximum, default_value, flags);
534 guint64 minimum, maximum, default_value;
536 if (!PyArg_ParseTuple(args, "KKK", &minimum, &maximum,
539 pspec = g_param_spec_uint64 (prop_name, nick, blurb, minimum,
540 maximum, default_value, flags);
548 if (!PyArg_ParseTuple(args, "O", &pydefault))
551 if (pyg_enum_get_value(prop_type, pydefault,
552 (gint *)&default_value))
555 pspec = g_param_spec_enum (prop_name, nick, blurb,
556 prop_type, default_value, flags);
564 if (!PyArg_ParseTuple(args, "O", &pydefault))
567 if (pyg_flags_get_value(prop_type, pydefault,
571 pspec = g_param_spec_flags (prop_name, nick, blurb,
572 prop_type, default_value, flags);
577 gfloat minimum, maximum, default_value;
579 if (!PyArg_ParseTuple(args, "fff", &minimum, &maximum,
582 pspec = g_param_spec_float (prop_name, nick, blurb, minimum,
583 maximum, default_value, flags);
588 gdouble minimum, maximum, default_value;
590 if (!PyArg_ParseTuple(args, "ddd", &minimum, &maximum,
593 pspec = g_param_spec_double (prop_name, nick, blurb, minimum,
594 maximum, default_value, flags);
599 const gchar *default_value;
601 if (!PyArg_ParseTuple(args, "z", &default_value))
603 pspec = g_param_spec_string (prop_name, nick, blurb,
604 default_value, flags);
608 if (!PyArg_ParseTuple(args, ""))
610 pspec = g_param_spec_param (prop_name, nick, blurb, prop_type, flags);
613 if (!PyArg_ParseTuple(args, ""))
615 pspec = g_param_spec_boxed (prop_name, nick, blurb, prop_type, flags);
618 if (!PyArg_ParseTuple(args, ""))
620 if (prop_type == G_TYPE_GTYPE)
621 pspec = g_param_spec_gtype (prop_name, nick, blurb, G_TYPE_NONE, flags);
623 pspec = g_param_spec_pointer (prop_name, nick, blurb, flags);
626 case G_TYPE_INTERFACE:
627 if (!PyArg_ParseTuple(args, ""))
629 pspec = g_param_spec_object (prop_name, nick, blurb, prop_type, flags);
634 GVariant *default_value = NULL;
636 if (!PyArg_ParseTuple(args, "O", &pydefault))
638 if (pydefault != Py_None)
639 default_value = pyg_boxed_get (pydefault, GVariant);
640 pspec = g_param_spec_variant (prop_name, nick, blurb, G_VARIANT_TYPE_ANY, default_value, flags);
644 /* unhandled pspec type ... */
651 g_snprintf(buf, sizeof(buf), "could not create param spec for type %s",
652 g_type_name(prop_type));
653 PyErr_SetString(PyExc_TypeError, buf);
661 pyg_param_spec_from_object (PyObject *tuple)
664 const gchar *prop_name;
666 const gchar *nick, *blurb;
667 PyObject *slice, *item, *py_prop_type;
670 val_length = PyTuple_Size(tuple);
671 if (val_length < 4) {
672 PyErr_SetString(PyExc_TypeError,
673 "paramspec tuples must be at least 4 elements long");
677 slice = PySequence_GetSlice(tuple, 0, 4);
682 if (!PyArg_ParseTuple(slice, "sOzz", &prop_name, &py_prop_type, &nick, &blurb)) {
689 prop_type = pyg_type_from_object(py_prop_type);
694 item = PyTuple_GetItem(tuple, val_length-1);
695 if (!PYGLIB_PyLong_Check(item)) {
696 PyErr_SetString(PyExc_TypeError,
697 "last element in tuple must be an int");
701 /* slice is the extra items in the tuple */
702 slice = PySequence_GetSlice(tuple, 4, val_length-1);
703 pspec = create_property(prop_name, prop_type,
705 PYGLIB_PyLong_AsLong(item));
711 add_properties (GObjectClass *klass, PyObject *properties)
715 PyObject *key, *value;
717 while (PyDict_Next(properties, &pos, &key, &value)) {
718 const gchar *prop_name;
720 const gchar *nick, *blurb;
723 PyObject *slice, *item, *py_prop_type;
726 /* values are of format (type,nick,blurb, type_specific_args, flags) */
728 if (!PYGLIB_PyUnicode_Check(key)) {
729 PyErr_SetString(PyExc_TypeError,
730 "__gproperties__ keys must be strings");
734 prop_name = PYGLIB_PyUnicode_AsString (key);
736 if (!PyTuple_Check(value)) {
737 PyErr_SetString(PyExc_TypeError,
738 "__gproperties__ values must be tuples");
742 val_length = PyTuple_Size(value);
743 if (val_length < 4) {
744 PyErr_SetString(PyExc_TypeError,
745 "__gproperties__ values must be at least 4 elements long");
750 slice = PySequence_GetSlice(value, 0, 3);
755 if (!PyArg_ParseTuple(slice, "Ozz", &py_prop_type, &nick, &blurb)) {
761 prop_type = pyg_type_from_object(py_prop_type);
766 item = PyTuple_GetItem(value, val_length-1);
767 if (!PYGLIB_PyLong_Check(item)) {
768 PyErr_SetString(PyExc_TypeError,
769 "last element in __gproperties__ value tuple must be an int");
773 flags = PYGLIB_PyLong_AsLong(item);
775 /* slice is the extra items in the tuple */
776 slice = PySequence_GetSlice(value, 3, val_length-1);
777 pspec = create_property(prop_name, prop_type, nick, blurb,
782 g_object_class_install_property(klass, 1, pspec);
784 PyObject *type, *value, *traceback;
786 PyErr_Fetch(&type, &value, &traceback);
787 if (PYGLIB_PyUnicode_Check(value)) {
790 "%s (while registering property '%s' for GType '%s')",
791 PYGLIB_PyUnicode_AsString(value),
792 prop_name, G_OBJECT_CLASS_NAME(klass));
794 value = PYGLIB_PyUnicode_FromString(msg);
796 PyErr_Restore(type, value, traceback);
805 pyg_object_class_init(GObjectClass *class, PyObject *py_class)
807 PyObject *gproperties, *gsignals, *overridden_signals;
808 PyObject *class_dict = ((PyTypeObject*) py_class)->tp_dict;
810 class->set_property = pyg_object_set_property;
811 class->get_property = pyg_object_get_property;
813 /* install signals */
814 /* we look this up in the instance dictionary, so we don't
815 * accidentally get a parent type's __gsignals__ attribute. */
816 gsignals = PyDict_GetItemString(class_dict, "__gsignals__");
818 if (!PyDict_Check(gsignals)) {
819 PyErr_SetString(PyExc_TypeError,
820 "__gsignals__ attribute not a dict!");
823 if (!(overridden_signals = add_signals(class, gsignals))) {
826 if (PyDict_SetItemString(class_dict, "__gsignals__",
827 overridden_signals)) {
830 Py_DECREF(overridden_signals);
832 PyDict_DelItemString(class_dict, "__gsignals__");
837 /* install properties */
838 /* we look this up in the instance dictionary, so we don't
839 * accidentally get a parent type's __gproperties__ attribute. */
840 gproperties = PyDict_GetItemString(class_dict, "__gproperties__");
842 if (!PyDict_Check(gproperties)) {
843 PyErr_SetString(PyExc_TypeError,
844 "__gproperties__ attribute not a dict!");
847 if (!add_properties(class, gproperties)) {
850 PyDict_DelItemString(class_dict, "__gproperties__");
851 /* Borrowed reference. Py_DECREF(gproperties); */
858 pyg_register_class_init(GType gtype, PyGClassInitFunc class_init)
862 list = g_type_get_qdata(gtype, pygobject_class_init_key);
863 list = g_slist_prepend(list, class_init);
864 g_type_set_qdata(gtype, pygobject_class_init_key, list);
868 pyg_run_class_init(GType gtype, gpointer gclass, PyTypeObject *pyclass)
871 PyGClassInitFunc class_init;
875 parent_type = g_type_parent(gtype);
877 rv = pyg_run_class_init(parent_type, gclass, pyclass);
882 list = g_type_get_qdata(gtype, pygobject_class_init_key);
883 for (; list; list = list->next) {
884 class_init = list->data;
885 rv = class_init(gclass, pyclass);
894 _wrap_pyg_type_register(PyObject *self, PyObject *args)
897 char *type_name = NULL;
899 if (!PyArg_ParseTuple(args, "O!|z:gobject.type_register",
900 &PyType_Type, &class, &type_name))
902 if (!PyType_IsSubtype(class, &PyGObject_Type)) {
903 PyErr_SetString(PyExc_TypeError,
904 "argument must be a GObject subclass");
908 /* Check if type already registered */
909 if (pyg_type_from_object((PyObject *) class) ==
910 pyg_type_from_object((PyObject *) class->tp_base))
912 if (pyg_type_register(class, type_name))
917 return (PyObject *) class;
921 get_type_name_for_class(PyTypeObject *class)
924 char name_serial_str[16];
926 char *type_name = NULL;
928 /* make name for new GType */
930 /* give up after 1000 tries, just in case.. */
931 while (name_serial < 1000)
934 g_snprintf(name_serial_str, 16, "-v%i", name_serial);
935 module = PyObject_GetAttrString((PyObject *)class, "__module__");
936 if (module && PYGLIB_PyUnicode_Check(module)) {
937 type_name = g_strconcat(PYGLIB_PyUnicode_AsString(module), ".",
939 name_serial > 1 ? name_serial_str : NULL,
947 type_name = g_strconcat(class->tp_name,
948 name_serial > 1 ? name_serial_str : NULL,
951 /* convert '.' in type name to '+', which isn't banned (grumble) */
952 for (i = 0; type_name[i] != '\0'; i++)
953 if (type_name[i] == '.')
955 if (g_type_from_name(type_name) == 0)
956 break; /* we now have a unique name */
964 static GPrivate pygobject_construction_wrapper;
967 pygobject_init_wrapper_set(PyObject *wrapper)
969 g_private_set(&pygobject_construction_wrapper, wrapper);
972 static inline PyObject *
973 pygobject_init_wrapper_get(void)
975 return (PyObject *) g_private_get(&pygobject_construction_wrapper);
979 pygobject_constructv(PyGObject *self,
981 GParameter *parameters)
985 g_assert (self->obj == NULL);
986 pygobject_init_wrapper_set((PyObject *) self);
987 obj = g_object_newv(pyg_type_from_object((PyObject *) self),
988 n_parameters, parameters);
990 if (g_object_is_floating (obj))
991 self->private_flags.flags |= PYGOBJECT_GOBJECT_WAS_FLOATING;
992 pygobject_sink (obj);
994 pygobject_init_wrapper_set(NULL);
996 pygobject_register_wrapper((PyObject *) self);
1002 pygobject__g_instance_init(GTypeInstance *instance,
1005 GObject *object = (GObject *) instance;
1006 PyObject *wrapper, *args, *kwargs;
1008 wrapper = g_object_get_qdata(object, pygobject_wrapper_key);
1009 if (wrapper == NULL) {
1010 wrapper = pygobject_init_wrapper_get();
1011 if (wrapper && ((PyGObject *) wrapper)->obj == NULL) {
1012 ((PyGObject *) wrapper)->obj = object;
1013 pygobject_register_wrapper(wrapper);
1016 pygobject_init_wrapper_set(NULL);
1017 if (wrapper == NULL) {
1018 /* this looks like a python object created through
1019 * g_object_new -> we have no python wrapper, so create it
1021 PyGILState_STATE state;
1022 state = pyglib_gil_state_ensure();
1023 wrapper = pygobject_new_full(object,
1027 /* float the wrapper ref here because we are going to orphan it
1028 * so we don't destroy the wrapper. The next call to pygobject_new_full
1029 * will take the ref */
1030 pygobject_ref_float ((PyGObject *) wrapper);
1031 args = PyTuple_New(0);
1032 kwargs = PyDict_New();
1033 if (Py_TYPE(wrapper)->tp_init(wrapper, args, kwargs))
1038 pyglib_gil_state_release(state);
1043 /* This implementation is bad, see bug 566571 for an example why.
1044 * Instead of scanning explicitly declared bases for interfaces, we
1045 * should automatically initialize all implemented interfaces to
1046 * prevent bugs like that one. However, this will lead to
1047 * performance degradation as each virtual method in derived classes
1048 * will round-trip through do_*() stuff, *even* if it is not
1049 * overriden. We need to teach codegen to retain parent method
1050 * instead of setting virtual to *_proxy_do_*() if corresponding
1051 * do_*() is not overriden. Ok, that was a messy explanation.
1054 pyg_type_add_interfaces(PyTypeObject *class, GType instance_type,
1056 GType *parent_interfaces, guint n_parent_interfaces)
1061 g_warning("type has no bases");
1065 for (i = 0; i < PyTuple_GET_SIZE(bases); ++i) {
1066 PyObject *base = PyTuple_GET_ITEM(bases, i);
1068 const GInterfaceInfo *iinfo;
1069 GInterfaceInfo iinfo_copy;
1071 /* 'base' can also be a PyClassObject, see bug #566571. */
1072 if (!PyType_Check(base))
1075 if (!PyType_IsSubtype((PyTypeObject*) base, &PyGInterface_Type))
1078 itype = pyg_type_from_object(base);
1080 /* Happens for _implementations_ of an interface. */
1081 if (!G_TYPE_IS_INTERFACE(itype))
1084 iinfo = pyg_lookup_interface_info(itype);
1087 error = g_strdup_printf("Interface type %s "
1088 "has no Python implementation support",
1089 ((PyTypeObject *) base)->tp_name);
1090 PyErr_Warn(PyExc_RuntimeWarning, error);
1095 iinfo_copy = *iinfo;
1096 iinfo_copy.interface_data = class;
1097 g_type_add_interface_static(instance_type, itype, &iinfo_copy);
1102 pyg_type_register(PyTypeObject *class, const char *type_name)
1105 GType parent_type, instance_type;
1106 GType *parent_interfaces;
1107 guint n_parent_interfaces;
1110 GTypeInfo type_info = {
1113 (GBaseInitFunc) NULL,
1114 (GBaseFinalizeFunc) NULL,
1116 (GClassInitFunc) pyg_object_class_init,
1117 (GClassFinalizeFunc) NULL,
1118 NULL, /* class_data */
1120 0, /* instance_size */
1121 0, /* n_preallocs */
1122 (GInstanceInitFunc) pygobject__g_instance_init
1124 gchar *new_type_name;
1126 /* find the GType of the parent */
1127 parent_type = pyg_type_from_object((PyObject *)class);
1131 parent_interfaces = g_type_interfaces(parent_type, &n_parent_interfaces);
1134 /* care is taken below not to free this */
1135 new_type_name = (gchar *) type_name;
1137 new_type_name = get_type_name_for_class(class);
1139 /* set class_data that will be passed to the class_init function. */
1140 type_info.class_data = class;
1142 /* fill in missing values of GTypeInfo struct */
1143 g_type_query(parent_type, &query);
1144 type_info.class_size = query.class_size;
1145 type_info.instance_size = query.instance_size;
1147 /* create new typecode */
1148 instance_type = g_type_register_static(parent_type, new_type_name,
1150 if (instance_type == 0) {
1151 PyErr_Format(PyExc_RuntimeError,
1152 "could not create new GType: %s (subclass of %s)",
1154 g_type_name(parent_type));
1156 if (type_name == NULL)
1157 g_free(new_type_name);
1162 if (type_name == NULL)
1163 g_free(new_type_name);
1165 /* store pointer to the class with the GType */
1167 g_type_set_qdata(instance_type, g_quark_from_string("PyGObject::class"),
1170 /* Mark this GType as a custom python type */
1171 g_type_set_qdata(instance_type, pygobject_custom_key,
1172 GINT_TO_POINTER (1));
1174 /* set new value of __gtype__ on class */
1175 gtype = pyg_type_wrapper_new(instance_type);
1176 PyObject_SetAttrString((PyObject *)class, "__gtype__", gtype);
1179 /* if no __doc__, set it to the auto doc descriptor */
1180 if (PyDict_GetItemString(class->tp_dict, "__doc__") == NULL) {
1181 PyDict_SetItemString(class->tp_dict, "__doc__",
1182 pyg_object_descr_doc_get());
1186 * Note, all interfaces need to be registered before the first
1187 * g_type_class_ref(), see bug #686149.
1189 * See also comment above pyg_type_add_interfaces().
1191 pyg_type_add_interfaces(class, instance_type, class->tp_bases,
1192 parent_interfaces, n_parent_interfaces);
1195 gclass = g_type_class_ref(instance_type);
1196 if (PyErr_Occurred() != NULL) {
1197 g_type_class_unref(gclass);
1198 g_free(parent_interfaces);
1202 if (pyg_run_class_init(instance_type, gclass, class)) {
1203 g_type_class_unref(gclass);
1204 g_free(parent_interfaces);
1207 g_type_class_unref(gclass);
1208 g_free(parent_interfaces);
1210 if (PyErr_Occurred() != NULL)
1216 pyg_signal_new(PyObject *self, PyObject *args)
1220 GSignalFlags signal_flags;
1222 PyObject *py_return_type, *py_param_types;
1224 GType instance_type = 0;
1225 Py_ssize_t n_params, i;
1230 if (!PyArg_ParseTuple(args, "sOiOO:gobject.signal_new", &signal_name,
1231 &py_type, &signal_flags, &py_return_type,
1235 instance_type = pyg_type_from_object(py_type);
1238 if (!(G_TYPE_IS_INSTANTIATABLE(instance_type) || G_TYPE_IS_INTERFACE(instance_type))) {
1239 PyErr_SetString(PyExc_TypeError,
1240 "argument 2 must be an object type or interface type");
1244 return_type = pyg_type_from_object(py_return_type);
1248 if (!PySequence_Check(py_param_types)) {
1249 PyErr_SetString(PyExc_TypeError,
1250 "argument 5 must be a sequence of GType codes");
1253 n_params = PySequence_Length(py_param_types);
1254 param_types = g_new(GType, n_params);
1255 for (i = 0; i < n_params; i++) {
1256 PyObject *item = PySequence_GetItem(py_param_types, i);
1258 param_types[i] = pyg_type_from_object(item);
1259 if (param_types[i] == 0) {
1262 PyErr_SetString(PyExc_TypeError,
1263 "argument 5 must be a sequence of GType codes");
1264 g_free(param_types);
1270 signal_id = g_signal_newv(signal_name, instance_type, signal_flags,
1271 pyg_signal_class_closure_get(),
1272 (GSignalAccumulator)0, NULL,
1273 (GSignalCMarshaller)0,
1274 return_type, n_params, param_types);
1275 g_free(param_types);
1277 return PYGLIB_PyLong_FromLong(signal_id);
1278 PyErr_SetString(PyExc_RuntimeError, "could not create signal");
1283 pyg_object_class_list_properties (PyObject *self, PyObject *args)
1286 PyObject *py_itype, *list;
1288 GObjectClass *class = NULL;
1289 gpointer iface = NULL;
1293 if (!PyArg_ParseTuple(args, "O:gobject.list_properties",
1296 if ((itype = pyg_type_from_object(py_itype)) == 0)
1299 if (G_TYPE_IS_INTERFACE(itype)) {
1300 iface = g_type_default_interface_ref(itype);
1302 PyErr_SetString(PyExc_RuntimeError,
1303 "could not get a reference to interface type");
1306 specs = g_object_interface_list_properties(iface, &nprops);
1307 } else if (g_type_is_a(itype, G_TYPE_OBJECT)) {
1308 class = g_type_class_ref(itype);
1310 PyErr_SetString(PyExc_RuntimeError,
1311 "could not get a reference to type class");
1314 specs = g_object_class_list_properties(class, &nprops);
1316 PyErr_SetString(PyExc_TypeError,
1317 "type must be derived from GObject or an interface");
1321 list = PyTuple_New(nprops);
1324 g_type_class_unref(class);
1327 for (i = 0; i < nprops; i++) {
1328 PyTuple_SetItem(list, i, pyg_param_spec_new(specs[i]));
1332 g_type_class_unref(class);
1334 g_type_default_interface_unref(iface);
1340 pyg_object_new (PyGObject *self, PyObject *args, PyObject *kwargs)
1344 GObject *obj = NULL;
1345 GObjectClass *class;
1346 guint n_params = 0, i;
1347 GParameter *params = NULL;
1349 if (!PyArg_ParseTuple (args, "O:gobject.new", &pytype)) {
1353 if ((type = pyg_type_from_object (pytype)) == 0)
1356 if (G_TYPE_IS_ABSTRACT(type)) {
1357 PyErr_Format(PyExc_TypeError, "cannot create instance of abstract "
1358 "(non-instantiable) type `%s'", g_type_name(type));
1362 if ((class = g_type_class_ref (type)) == NULL) {
1363 PyErr_SetString(PyExc_TypeError,
1364 "could not get a reference to type class");
1368 if (!pygobject_prepare_construct_properties (class, kwargs, &n_params, ¶ms))
1371 obj = g_object_newv(type, n_params, params);
1373 PyErr_SetString (PyExc_RuntimeError, "could not create object");
1376 for (i = 0; i < n_params; i++) {
1377 g_free((gchar *) params[i].name);
1378 g_value_unset(¶ms[i].value);
1381 g_type_class_unref(class);
1384 pygobject_sink (obj);
1385 self = (PyGObject *) pygobject_new((GObject *)obj);
1386 g_object_unref(obj);
1390 return (PyObject *) self;
1394 pyg_handler_marshal(gpointer user_data)
1396 PyObject *tuple, *ret;
1398 PyGILState_STATE state;
1400 g_return_val_if_fail(user_data != NULL, FALSE);
1402 state = pyglib_gil_state_ensure();
1404 tuple = (PyObject *)user_data;
1405 ret = PyObject_CallObject(PyTuple_GetItem(tuple, 0),
1406 PyTuple_GetItem(tuple, 1));
1411 res = PyObject_IsTrue(ret);
1415 pyglib_gil_state_release(state);
1421 pygobject_gil_state_ensure (void)
1423 return pyglib_gil_state_ensure ();
1427 pygobject_gil_state_release (int flag)
1429 pyglib_gil_state_release(flag);
1432 /* Only for backwards compatibility */
1434 pygobject_enable_threads(void)
1440 pyg_signal_accumulator_true_handled(PyObject *unused, PyObject *args)
1442 PyErr_SetString(PyExc_TypeError,
1443 "signal_accumulator_true_handled can only"
1444 " be used as accumulator argument when registering signals");
1449 marshal_emission_hook(GSignalInvocationHint *ihint,
1450 guint n_param_values,
1451 const GValue *param_values,
1454 PyGILState_STATE state;
1455 gboolean retval = FALSE;
1456 PyObject *func, *args;
1461 state = pyglib_gil_state_ensure();
1463 /* construct Python tuple for the parameter values */
1464 params = PyTuple_New(n_param_values);
1466 for (i = 0; i < n_param_values; i++) {
1467 PyObject *item = pyg_value_as_pyobject(¶m_values[i], FALSE);
1469 /* error condition */
1473 PyTuple_SetItem(params, i, item);
1476 args = (PyObject *)user_data;
1477 func = PyTuple_GetItem(args, 0);
1478 args = PySequence_Concat(params, PyTuple_GetItem(args, 1));
1481 /* params passed to function may have extra arguments */
1483 retobj = PyObject_CallObject(func, args);
1485 if (retobj == NULL) {
1489 retval = (retobj == Py_True ? TRUE : FALSE);
1492 pyglib_gil_state_release(state);
1497 pyg_add_emission_hook(PyGObject *self, PyObject *args)
1499 PyObject *first, *callback, *extra_args, *data, *repr;
1508 len = PyTuple_Size(args);
1510 PyErr_SetString(PyExc_TypeError,
1511 "gobject.add_emission_hook requires at least 3 arguments");
1514 first = PySequence_GetSlice(args, 0, 3);
1515 if (!PyArg_ParseTuple(first, "OsO:add_emission_hook",
1516 &pygtype, &name, &callback)) {
1522 if ((gtype = pyg_type_from_object(pygtype)) == 0) {
1525 if (!PyCallable_Check(callback)) {
1526 PyErr_SetString(PyExc_TypeError, "third argument must be callable");
1530 if (!g_signal_parse_name(name, gtype, &sigid, &detail, TRUE)) {
1531 repr = PyObject_Repr((PyObject*)self);
1532 PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s",
1533 PYGLIB_PyUnicode_AsString(repr),
1538 extra_args = PySequence_GetSlice(args, 3, len);
1539 if (extra_args == NULL)
1542 data = Py_BuildValue("(ON)", callback, extra_args);
1546 hook_id = g_signal_add_emission_hook(sigid, detail,
1547 marshal_emission_hook,
1549 (GDestroyNotify)pyg_destroy_notify);
1551 return PyLong_FromUnsignedLong(hook_id);
1555 pyg__install_metaclass(PyObject *dummy, PyTypeObject *metaclass)
1557 Py_INCREF(metaclass);
1558 PyGObject_MetaType = metaclass;
1559 Py_INCREF(metaclass);
1561 Py_TYPE(&PyGObject_Type) = metaclass;
1568 pyg__gvalue_get(PyObject *module, PyObject *pygvalue)
1570 if (!pyg_boxed_check (pygvalue, G_TYPE_VALUE)) {
1571 PyErr_SetString (PyExc_TypeError, "Expected GValue argument.");
1575 return pyg_value_as_pyobject (pyg_boxed_get(pygvalue, GValue),
1576 /*copy_boxed=*/ TRUE);
1580 pyg__gvalue_set(PyObject *module, PyObject *args)
1585 if (!PyArg_ParseTuple (args, "OO:_gobject._gvalue_set",
1586 &pygvalue, &pyobject))
1589 if (!pyg_boxed_check (pygvalue, G_TYPE_VALUE)) {
1590 PyErr_SetString (PyExc_TypeError, "Expected GValue argument.");
1594 if (pyg_value_from_pyobject_with_error (pyg_boxed_get (pygvalue, GValue),
1601 static PyMethodDef _gobject_functions[] = {
1602 { "type_name", pyg_type_name, METH_VARARGS },
1603 { "type_from_name", pyg_type_from_name, METH_VARARGS },
1604 { "type_is_a", pyg_type_is_a, METH_VARARGS },
1605 { "type_register", _wrap_pyg_type_register, METH_VARARGS },
1606 { "signal_new", pyg_signal_new, METH_VARARGS },
1607 { "list_properties",
1608 pyg_object_class_list_properties, METH_VARARGS },
1610 (PyCFunction)pyg_object_new, METH_VARARGS|METH_KEYWORDS },
1611 { "signal_accumulator_true_handled",
1612 (PyCFunction)pyg_signal_accumulator_true_handled, METH_VARARGS },
1613 { "add_emission_hook",
1614 (PyCFunction)pyg_add_emission_hook, METH_VARARGS },
1615 { "_install_metaclass",
1616 (PyCFunction)pyg__install_metaclass, METH_O },
1618 (PyCFunction)pyg__gvalue_get, METH_O },
1620 (PyCFunction)pyg__gvalue_set, METH_VARARGS },
1626 /* ----------------- Constant extraction ------------------------ */
1629 * pyg_constant_strip_prefix:
1630 * @name: the constant name.
1631 * @strip_prefix: the prefix to strip.
1633 * Advances the pointer @name by strlen(@strip_prefix) characters. If
1634 * the resulting name does not start with a letter or underscore, the
1635 * @name pointer will be rewound. This is to ensure that the
1636 * resulting name is a valid identifier. Hence the returned string is
1637 * a pointer into the string @name.
1639 * Returns: the stripped constant name.
1642 pyg_constant_strip_prefix(const gchar *name, const gchar *strip_prefix)
1647 prefix_len = strlen(strip_prefix);
1649 /* Check so name starts with strip_prefix, if it doesn't:
1650 * return the rest of the part which doesn't match
1652 for (i = 0; i < prefix_len; i++) {
1653 if (name[i] != strip_prefix[i] && name[i] != '_') {
1658 /* strip off prefix from value name, while keeping it a valid
1660 for (i = prefix_len; i >= 0; i--) {
1661 if (g_ascii_isalpha(name[i]) || name[i] == '_') {
1669 * pyg_enum_add_constants:
1670 * @module: a Python module
1671 * @enum_type: the GType of the enumeration.
1672 * @strip_prefix: the prefix to strip from the constant names.
1674 * Adds constants to the given Python module for each value name of
1675 * the enumeration. A prefix will be stripped from each enum name.
1678 pyg_enum_add_constants(PyObject *module, GType enum_type,
1679 const gchar *strip_prefix)
1684 if (!G_TYPE_IS_ENUM(enum_type)) {
1685 if (G_TYPE_IS_FLAGS(enum_type)) /* See bug #136204 */
1686 pyg_flags_add_constants(module, enum_type, strip_prefix);
1688 g_warning("`%s' is not an enum type", g_type_name(enum_type));
1691 g_return_if_fail (strip_prefix != NULL);
1693 eclass = G_ENUM_CLASS(g_type_class_ref(enum_type));
1695 for (i = 0; i < eclass->n_values; i++) {
1696 const gchar *name = eclass->values[i].value_name;
1697 gint value = eclass->values[i].value;
1699 PyModule_AddIntConstant(module,
1700 (char*) pyg_constant_strip_prefix(name, strip_prefix),
1704 g_type_class_unref(eclass);
1708 * pyg_flags_add_constants:
1709 * @module: a Python module
1710 * @flags_type: the GType of the flags type.
1711 * @strip_prefix: the prefix to strip from the constant names.
1713 * Adds constants to the given Python module for each value name of
1714 * the flags set. A prefix will be stripped from each flag name.
1717 pyg_flags_add_constants(PyObject *module, GType flags_type,
1718 const gchar *strip_prefix)
1720 GFlagsClass *fclass;
1723 if (!G_TYPE_IS_FLAGS(flags_type)) {
1724 if (G_TYPE_IS_ENUM(flags_type)) /* See bug #136204 */
1725 pyg_enum_add_constants(module, flags_type, strip_prefix);
1727 g_warning("`%s' is not an flags type", g_type_name(flags_type));
1730 g_return_if_fail (strip_prefix != NULL);
1732 fclass = G_FLAGS_CLASS(g_type_class_ref(flags_type));
1734 for (i = 0; i < fclass->n_values; i++) {
1735 const gchar *name = fclass->values[i].value_name;
1736 guint value = fclass->values[i].value;
1738 PyModule_AddIntConstant(module,
1739 (char*) pyg_constant_strip_prefix(name, strip_prefix),
1743 g_type_class_unref(fclass);
1747 * pyg_parse_constructor_args: helper function for PyGObject constructors
1748 * @obj_type: GType of the GObject, for parameter introspection
1749 * @arg_names: %NULL-terminated array of constructor argument names
1750 * @prop_names: %NULL-terminated array of property names, with direct
1751 * correspondence to @arg_names
1752 * @params: GParameter array where parameters will be placed; length
1753 * of this array must be at least equal to the number of
1754 * arguments/properties
1755 * @nparams: output parameter to contain actual number of arguments found
1756 * @py_args: array of PyObject* containing the actual constructor arguments
1758 * Parses an array of PyObject's and creates a GParameter array
1760 * Return value: %TRUE if all is successful, otherwise %FALSE and
1761 * python exception set.
1764 pyg_parse_constructor_args(GType obj_type,
1771 guint arg_i, param_i;
1772 GObjectClass *oclass;
1774 oclass = g_type_class_ref(obj_type);
1775 g_return_val_if_fail(oclass, FALSE);
1777 for (param_i = arg_i = 0; arg_names[arg_i]; ++arg_i) {
1779 if (!py_args[arg_i])
1781 spec = g_object_class_find_property(oclass, prop_names[arg_i]);
1782 params[param_i].name = prop_names[arg_i];
1783 g_value_init(¶ms[param_i].value, spec->value_type);
1784 if (pyg_value_from_pyobject(¶ms[param_i].value, py_args[arg_i]) == -1) {
1786 PyErr_Format(PyExc_TypeError, "could not convert parameter '%s' of type '%s'",
1787 arg_names[arg_i], g_type_name(spec->value_type));
1788 g_type_class_unref(oclass);
1789 for (i = 0; i < param_i; ++i)
1790 g_value_unset(¶ms[i].value);
1795 g_type_class_unref(oclass);
1801 pyg_integer_richcompare(PyObject *v, PyObject *w, int op)
1807 case Py_EQ: t = PYGLIB_PyLong_AS_LONG(v) == PYGLIB_PyLong_AS_LONG(w); break;
1808 case Py_NE: t = PYGLIB_PyLong_AS_LONG(v) != PYGLIB_PyLong_AS_LONG(w); break;
1809 case Py_LE: t = PYGLIB_PyLong_AS_LONG(v) <= PYGLIB_PyLong_AS_LONG(w); break;
1810 case Py_GE: t = PYGLIB_PyLong_AS_LONG(v) >= PYGLIB_PyLong_AS_LONG(w); break;
1811 case Py_LT: t = PYGLIB_PyLong_AS_LONG(v) < PYGLIB_PyLong_AS_LONG(w); break;
1812 case Py_GT: t = PYGLIB_PyLong_AS_LONG(v) > PYGLIB_PyLong_AS_LONG(w); break;
1813 default: g_assert_not_reached();
1816 result = t ? Py_True : Py_False;
1822 _log_func(const gchar *log_domain,
1823 GLogLevelFlags log_level,
1824 const gchar *message,
1827 if (G_LIKELY(Py_IsInitialized()))
1829 PyGILState_STATE state;
1830 PyObject* warning = user_data;
1832 state = pyglib_gil_state_ensure();
1833 PyErr_Warn(warning, (char *) message);
1834 pyglib_gil_state_release(state);
1836 g_log_default_handler(log_domain, log_level, message, user_data);
1840 add_warning_redirection(const char *domain,
1843 g_return_if_fail(domain != NULL);
1844 g_return_if_fail(warning != NULL);
1846 if (!log_handlers_disabled)
1849 gpointer old_handler;
1852 log_handlers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
1854 if ((old_handler = g_hash_table_lookup(log_handlers, domain)))
1855 g_log_remove_handler(domain, GPOINTER_TO_UINT(old_handler));
1857 handler = g_log_set_handler(domain, G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING,
1858 _log_func, warning);
1859 g_hash_table_insert(log_handlers, g_strdup(domain), GUINT_TO_POINTER(handler));
1864 remove_handler(gpointer domain,
1868 g_log_remove_handler(domain, GPOINTER_TO_UINT(handler));
1872 disable_warning_redirections(void)
1874 log_handlers_disabled = TRUE;
1878 g_hash_table_foreach(log_handlers, remove_handler, NULL);
1879 g_hash_table_destroy(log_handlers);
1880 log_handlers = NULL;
1884 /* ----------------- gobject module initialisation -------------- */
1886 struct _PyGObject_Functions pygobject_api_functions = {
1887 pygobject_register_class,
1888 pygobject_register_wrapper,
1889 pygobject_lookup_class,
1893 pygobject_watch_closure,
1896 pyg_type_from_object,
1897 pyg_type_wrapper_new,
1899 pyg_flags_get_value,
1900 pyg_register_gtype_custom,
1901 pyg_value_from_pyobject,
1902 pyg_value_as_pyobject,
1904 pyg_register_interface,
1911 pyg_register_pointer,
1914 pyg_enum_add_constants,
1915 pyg_flags_add_constants,
1917 pyg_constant_strip_prefix,
1921 _pyg_set_thread_block_funcs,
1922 (PyGThreadBlockFunc)0, /* block_threads */
1923 (PyGThreadBlockFunc)0, /* unblock_threads */
1927 pyg_param_spec_from_object,
1929 pyg_pyobj_to_unichar_conv,
1930 pyg_parse_constructor_args,
1931 pyg_param_gvalue_as_pyobject,
1932 pyg_param_gvalue_from_pyobject,
1936 pyg_enum_from_gtype,
1940 pyg_flags_from_gtype,
1942 /* threads_enabled */
1943 #ifdef DISABLE_THREADING
1949 pygobject_enable_threads,
1950 pygobject_gil_state_ensure,
1951 pygobject_gil_state_release,
1952 pyg_register_class_init,
1953 pyg_register_interface_info,
1955 pyg_closure_set_exception_handler,
1957 add_warning_redirection,
1958 disable_warning_redirections,
1960 NULL, /* previously type_register_custom */
1962 pygi_gerror_exception_check,
1964 pyg_option_group_new,
1965 pyg_type_from_object_strict,
1970 pyg_value_from_pyobject_with_error
1973 /* for addon libraries ... */
1975 pygobject_register_api(PyObject *d)
1979 api = PYGLIB_CPointer_WrapPointer(&pygobject_api_functions, "gobject._PyGObject_API");
1980 PyDict_SetItemString(d, "_PyGObject_API", api);
1984 /* some constants */
1986 pygobject_register_constants(PyObject *m)
1988 /* PyFloat_ return a new ref, and add object takes the ref */
1989 PyModule_AddObject(m, "G_MINFLOAT", PyFloat_FromDouble(G_MINFLOAT));
1990 PyModule_AddObject(m, "G_MAXFLOAT", PyFloat_FromDouble(G_MAXFLOAT));
1991 PyModule_AddObject(m, "G_MINDOUBLE", PyFloat_FromDouble(G_MINDOUBLE));
1992 PyModule_AddObject(m, "G_MAXDOUBLE", PyFloat_FromDouble(G_MAXDOUBLE));
1993 PyModule_AddIntConstant(m, "G_MINSHORT", G_MINSHORT);
1994 PyModule_AddIntConstant(m, "G_MAXSHORT", G_MAXSHORT);
1995 PyModule_AddIntConstant(m, "G_MAXUSHORT", G_MAXUSHORT);
1996 PyModule_AddIntConstant(m, "G_MININT", G_MININT);
1997 PyModule_AddIntConstant(m, "G_MAXINT", G_MAXINT);
1998 PyModule_AddObject(m, "G_MAXUINT", PyLong_FromUnsignedLong(G_MAXUINT));
1999 PyModule_AddObject(m, "G_MINLONG", PyLong_FromLong(G_MINLONG));
2000 PyModule_AddObject(m, "G_MAXLONG", PyLong_FromLong(G_MAXLONG));
2001 PyModule_AddObject(m, "G_MAXULONG", PyLong_FromUnsignedLong(G_MAXULONG));
2002 PyModule_AddObject(m, "G_MAXSIZE", PyLong_FromSize_t(G_MAXSIZE));
2003 PyModule_AddObject(m, "G_MAXSSIZE", PyLong_FromSsize_t(G_MAXSSIZE));
2004 PyModule_AddObject(m, "G_MINSSIZE", PyLong_FromSsize_t(G_MINSSIZE));
2005 PyModule_AddObject(m, "G_MINOFFSET", PyLong_FromLongLong(G_MINOFFSET));
2006 PyModule_AddObject(m, "G_MAXOFFSET", PyLong_FromLongLong(G_MAXOFFSET));
2008 PyModule_AddIntConstant(m, "SIGNAL_RUN_FIRST", G_SIGNAL_RUN_FIRST);
2009 PyModule_AddIntConstant(m, "PARAM_READWRITE", G_PARAM_READWRITE);
2011 /* The rest of the types are set in __init__.py */
2012 PyModule_AddObject(m, "TYPE_INVALID", pyg_type_wrapper_new(G_TYPE_INVALID));
2013 PyModule_AddObject(m, "TYPE_GSTRING", pyg_type_wrapper_new(G_TYPE_GSTRING));
2018 pygobject_register_features(PyObject *d)
2022 features = PyDict_New();
2023 PyDict_SetItemString(features, "generic-c-marshaller", Py_True);
2024 PyDict_SetItemString(d, "features", features);
2025 Py_DECREF(features);
2029 pygobject_register_version_tuples(PyObject *d)
2033 /* pygobject version */
2034 tuple = Py_BuildValue ("(iii)",
2035 PYGOBJECT_MAJOR_VERSION,
2036 PYGOBJECT_MINOR_VERSION,
2037 PYGOBJECT_MICRO_VERSION);
2038 PyDict_SetItemString(d, "pygobject_version", tuple);
2042 pygobject_register_warnings(PyObject *d)
2046 warning = PyErr_NewException("gobject.Warning", PyExc_Warning, NULL);
2047 PyDict_SetItemString(d, "Warning", warning);
2048 add_warning_redirection("GLib", warning);
2049 add_warning_redirection("GLib-GObject", warning);
2050 add_warning_redirection("GThread", warning);
2054 PYGLIB_MODULE_START(_gobject, "_gobject")
2058 d = PyModule_GetDict(module);
2059 pygobject_register_api(d);
2060 pygobject_register_constants(module);
2061 pygobject_register_features(d);
2062 pygobject_register_version_tuples(d);
2063 pygobject_register_warnings(d);
2064 pygobject_type_register_types(d);
2065 pygobject_object_register_types(d);
2066 pygobject_interface_register_types(d);
2067 pygobject_paramspec_register_types(d);
2068 pygobject_boxed_register_types(d);
2069 pygobject_pointer_register_types(d);
2070 pygobject_enum_register_types(d);
2071 pygobject_flags_register_types(d);