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 "gobjectmodule.h"
34 #include "pyginterface.h"
35 #include "pygparamspec.h"
36 #include "pygpointer.h"
38 #include "pygoptiongroup.h"
39 #include "pygobject-object.h"
41 #include "pygi-value.h"
42 #include "pygi-error.h"
43 #include "pygi-property.h"
45 static GHashTable *log_handlers = NULL;
46 static gboolean log_handlers_disabled = FALSE;
48 static void pyg_flags_add_constants(PyObject *module, GType flags_type,
49 const gchar *strip_prefix);
51 static int pyg_type_register(PyTypeObject *class, const char *type_name);
53 /* -------------- GDK threading hooks ---------------------------- */
56 * pyg_set_thread_block_funcs:
57 * Deprecated, only available for ABI compatibility.
60 _pyg_set_thread_block_funcs (PyGThreadBlockFunc block_threads_func,
61 PyGThreadBlockFunc unblock_threads_func)
63 PyGILState_STATE state = pyglib_gil_state_ensure ();
64 PyErr_Warn (PyExc_DeprecationWarning,
65 "Using pyg_set_thread_block_funcs is not longer needed. "
66 "PyGObject always uses Py_BLOCK/UNBLOCK_THREADS.");
67 pyglib_gil_state_release (state);
72 * @user_data: a PyObject pointer.
74 * A function that can be used as a GDestroyNotify callback that will
75 * call Py_DECREF on the data.
78 pyg_destroy_notify(gpointer user_data)
80 PyObject *obj = (PyObject *)user_data;
81 PyGILState_STATE state;
83 state = pyglib_gil_state_ensure();
85 pyglib_gil_state_release(state);
89 /* ---------------- gobject module functions -------------------- */
92 pyg_type_name (PyObject *self, PyObject *args)
99 if (PyErr_Warn(PyExc_DeprecationWarning,
100 "gobject.type_name is deprecated; "
101 "use GType.name instead"))
105 if (!PyArg_ParseTuple(args, "O:gobject.type_name", >ype))
107 if ((type = pyg_type_from_object(gtype)) == 0)
109 name = g_type_name(type);
111 return PYGLIB_PyUnicode_FromString(name);
112 PyErr_SetString(PyExc_RuntimeError, "unknown typecode");
117 pyg_type_from_name (PyObject *self, PyObject *args)
121 PyObject *repr = NULL;
123 if (PyErr_Warn(PyExc_DeprecationWarning,
124 "gobject.type_from_name is deprecated; "
125 "use GType.from_name instead"))
128 if (!PyArg_ParseTuple(args, "s:gobject.type_from_name", &name))
130 type = g_type_from_name(name);
132 return pyg_type_wrapper_new(type);
133 repr = PyObject_Repr((PyObject*)self);
134 PyErr_Format(PyExc_RuntimeError, "%s: unknown type name: %s",
135 PYGLIB_PyUnicode_AsString(repr),
142 pyg_type_is_a (PyObject *self, PyObject *args)
144 PyObject *gtype, *gparent;
147 if (PyErr_Warn(PyExc_DeprecationWarning,
148 "gobject.type_is_a is deprecated; "
149 "use GType.is_a instead"))
152 if (!PyArg_ParseTuple(args, "OO:gobject.type_is_a", >ype, &gparent))
154 if ((type = pyg_type_from_object(gtype)) == 0)
156 if ((parent = pyg_type_from_object(gparent)) == 0)
158 return PyBool_FromLong(g_type_is_a(type, parent));
162 pyg_object_set_property (GObject *object, guint property_id,
163 const GValue *value, GParamSpec *pspec)
165 PyObject *object_wrapper, *retval;
166 PyObject *py_pspec, *py_value;
167 PyGILState_STATE state;
169 state = pyglib_gil_state_ensure();
171 object_wrapper = pygobject_new(object);
173 if (object_wrapper == NULL) {
174 pyglib_gil_state_release(state);
178 py_pspec = pyg_param_spec_new(pspec);
179 py_value = pyg_value_as_pyobject (value, TRUE);
181 retval = PyObject_CallMethod(object_wrapper, "do_set_property",
182 "OO", py_pspec, py_value);
189 Py_DECREF(object_wrapper);
193 pyglib_gil_state_release(state);
197 pyg_object_get_property (GObject *object, guint property_id,
198 GValue *value, GParamSpec *pspec)
200 PyObject *object_wrapper, *retval;
201 PyGILState_STATE state;
203 state = pyglib_gil_state_ensure();
205 object_wrapper = pygobject_new(object);
207 if (object_wrapper == NULL) {
208 pyglib_gil_state_release(state);
212 retval = pygi_call_do_get_property (object_wrapper, pspec);
213 if (retval && pyg_value_from_pyobject (value, retval) < 0) {
216 Py_DECREF(object_wrapper);
219 pyglib_gil_state_release(state);
222 typedef struct _PyGSignalAccumulatorData {
225 } PyGSignalAccumulatorData;
228 _pyg_signal_accumulator(GSignalInvocationHint *ihint,
230 const GValue *handler_return,
233 PyObject *py_ihint, *py_return_accu, *py_handler_return, *py_detail;
235 gboolean retval = FALSE;
236 PyGSignalAccumulatorData *data = _data;
237 PyGILState_STATE state;
239 state = pyglib_gil_state_ensure();
241 py_detail = PYGLIB_PyUnicode_FromString(g_quark_to_string(ihint->detail));
247 py_ihint = Py_BuildValue("lNi", (long int) ihint->signal_id,
248 py_detail, ihint->run_type);
249 py_handler_return = pyg_value_as_pyobject(handler_return, TRUE);
250 py_return_accu = pyg_value_as_pyobject(return_accu, FALSE);
252 py_retval = PyObject_CallFunction(data->callable, "NNNO", py_ihint,
253 py_return_accu, py_handler_return,
256 py_retval = PyObject_CallFunction(data->callable, "NNN", py_ihint,
257 py_return_accu, py_handler_return);
261 if (!PyTuple_Check(py_retval) || PyTuple_Size(py_retval) != 2) {
262 PyErr_SetString(PyExc_TypeError, "accumulator function must return"
263 " a (bool, object) tuple");
266 retval = PyObject_IsTrue(PyTuple_GET_ITEM(py_retval, 0));
267 if (pyg_value_from_pyobject(return_accu, PyTuple_GET_ITEM(py_retval, 1))) {
271 Py_DECREF(py_retval);
273 pyglib_gil_state_release(state);
278 create_signal (GType instance_type, const gchar *signal_name, PyObject *tuple)
280 GSignalFlags signal_flags;
281 PyObject *py_return_type, *py_param_types;
286 GSignalAccumulator accumulator = NULL;
287 PyGSignalAccumulatorData *accum_data = NULL;
288 PyObject *py_accum = NULL, *py_accum_data = NULL;
290 if (!PyArg_ParseTuple(tuple, "iOO|OO", &signal_flags, &py_return_type,
291 &py_param_types, &py_accum, &py_accum_data))
296 g_snprintf(buf, sizeof(buf),
297 "value for __gsignals__['%s'] not in correct format", signal_name);
298 PyErr_SetString(PyExc_TypeError, buf);
302 if (py_accum && py_accum != Py_None && !PyCallable_Check(py_accum))
306 g_snprintf(buf, sizeof(buf),
307 "accumulator for __gsignals__['%s'] must be callable", signal_name);
308 PyErr_SetString(PyExc_TypeError, buf);
312 return_type = pyg_type_from_object(py_return_type);
315 if (!PySequence_Check(py_param_types)) {
318 g_snprintf(buf, sizeof(buf),
319 "third element of __gsignals__['%s'] tuple must be a sequence", signal_name);
320 PyErr_SetString(PyExc_TypeError, buf);
323 n_params = PySequence_Length(py_param_types);
324 param_types = g_new(GType, n_params);
325 for (i = 0; i < n_params; i++) {
326 PyObject *item = PySequence_GetItem(py_param_types, i);
328 param_types[i] = pyg_type_from_object(item);
329 if (param_types[i] == 0) {
337 if (py_accum != NULL && py_accum != Py_None) {
338 accum_data = g_new(PyGSignalAccumulatorData, 1);
339 accum_data->callable = py_accum;
341 accum_data->user_data = py_accum_data;
342 Py_XINCREF(py_accum_data);
343 accumulator = _pyg_signal_accumulator;
346 signal_id = g_signal_newv(signal_name, instance_type, signal_flags,
347 pyg_signal_class_closure_get(),
348 accumulator, accum_data,
349 gi_cclosure_marshal_generic,
350 return_type, n_params, param_types);
353 if (signal_id == 0) {
356 g_snprintf(buf, sizeof(buf), "could not create signal for %s",
358 PyErr_SetString(PyExc_RuntimeError, buf);
365 override_signal(GType instance_type, const gchar *signal_name)
369 signal_id = g_signal_lookup(signal_name, instance_type);
373 g_snprintf(buf, sizeof(buf), "could not look up %s", signal_name);
374 PyErr_SetString(PyExc_TypeError, buf);
377 g_signal_override_class_closure(signal_id, instance_type,
378 pyg_signal_class_closure_get());
383 add_signals (GObjectClass *klass, PyObject *signals)
387 PyObject *key, *value, *overridden_signals = NULL;
388 GType instance_type = G_OBJECT_CLASS_TYPE (klass);
390 overridden_signals = PyDict_New();
391 while (PyDict_Next(signals, &pos, &key, &value)) {
392 const gchar *signal_name;
393 gchar *signal_name_canon, *c;
395 if (!PYGLIB_PyUnicode_Check(key)) {
396 PyErr_SetString(PyExc_TypeError,
397 "__gsignals__ keys must be strings");
401 signal_name = PYGLIB_PyUnicode_AsString (key);
403 if (value == Py_None ||
404 (PYGLIB_PyUnicode_Check(value) &&
405 !strcmp(PYGLIB_PyUnicode_AsString(value), "override")))
407 /* canonicalize signal name, replacing '-' with '_' */
408 signal_name_canon = g_strdup(signal_name);
409 for (c = signal_name_canon; *c; ++c)
412 if (PyDict_SetItemString(overridden_signals,
413 signal_name_canon, key)) {
414 g_free(signal_name_canon);
418 g_free(signal_name_canon);
420 ret = override_signal(instance_type, signal_name);
422 ret = create_signal(instance_type, signal_name, value);
429 return overridden_signals;
431 Py_XDECREF(overridden_signals);
437 create_property (const gchar *prop_name,
444 GParamSpec *pspec = NULL;
446 switch (G_TYPE_FUNDAMENTAL(prop_type)) {
449 gchar minimum, maximum, default_value;
451 if (!PyArg_ParseTuple(args, "ccc", &minimum, &maximum,
454 pspec = g_param_spec_char (prop_name, nick, blurb, minimum,
455 maximum, default_value, flags);
460 gchar minimum, maximum, default_value;
462 if (!PyArg_ParseTuple(args, "ccc", &minimum, &maximum,
465 pspec = g_param_spec_uchar (prop_name, nick, blurb, minimum,
466 maximum, default_value, flags);
471 gboolean default_value;
473 if (!PyArg_ParseTuple(args, "i", &default_value))
475 pspec = g_param_spec_boolean (prop_name, nick, blurb,
476 default_value, flags);
481 gint minimum, maximum, default_value;
483 if (!PyArg_ParseTuple(args, "iii", &minimum, &maximum,
486 pspec = g_param_spec_int (prop_name, nick, blurb, minimum,
487 maximum, default_value, flags);
492 guint minimum, maximum, default_value;
494 if (!PyArg_ParseTuple(args, "III", &minimum, &maximum,
497 pspec = g_param_spec_uint (prop_name, nick, blurb, minimum,
498 maximum, default_value, flags);
503 glong minimum, maximum, default_value;
505 if (!PyArg_ParseTuple(args, "lll", &minimum, &maximum,
508 pspec = g_param_spec_long (prop_name, nick, blurb, minimum,
509 maximum, default_value, flags);
514 gulong minimum, maximum, default_value;
516 if (!PyArg_ParseTuple(args, "kkk", &minimum, &maximum,
519 pspec = g_param_spec_ulong (prop_name, nick, blurb, minimum,
520 maximum, default_value, flags);
525 gint64 minimum, maximum, default_value;
527 if (!PyArg_ParseTuple(args, "LLL", &minimum, &maximum,
530 pspec = g_param_spec_int64 (prop_name, nick, blurb, minimum,
531 maximum, default_value, flags);
536 guint64 minimum, maximum, default_value;
538 if (!PyArg_ParseTuple(args, "KKK", &minimum, &maximum,
541 pspec = g_param_spec_uint64 (prop_name, nick, blurb, minimum,
542 maximum, default_value, flags);
550 if (!PyArg_ParseTuple(args, "O", &pydefault))
553 if (pyg_enum_get_value(prop_type, pydefault,
554 (gint *)&default_value))
557 pspec = g_param_spec_enum (prop_name, nick, blurb,
558 prop_type, default_value, flags);
566 if (!PyArg_ParseTuple(args, "O", &pydefault))
569 if (pyg_flags_get_value(prop_type, pydefault,
573 pspec = g_param_spec_flags (prop_name, nick, blurb,
574 prop_type, default_value, flags);
579 gfloat minimum, maximum, default_value;
581 if (!PyArg_ParseTuple(args, "fff", &minimum, &maximum,
584 pspec = g_param_spec_float (prop_name, nick, blurb, minimum,
585 maximum, default_value, flags);
590 gdouble minimum, maximum, default_value;
592 if (!PyArg_ParseTuple(args, "ddd", &minimum, &maximum,
595 pspec = g_param_spec_double (prop_name, nick, blurb, minimum,
596 maximum, default_value, flags);
601 const gchar *default_value;
603 if (!PyArg_ParseTuple(args, "z", &default_value))
605 pspec = g_param_spec_string (prop_name, nick, blurb,
606 default_value, flags);
610 if (!PyArg_ParseTuple(args, ""))
612 pspec = g_param_spec_param (prop_name, nick, blurb, prop_type, flags);
615 if (!PyArg_ParseTuple(args, ""))
617 pspec = g_param_spec_boxed (prop_name, nick, blurb, prop_type, flags);
620 if (!PyArg_ParseTuple(args, ""))
622 if (prop_type == G_TYPE_GTYPE)
623 pspec = g_param_spec_gtype (prop_name, nick, blurb, G_TYPE_NONE, flags);
625 pspec = g_param_spec_pointer (prop_name, nick, blurb, flags);
628 case G_TYPE_INTERFACE:
629 if (!PyArg_ParseTuple(args, ""))
631 pspec = g_param_spec_object (prop_name, nick, blurb, prop_type, flags);
636 GVariant *default_value = NULL;
638 if (!PyArg_ParseTuple(args, "O", &pydefault))
640 if (pydefault != Py_None)
641 default_value = pyg_boxed_get (pydefault, GVariant);
642 pspec = g_param_spec_variant (prop_name, nick, blurb, G_VARIANT_TYPE_ANY, default_value, flags);
646 /* unhandled pspec type ... */
653 g_snprintf(buf, sizeof(buf), "could not create param spec for type %s",
654 g_type_name(prop_type));
655 PyErr_SetString(PyExc_TypeError, buf);
663 pyg_param_spec_from_object (PyObject *tuple)
666 const gchar *prop_name;
668 const gchar *nick, *blurb;
669 PyObject *slice, *item, *py_prop_type;
672 val_length = PyTuple_Size(tuple);
673 if (val_length < 4) {
674 PyErr_SetString(PyExc_TypeError,
675 "paramspec tuples must be at least 4 elements long");
679 slice = PySequence_GetSlice(tuple, 0, 4);
684 if (!PyArg_ParseTuple(slice, "sOzz", &prop_name, &py_prop_type, &nick, &blurb)) {
691 prop_type = pyg_type_from_object(py_prop_type);
696 item = PyTuple_GetItem(tuple, val_length-1);
697 if (!PYGLIB_PyLong_Check(item)) {
698 PyErr_SetString(PyExc_TypeError,
699 "last element in tuple must be an int");
703 /* slice is the extra items in the tuple */
704 slice = PySequence_GetSlice(tuple, 4, val_length-1);
705 pspec = create_property(prop_name, prop_type,
707 PYGLIB_PyLong_AsLong(item));
713 add_properties (GObjectClass *klass, PyObject *properties)
717 PyObject *key, *value;
719 while (PyDict_Next(properties, &pos, &key, &value)) {
720 const gchar *prop_name;
722 const gchar *nick, *blurb;
725 PyObject *slice, *item, *py_prop_type;
728 /* values are of format (type,nick,blurb, type_specific_args, flags) */
730 if (!PYGLIB_PyUnicode_Check(key)) {
731 PyErr_SetString(PyExc_TypeError,
732 "__gproperties__ keys must be strings");
736 prop_name = PYGLIB_PyUnicode_AsString (key);
738 if (!PyTuple_Check(value)) {
739 PyErr_SetString(PyExc_TypeError,
740 "__gproperties__ values must be tuples");
744 val_length = PyTuple_Size(value);
745 if (val_length < 4) {
746 PyErr_SetString(PyExc_TypeError,
747 "__gproperties__ values must be at least 4 elements long");
752 slice = PySequence_GetSlice(value, 0, 3);
757 if (!PyArg_ParseTuple(slice, "Ozz", &py_prop_type, &nick, &blurb)) {
763 prop_type = pyg_type_from_object(py_prop_type);
768 item = PyTuple_GetItem(value, val_length-1);
769 if (!PYGLIB_PyLong_Check(item)) {
770 PyErr_SetString(PyExc_TypeError,
771 "last element in __gproperties__ value tuple must be an int");
775 flags = PYGLIB_PyLong_AsLong(item);
777 /* slice is the extra items in the tuple */
778 slice = PySequence_GetSlice(value, 3, val_length-1);
779 pspec = create_property(prop_name, prop_type, nick, blurb,
784 g_object_class_install_property(klass, 1, pspec);
786 PyObject *type, *value, *traceback;
788 PyErr_Fetch(&type, &value, &traceback);
789 if (PYGLIB_PyUnicode_Check(value)) {
792 "%s (while registering property '%s' for GType '%s')",
793 PYGLIB_PyUnicode_AsString(value),
794 prop_name, G_OBJECT_CLASS_NAME(klass));
796 value = PYGLIB_PyUnicode_FromString(msg);
798 PyErr_Restore(type, value, traceback);
807 pyg_object_class_init(GObjectClass *class, PyObject *py_class)
809 PyObject *gproperties, *gsignals, *overridden_signals;
810 PyObject *class_dict = ((PyTypeObject*) py_class)->tp_dict;
812 class->set_property = pyg_object_set_property;
813 class->get_property = pyg_object_get_property;
815 /* install signals */
816 /* we look this up in the instance dictionary, so we don't
817 * accidentally get a parent type's __gsignals__ attribute. */
818 gsignals = PyDict_GetItemString(class_dict, "__gsignals__");
820 if (!PyDict_Check(gsignals)) {
821 PyErr_SetString(PyExc_TypeError,
822 "__gsignals__ attribute not a dict!");
825 if (!(overridden_signals = add_signals(class, gsignals))) {
828 if (PyDict_SetItemString(class_dict, "__gsignals__",
829 overridden_signals)) {
832 Py_DECREF(overridden_signals);
834 PyDict_DelItemString(class_dict, "__gsignals__");
839 /* install properties */
840 /* we look this up in the instance dictionary, so we don't
841 * accidentally get a parent type's __gproperties__ attribute. */
842 gproperties = PyDict_GetItemString(class_dict, "__gproperties__");
844 if (!PyDict_Check(gproperties)) {
845 PyErr_SetString(PyExc_TypeError,
846 "__gproperties__ attribute not a dict!");
849 if (!add_properties(class, gproperties)) {
852 PyDict_DelItemString(class_dict, "__gproperties__");
853 /* Borrowed reference. Py_DECREF(gproperties); */
860 pyg_register_class_init(GType gtype, PyGClassInitFunc class_init)
864 list = g_type_get_qdata(gtype, pygobject_class_init_key);
865 list = g_slist_prepend(list, class_init);
866 g_type_set_qdata(gtype, pygobject_class_init_key, list);
870 pyg_run_class_init(GType gtype, gpointer gclass, PyTypeObject *pyclass)
873 PyGClassInitFunc class_init;
877 parent_type = g_type_parent(gtype);
879 rv = pyg_run_class_init(parent_type, gclass, pyclass);
884 list = g_type_get_qdata(gtype, pygobject_class_init_key);
885 for (; list; list = list->next) {
886 class_init = list->data;
887 rv = class_init(gclass, pyclass);
896 _wrap_pyg_type_register(PyObject *self, PyObject *args)
899 char *type_name = NULL;
901 if (!PyArg_ParseTuple(args, "O!|z:gobject.type_register",
902 &PyType_Type, &class, &type_name))
904 if (!PyType_IsSubtype(class, &PyGObject_Type)) {
905 PyErr_SetString(PyExc_TypeError,
906 "argument must be a GObject subclass");
910 /* Check if type already registered */
911 if (pyg_type_from_object((PyObject *) class) ==
912 pyg_type_from_object((PyObject *) class->tp_base))
914 if (pyg_type_register(class, type_name))
919 return (PyObject *) class;
923 get_type_name_for_class(PyTypeObject *class)
926 char name_serial_str[16];
928 char *type_name = NULL;
930 /* make name for new GType */
932 /* give up after 1000 tries, just in case.. */
933 while (name_serial < 1000)
936 g_snprintf(name_serial_str, 16, "-v%i", name_serial);
937 module = PyObject_GetAttrString((PyObject *)class, "__module__");
938 if (module && PYGLIB_PyUnicode_Check(module)) {
939 type_name = g_strconcat(PYGLIB_PyUnicode_AsString(module), ".",
941 name_serial > 1 ? name_serial_str : NULL,
949 type_name = g_strconcat(class->tp_name,
950 name_serial > 1 ? name_serial_str : NULL,
953 /* convert '.' in type name to '+', which isn't banned (grumble) */
954 for (i = 0; type_name[i] != '\0'; i++)
955 if (type_name[i] == '.')
957 if (g_type_from_name(type_name) == 0)
958 break; /* we now have a unique name */
966 static GPrivate pygobject_construction_wrapper;
969 pygobject_init_wrapper_set(PyObject *wrapper)
971 g_private_set(&pygobject_construction_wrapper, wrapper);
974 static inline PyObject *
975 pygobject_init_wrapper_get(void)
977 return (PyObject *) g_private_get(&pygobject_construction_wrapper);
981 pygobject_constructv(PyGObject *self,
983 GParameter *parameters)
987 g_assert (self->obj == NULL);
988 pygobject_init_wrapper_set((PyObject *) self);
989 obj = g_object_newv(pyg_type_from_object((PyObject *) self),
990 n_parameters, parameters);
992 if (g_object_is_floating (obj))
993 self->private_flags.flags |= PYGOBJECT_GOBJECT_WAS_FLOATING;
994 pygobject_sink (obj);
996 pygobject_init_wrapper_set(NULL);
998 pygobject_register_wrapper((PyObject *) self);
1004 pygobject__g_instance_init(GTypeInstance *instance,
1007 GObject *object = (GObject *) instance;
1008 PyObject *wrapper, *args, *kwargs;
1010 wrapper = g_object_get_qdata(object, pygobject_wrapper_key);
1011 if (wrapper == NULL) {
1012 wrapper = pygobject_init_wrapper_get();
1013 if (wrapper && ((PyGObject *) wrapper)->obj == NULL) {
1014 ((PyGObject *) wrapper)->obj = object;
1015 pygobject_register_wrapper(wrapper);
1018 pygobject_init_wrapper_set(NULL);
1019 if (wrapper == NULL) {
1020 /* this looks like a python object created through
1021 * g_object_new -> we have no python wrapper, so create it
1023 PyGILState_STATE state;
1024 state = pyglib_gil_state_ensure();
1025 wrapper = pygobject_new_full(object,
1029 /* float the wrapper ref here because we are going to orphan it
1030 * so we don't destroy the wrapper. The next call to pygobject_new_full
1031 * will take the ref */
1032 pygobject_ref_float ((PyGObject *) wrapper);
1033 args = PyTuple_New(0);
1034 kwargs = PyDict_New();
1035 if (Py_TYPE(wrapper)->tp_init(wrapper, args, kwargs))
1040 pyglib_gil_state_release(state);
1045 /* This implementation is bad, see bug 566571 for an example why.
1046 * Instead of scanning explicitly declared bases for interfaces, we
1047 * should automatically initialize all implemented interfaces to
1048 * prevent bugs like that one. However, this will lead to
1049 * performance degradation as each virtual method in derived classes
1050 * will round-trip through do_*() stuff, *even* if it is not
1051 * overriden. We need to teach codegen to retain parent method
1052 * instead of setting virtual to *_proxy_do_*() if corresponding
1053 * do_*() is not overriden. Ok, that was a messy explanation.
1056 pyg_type_add_interfaces(PyTypeObject *class, GType instance_type,
1058 GType *parent_interfaces, guint n_parent_interfaces)
1063 g_warning("type has no bases");
1067 for (i = 0; i < PyTuple_GET_SIZE(bases); ++i) {
1068 PyObject *base = PyTuple_GET_ITEM(bases, i);
1070 const GInterfaceInfo *iinfo;
1071 GInterfaceInfo iinfo_copy;
1073 /* 'base' can also be a PyClassObject, see bug #566571. */
1074 if (!PyType_Check(base))
1077 if (!PyType_IsSubtype((PyTypeObject*) base, &PyGInterface_Type))
1080 itype = pyg_type_from_object(base);
1082 /* Happens for _implementations_ of an interface. */
1083 if (!G_TYPE_IS_INTERFACE(itype))
1086 iinfo = pyg_lookup_interface_info(itype);
1089 error = g_strdup_printf("Interface type %s "
1090 "has no Python implementation support",
1091 ((PyTypeObject *) base)->tp_name);
1092 PyErr_Warn(PyExc_RuntimeWarning, error);
1097 iinfo_copy = *iinfo;
1098 iinfo_copy.interface_data = class;
1099 g_type_add_interface_static(instance_type, itype, &iinfo_copy);
1104 pyg_type_register(PyTypeObject *class, const char *type_name)
1107 GType parent_type, instance_type;
1108 GType *parent_interfaces;
1109 guint n_parent_interfaces;
1112 GTypeInfo type_info = {
1115 (GBaseInitFunc) NULL,
1116 (GBaseFinalizeFunc) NULL,
1118 (GClassInitFunc) pyg_object_class_init,
1119 (GClassFinalizeFunc) NULL,
1120 NULL, /* class_data */
1122 0, /* instance_size */
1123 0, /* n_preallocs */
1124 (GInstanceInitFunc) pygobject__g_instance_init
1126 gchar *new_type_name;
1128 /* find the GType of the parent */
1129 parent_type = pyg_type_from_object((PyObject *)class);
1133 parent_interfaces = g_type_interfaces(parent_type, &n_parent_interfaces);
1136 /* care is taken below not to free this */
1137 new_type_name = (gchar *) type_name;
1139 new_type_name = get_type_name_for_class(class);
1141 /* set class_data that will be passed to the class_init function. */
1142 type_info.class_data = class;
1144 /* fill in missing values of GTypeInfo struct */
1145 g_type_query(parent_type, &query);
1146 type_info.class_size = query.class_size;
1147 type_info.instance_size = query.instance_size;
1149 /* create new typecode */
1150 instance_type = g_type_register_static(parent_type, new_type_name,
1152 if (instance_type == 0) {
1153 PyErr_Format(PyExc_RuntimeError,
1154 "could not create new GType: %s (subclass of %s)",
1156 g_type_name(parent_type));
1158 if (type_name == NULL)
1159 g_free(new_type_name);
1164 if (type_name == NULL)
1165 g_free(new_type_name);
1167 /* store pointer to the class with the GType */
1169 g_type_set_qdata(instance_type, g_quark_from_string("PyGObject::class"),
1172 /* Mark this GType as a custom python type */
1173 g_type_set_qdata(instance_type, pygobject_custom_key,
1174 GINT_TO_POINTER (1));
1176 /* set new value of __gtype__ on class */
1177 gtype = pyg_type_wrapper_new(instance_type);
1178 PyObject_SetAttrString((PyObject *)class, "__gtype__", gtype);
1181 /* if no __doc__, set it to the auto doc descriptor */
1182 if (PyDict_GetItemString(class->tp_dict, "__doc__") == NULL) {
1183 PyDict_SetItemString(class->tp_dict, "__doc__",
1184 pyg_object_descr_doc_get());
1188 * Note, all interfaces need to be registered before the first
1189 * g_type_class_ref(), see bug #686149.
1191 * See also comment above pyg_type_add_interfaces().
1193 pyg_type_add_interfaces(class, instance_type, class->tp_bases,
1194 parent_interfaces, n_parent_interfaces);
1197 gclass = g_type_class_ref(instance_type);
1198 if (PyErr_Occurred() != NULL) {
1199 g_type_class_unref(gclass);
1200 g_free(parent_interfaces);
1204 if (pyg_run_class_init(instance_type, gclass, class)) {
1205 g_type_class_unref(gclass);
1206 g_free(parent_interfaces);
1209 g_type_class_unref(gclass);
1210 g_free(parent_interfaces);
1212 if (PyErr_Occurred() != NULL)
1218 pyg_signal_new(PyObject *self, PyObject *args)
1222 GSignalFlags signal_flags;
1224 PyObject *py_return_type, *py_param_types;
1226 GType instance_type = 0;
1227 Py_ssize_t n_params, i;
1232 if (!PyArg_ParseTuple(args, "sOiOO:gobject.signal_new", &signal_name,
1233 &py_type, &signal_flags, &py_return_type,
1237 instance_type = pyg_type_from_object(py_type);
1240 if (!(G_TYPE_IS_INSTANTIATABLE(instance_type) || G_TYPE_IS_INTERFACE(instance_type))) {
1241 PyErr_SetString(PyExc_TypeError,
1242 "argument 2 must be an object type or interface type");
1246 return_type = pyg_type_from_object(py_return_type);
1250 if (!PySequence_Check(py_param_types)) {
1251 PyErr_SetString(PyExc_TypeError,
1252 "argument 5 must be a sequence of GType codes");
1255 n_params = PySequence_Length(py_param_types);
1256 param_types = g_new(GType, n_params);
1257 for (i = 0; i < n_params; i++) {
1258 PyObject *item = PySequence_GetItem(py_param_types, i);
1260 param_types[i] = pyg_type_from_object(item);
1261 if (param_types[i] == 0) {
1264 PyErr_SetString(PyExc_TypeError,
1265 "argument 5 must be a sequence of GType codes");
1266 g_free(param_types);
1272 signal_id = g_signal_newv(signal_name, instance_type, signal_flags,
1273 pyg_signal_class_closure_get(),
1274 (GSignalAccumulator)0, NULL,
1275 (GSignalCMarshaller)0,
1276 return_type, n_params, param_types);
1277 g_free(param_types);
1279 return PYGLIB_PyLong_FromLong(signal_id);
1280 PyErr_SetString(PyExc_RuntimeError, "could not create signal");
1285 pyg_object_class_list_properties (PyObject *self, PyObject *args)
1288 PyObject *py_itype, *list;
1290 GObjectClass *class = NULL;
1291 gpointer iface = NULL;
1295 if (!PyArg_ParseTuple(args, "O:gobject.list_properties",
1298 if ((itype = pyg_type_from_object(py_itype)) == 0)
1301 if (G_TYPE_IS_INTERFACE(itype)) {
1302 iface = g_type_default_interface_ref(itype);
1304 PyErr_SetString(PyExc_RuntimeError,
1305 "could not get a reference to interface type");
1308 specs = g_object_interface_list_properties(iface, &nprops);
1309 } else if (g_type_is_a(itype, G_TYPE_OBJECT)) {
1310 class = g_type_class_ref(itype);
1312 PyErr_SetString(PyExc_RuntimeError,
1313 "could not get a reference to type class");
1316 specs = g_object_class_list_properties(class, &nprops);
1318 PyErr_SetString(PyExc_TypeError,
1319 "type must be derived from GObject or an interface");
1323 list = PyTuple_New(nprops);
1326 g_type_class_unref(class);
1329 for (i = 0; i < nprops; i++) {
1330 PyTuple_SetItem(list, i, pyg_param_spec_new(specs[i]));
1334 g_type_class_unref(class);
1336 g_type_default_interface_unref(iface);
1342 pyg_object_new (PyGObject *self, PyObject *args, PyObject *kwargs)
1346 GObject *obj = NULL;
1347 GObjectClass *class;
1348 guint n_params = 0, i;
1349 GParameter *params = NULL;
1351 if (!PyArg_ParseTuple (args, "O:gobject.new", &pytype)) {
1355 if ((type = pyg_type_from_object (pytype)) == 0)
1358 if (G_TYPE_IS_ABSTRACT(type)) {
1359 PyErr_Format(PyExc_TypeError, "cannot create instance of abstract "
1360 "(non-instantiable) type `%s'", g_type_name(type));
1364 if ((class = g_type_class_ref (type)) == NULL) {
1365 PyErr_SetString(PyExc_TypeError,
1366 "could not get a reference to type class");
1370 if (!pygobject_prepare_construct_properties (class, kwargs, &n_params, ¶ms))
1373 obj = g_object_newv(type, n_params, params);
1375 PyErr_SetString (PyExc_RuntimeError, "could not create object");
1378 for (i = 0; i < n_params; i++) {
1379 g_free((gchar *) params[i].name);
1380 g_value_unset(¶ms[i].value);
1383 g_type_class_unref(class);
1386 pygobject_sink (obj);
1387 self = (PyGObject *) pygobject_new((GObject *)obj);
1388 g_object_unref(obj);
1392 return (PyObject *) self;
1396 pygobject_gil_state_ensure (void)
1398 return pyglib_gil_state_ensure ();
1402 pygobject_gil_state_release (int flag)
1404 pyglib_gil_state_release(flag);
1407 /* Only for backwards compatibility */
1409 pygobject_enable_threads(void)
1415 pyg_signal_accumulator_true_handled(PyObject *unused, PyObject *args)
1417 PyErr_SetString(PyExc_TypeError,
1418 "signal_accumulator_true_handled can only"
1419 " be used as accumulator argument when registering signals");
1424 marshal_emission_hook(GSignalInvocationHint *ihint,
1425 guint n_param_values,
1426 const GValue *param_values,
1429 PyGILState_STATE state;
1430 gboolean retval = FALSE;
1431 PyObject *func, *args;
1436 state = pyglib_gil_state_ensure();
1438 /* construct Python tuple for the parameter values */
1439 params = PyTuple_New(n_param_values);
1441 for (i = 0; i < n_param_values; i++) {
1442 PyObject *item = pyg_value_as_pyobject(¶m_values[i], FALSE);
1444 /* error condition */
1448 PyTuple_SetItem(params, i, item);
1451 args = (PyObject *)user_data;
1452 func = PyTuple_GetItem(args, 0);
1453 args = PySequence_Concat(params, PyTuple_GetItem(args, 1));
1456 /* params passed to function may have extra arguments */
1458 retobj = PyObject_CallObject(func, args);
1460 if (retobj == NULL) {
1464 retval = (retobj == Py_True ? TRUE : FALSE);
1467 pyglib_gil_state_release(state);
1472 pyg_add_emission_hook(PyGObject *self, PyObject *args)
1474 PyObject *first, *callback, *extra_args, *data, *repr;
1483 len = PyTuple_Size(args);
1485 PyErr_SetString(PyExc_TypeError,
1486 "gobject.add_emission_hook requires at least 3 arguments");
1489 first = PySequence_GetSlice(args, 0, 3);
1490 if (!PyArg_ParseTuple(first, "OsO:add_emission_hook",
1491 &pygtype, &name, &callback)) {
1497 if ((gtype = pyg_type_from_object(pygtype)) == 0) {
1500 if (!PyCallable_Check(callback)) {
1501 PyErr_SetString(PyExc_TypeError, "third argument must be callable");
1505 if (!g_signal_parse_name(name, gtype, &sigid, &detail, TRUE)) {
1506 repr = PyObject_Repr((PyObject*)self);
1507 PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s",
1508 PYGLIB_PyUnicode_AsString(repr),
1513 extra_args = PySequence_GetSlice(args, 3, len);
1514 if (extra_args == NULL)
1517 data = Py_BuildValue("(ON)", callback, extra_args);
1521 hook_id = g_signal_add_emission_hook(sigid, detail,
1522 marshal_emission_hook,
1524 (GDestroyNotify)pyg_destroy_notify);
1526 return PyLong_FromUnsignedLong(hook_id);
1530 pyg__install_metaclass(PyObject *dummy, PyTypeObject *metaclass)
1532 Py_INCREF(metaclass);
1533 PyGObject_MetaType = metaclass;
1534 Py_INCREF(metaclass);
1536 Py_TYPE(&PyGObject_Type) = metaclass;
1543 pyg__gvalue_get(PyObject *module, PyObject *pygvalue)
1545 if (!pyg_boxed_check (pygvalue, G_TYPE_VALUE)) {
1546 PyErr_SetString (PyExc_TypeError, "Expected GValue argument.");
1550 return pyg_value_as_pyobject (pyg_boxed_get(pygvalue, GValue),
1551 /*copy_boxed=*/ TRUE);
1555 pyg__gvalue_set(PyObject *module, PyObject *args)
1560 if (!PyArg_ParseTuple (args, "OO:_gobject._gvalue_set",
1561 &pygvalue, &pyobject))
1564 if (!pyg_boxed_check (pygvalue, G_TYPE_VALUE)) {
1565 PyErr_SetString (PyExc_TypeError, "Expected GValue argument.");
1569 if (pyg_value_from_pyobject_with_error (pyg_boxed_get (pygvalue, GValue),
1576 static PyMethodDef _gobject_functions[] = {
1577 { "type_name", pyg_type_name, METH_VARARGS },
1578 { "type_from_name", pyg_type_from_name, METH_VARARGS },
1579 { "type_is_a", pyg_type_is_a, METH_VARARGS },
1580 { "type_register", _wrap_pyg_type_register, METH_VARARGS },
1581 { "signal_new", pyg_signal_new, METH_VARARGS },
1582 { "list_properties",
1583 pyg_object_class_list_properties, METH_VARARGS },
1585 (PyCFunction)pyg_object_new, METH_VARARGS|METH_KEYWORDS },
1586 { "signal_accumulator_true_handled",
1587 (PyCFunction)pyg_signal_accumulator_true_handled, METH_VARARGS },
1588 { "add_emission_hook",
1589 (PyCFunction)pyg_add_emission_hook, METH_VARARGS },
1590 { "_install_metaclass",
1591 (PyCFunction)pyg__install_metaclass, METH_O },
1593 (PyCFunction)pyg__gvalue_get, METH_O },
1595 (PyCFunction)pyg__gvalue_set, METH_VARARGS },
1601 /* ----------------- Constant extraction ------------------------ */
1604 * pyg_constant_strip_prefix:
1605 * @name: the constant name.
1606 * @strip_prefix: the prefix to strip.
1608 * Advances the pointer @name by strlen(@strip_prefix) characters. If
1609 * the resulting name does not start with a letter or underscore, the
1610 * @name pointer will be rewound. This is to ensure that the
1611 * resulting name is a valid identifier. Hence the returned string is
1612 * a pointer into the string @name.
1614 * Returns: the stripped constant name.
1617 pyg_constant_strip_prefix(const gchar *name, const gchar *strip_prefix)
1622 prefix_len = strlen(strip_prefix);
1624 /* Check so name starts with strip_prefix, if it doesn't:
1625 * return the rest of the part which doesn't match
1627 for (i = 0; i < prefix_len; i++) {
1628 if (name[i] != strip_prefix[i] && name[i] != '_') {
1633 /* strip off prefix from value name, while keeping it a valid
1635 for (i = prefix_len; i >= 0; i--) {
1636 if (g_ascii_isalpha(name[i]) || name[i] == '_') {
1644 * pyg_enum_add_constants:
1645 * @module: a Python module
1646 * @enum_type: the GType of the enumeration.
1647 * @strip_prefix: the prefix to strip from the constant names.
1649 * Adds constants to the given Python module for each value name of
1650 * the enumeration. A prefix will be stripped from each enum name.
1653 pyg_enum_add_constants(PyObject *module, GType enum_type,
1654 const gchar *strip_prefix)
1659 if (!G_TYPE_IS_ENUM(enum_type)) {
1660 if (G_TYPE_IS_FLAGS(enum_type)) /* See bug #136204 */
1661 pyg_flags_add_constants(module, enum_type, strip_prefix);
1663 g_warning("`%s' is not an enum type", g_type_name(enum_type));
1666 g_return_if_fail (strip_prefix != NULL);
1668 eclass = G_ENUM_CLASS(g_type_class_ref(enum_type));
1670 for (i = 0; i < eclass->n_values; i++) {
1671 const gchar *name = eclass->values[i].value_name;
1672 gint value = eclass->values[i].value;
1674 PyModule_AddIntConstant(module,
1675 (char*) pyg_constant_strip_prefix(name, strip_prefix),
1679 g_type_class_unref(eclass);
1683 * pyg_flags_add_constants:
1684 * @module: a Python module
1685 * @flags_type: the GType of the flags type.
1686 * @strip_prefix: the prefix to strip from the constant names.
1688 * Adds constants to the given Python module for each value name of
1689 * the flags set. A prefix will be stripped from each flag name.
1692 pyg_flags_add_constants(PyObject *module, GType flags_type,
1693 const gchar *strip_prefix)
1695 GFlagsClass *fclass;
1698 if (!G_TYPE_IS_FLAGS(flags_type)) {
1699 if (G_TYPE_IS_ENUM(flags_type)) /* See bug #136204 */
1700 pyg_enum_add_constants(module, flags_type, strip_prefix);
1702 g_warning("`%s' is not an flags type", g_type_name(flags_type));
1705 g_return_if_fail (strip_prefix != NULL);
1707 fclass = G_FLAGS_CLASS(g_type_class_ref(flags_type));
1709 for (i = 0; i < fclass->n_values; i++) {
1710 const gchar *name = fclass->values[i].value_name;
1711 guint value = fclass->values[i].value;
1713 PyModule_AddIntConstant(module,
1714 (char*) pyg_constant_strip_prefix(name, strip_prefix),
1718 g_type_class_unref(fclass);
1722 * pyg_parse_constructor_args: helper function for PyGObject constructors
1723 * @obj_type: GType of the GObject, for parameter introspection
1724 * @arg_names: %NULL-terminated array of constructor argument names
1725 * @prop_names: %NULL-terminated array of property names, with direct
1726 * correspondence to @arg_names
1727 * @params: GParameter array where parameters will be placed; length
1728 * of this array must be at least equal to the number of
1729 * arguments/properties
1730 * @nparams: output parameter to contain actual number of arguments found
1731 * @py_args: array of PyObject* containing the actual constructor arguments
1733 * Parses an array of PyObject's and creates a GParameter array
1735 * Return value: %TRUE if all is successful, otherwise %FALSE and
1736 * python exception set.
1739 pyg_parse_constructor_args(GType obj_type,
1746 guint arg_i, param_i;
1747 GObjectClass *oclass;
1749 oclass = g_type_class_ref(obj_type);
1750 g_return_val_if_fail(oclass, FALSE);
1752 for (param_i = arg_i = 0; arg_names[arg_i]; ++arg_i) {
1754 if (!py_args[arg_i])
1756 spec = g_object_class_find_property(oclass, prop_names[arg_i]);
1757 params[param_i].name = prop_names[arg_i];
1758 g_value_init(¶ms[param_i].value, spec->value_type);
1759 if (pyg_value_from_pyobject(¶ms[param_i].value, py_args[arg_i]) == -1) {
1761 PyErr_Format(PyExc_TypeError, "could not convert parameter '%s' of type '%s'",
1762 arg_names[arg_i], g_type_name(spec->value_type));
1763 g_type_class_unref(oclass);
1764 for (i = 0; i < param_i; ++i)
1765 g_value_unset(¶ms[i].value);
1770 g_type_class_unref(oclass);
1776 _log_func(const gchar *log_domain,
1777 GLogLevelFlags log_level,
1778 const gchar *message,
1781 if (G_LIKELY(Py_IsInitialized()))
1783 PyGILState_STATE state;
1784 PyObject* warning = user_data;
1786 state = pyglib_gil_state_ensure();
1787 PyErr_Warn(warning, (char *) message);
1788 pyglib_gil_state_release(state);
1790 g_log_default_handler(log_domain, log_level, message, user_data);
1794 add_warning_redirection(const char *domain,
1797 g_return_if_fail(domain != NULL);
1798 g_return_if_fail(warning != NULL);
1800 if (!log_handlers_disabled)
1803 gpointer old_handler;
1806 log_handlers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
1808 if ((old_handler = g_hash_table_lookup(log_handlers, domain)))
1809 g_log_remove_handler(domain, GPOINTER_TO_UINT(old_handler));
1811 handler = g_log_set_handler(domain, G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING,
1812 _log_func, warning);
1813 g_hash_table_insert(log_handlers, g_strdup(domain), GUINT_TO_POINTER(handler));
1818 remove_handler(gpointer domain,
1822 g_log_remove_handler(domain, GPOINTER_TO_UINT(handler));
1826 disable_warning_redirections(void)
1828 log_handlers_disabled = TRUE;
1832 g_hash_table_foreach(log_handlers, remove_handler, NULL);
1833 g_hash_table_destroy(log_handlers);
1834 log_handlers = NULL;
1838 /* ----------------- gobject module initialisation -------------- */
1840 struct _PyGObject_Functions pygobject_api_functions = {
1841 pygobject_register_class,
1842 pygobject_register_wrapper,
1843 pygobject_lookup_class,
1847 pygobject_watch_closure,
1850 pyg_type_from_object,
1851 pyg_type_wrapper_new,
1853 pyg_flags_get_value,
1854 pyg_register_gtype_custom,
1855 pyg_value_from_pyobject,
1856 pyg_value_as_pyobject,
1858 pyg_register_interface,
1865 pyg_register_pointer,
1868 pyg_enum_add_constants,
1869 pyg_flags_add_constants,
1871 pyg_constant_strip_prefix,
1875 _pyg_set_thread_block_funcs,
1876 (PyGThreadBlockFunc)0, /* block_threads */
1877 (PyGThreadBlockFunc)0, /* unblock_threads */
1881 pyg_param_spec_from_object,
1883 pyg_pyobj_to_unichar_conv,
1884 pyg_parse_constructor_args,
1885 pyg_param_gvalue_as_pyobject,
1886 pyg_param_gvalue_from_pyobject,
1890 pyg_enum_from_gtype,
1894 pyg_flags_from_gtype,
1896 /* threads_enabled */
1897 #ifdef DISABLE_THREADING
1903 pygobject_enable_threads,
1904 pygobject_gil_state_ensure,
1905 pygobject_gil_state_release,
1906 pyg_register_class_init,
1907 pyg_register_interface_info,
1909 pyg_closure_set_exception_handler,
1911 add_warning_redirection,
1912 disable_warning_redirections,
1914 NULL, /* previously type_register_custom */
1916 pygi_gerror_exception_check,
1918 pyg_option_group_new,
1919 pyg_type_from_object_strict,
1924 pyg_value_from_pyobject_with_error
1927 /* for addon libraries ... */
1929 pygobject_register_api(PyObject *d)
1933 api = PYGLIB_CPointer_WrapPointer(&pygobject_api_functions, "gobject._PyGObject_API");
1934 PyDict_SetItemString(d, "_PyGObject_API", api);
1938 /* some constants */
1940 pygobject_register_constants(PyObject *m)
1942 /* PyFloat_ return a new ref, and add object takes the ref */
1943 PyModule_AddObject(m, "G_MINFLOAT", PyFloat_FromDouble(G_MINFLOAT));
1944 PyModule_AddObject(m, "G_MAXFLOAT", PyFloat_FromDouble(G_MAXFLOAT));
1945 PyModule_AddObject(m, "G_MINDOUBLE", PyFloat_FromDouble(G_MINDOUBLE));
1946 PyModule_AddObject(m, "G_MAXDOUBLE", PyFloat_FromDouble(G_MAXDOUBLE));
1947 PyModule_AddIntConstant(m, "G_MINSHORT", G_MINSHORT);
1948 PyModule_AddIntConstant(m, "G_MAXSHORT", G_MAXSHORT);
1949 PyModule_AddIntConstant(m, "G_MAXUSHORT", G_MAXUSHORT);
1950 PyModule_AddIntConstant(m, "G_MININT", G_MININT);
1951 PyModule_AddIntConstant(m, "G_MAXINT", G_MAXINT);
1952 PyModule_AddObject(m, "G_MAXUINT", PyLong_FromUnsignedLong(G_MAXUINT));
1953 PyModule_AddObject(m, "G_MINLONG", PyLong_FromLong(G_MINLONG));
1954 PyModule_AddObject(m, "G_MAXLONG", PyLong_FromLong(G_MAXLONG));
1955 PyModule_AddObject(m, "G_MAXULONG", PyLong_FromUnsignedLong(G_MAXULONG));
1956 PyModule_AddObject(m, "G_MAXSIZE", PyLong_FromSize_t(G_MAXSIZE));
1957 PyModule_AddObject(m, "G_MAXSSIZE", PyLong_FromSsize_t(G_MAXSSIZE));
1958 PyModule_AddObject(m, "G_MINSSIZE", PyLong_FromSsize_t(G_MINSSIZE));
1959 PyModule_AddObject(m, "G_MINOFFSET", PyLong_FromLongLong(G_MINOFFSET));
1960 PyModule_AddObject(m, "G_MAXOFFSET", PyLong_FromLongLong(G_MAXOFFSET));
1962 PyModule_AddIntConstant(m, "SIGNAL_RUN_FIRST", G_SIGNAL_RUN_FIRST);
1963 PyModule_AddIntConstant(m, "PARAM_READWRITE", G_PARAM_READWRITE);
1965 /* The rest of the types are set in __init__.py */
1966 PyModule_AddObject(m, "TYPE_INVALID", pyg_type_wrapper_new(G_TYPE_INVALID));
1967 PyModule_AddObject(m, "TYPE_GSTRING", pyg_type_wrapper_new(G_TYPE_GSTRING));
1972 pygobject_register_features(PyObject *d)
1976 features = PyDict_New();
1977 PyDict_SetItemString(features, "generic-c-marshaller", Py_True);
1978 PyDict_SetItemString(d, "features", features);
1979 Py_DECREF(features);
1983 pygobject_register_version_tuples(PyObject *d)
1987 /* pygobject version */
1988 tuple = Py_BuildValue ("(iii)",
1989 PYGOBJECT_MAJOR_VERSION,
1990 PYGOBJECT_MINOR_VERSION,
1991 PYGOBJECT_MICRO_VERSION);
1992 PyDict_SetItemString(d, "pygobject_version", tuple);
1996 pygobject_register_warnings(PyObject *d)
2000 warning = PyErr_NewException("gobject.Warning", PyExc_Warning, NULL);
2001 PyDict_SetItemString(d, "Warning", warning);
2002 add_warning_redirection("GLib", warning);
2003 add_warning_redirection("GLib-GObject", warning);
2004 add_warning_redirection("GThread", warning);
2008 PYGLIB_MODULE_START(_gobject, "_gobject")
2012 d = PyModule_GetDict(module);
2013 pygobject_register_api(d);
2014 pygobject_register_constants(module);
2015 pygobject_register_features(d);
2016 pygobject_register_version_tuples(d);
2017 pygobject_register_warnings(d);
2018 pygobject_type_register_types(d);
2019 pygobject_object_register_types(d);
2020 pygobject_interface_register_types(d);
2021 pygobject_paramspec_register_types(d);
2022 pygobject_boxed_register_types(d);
2023 pygobject_pointer_register_types(d);
2024 pygobject_enum_register_types(d);
2025 pygobject_flags_register_types(d);