summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohan Dahlin <johan@src.gnome.org>2004-07-17 16:38:46 +0000
committerJohan Dahlin <johan@src.gnome.org>2004-07-17 16:38:46 +0000
commitb889f44c6adeaf10d5e1ae0a2e69ef5ee218ccad (patch)
tree6db2e280696773afe0d2d54425441ed1abc5ee4b
parent8238d9cc0fce600328f50efa139db1f575058ae8 (diff)
downloadpygobject-b889f44c6adeaf10d5e1ae0a2e69ef5ee218ccad.tar.gz
pygobject-b889f44c6adeaf10d5e1ae0a2e69ef5ee218ccad.tar.xz
pygobject-b889f44c6adeaf10d5e1ae0a2e69ef5ee218ccad.zip
gtk/*.override
* gtk/*.override * codegen/codegen.py: * codegen/argtypes.py: Update for enums * pygtype.c (pyg_value_as_pyobject): Use new enum/flag functions * pygenum.[ch]: Handle GFlag * pygflags.[ch]: Handle GFlag * atk-types.defs: Fix gtype-id for all enums * gobjectmodule.c (initgobject): Clean up and add convinience macros * tests/enum.py: New file * tests/common.py: New file, also stole from gst-python
-rw-r--r--gobject/gobjectmodule.c84
-rw-r--r--gobject/pygenum.c252
-rw-r--r--gobject/pygenum.h54
-rw-r--r--gobject/pygflags.c367
-rw-r--r--gobject/pygflags.h52
-rw-r--r--gobject/pygobject-private.h3
-rw-r--r--gobject/pygobject.h29
-rw-r--r--gobject/pygtype.c7
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/common.py16
-rw-r--r--tests/conversion.py2
-rw-r--r--tests/runtests.py2
-rw-r--r--tests/signal.py2
13 files changed, 815 insertions, 56 deletions
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c
index ec9fcf4..6159ca9 100644
--- a/gobject/gobjectmodule.c
+++ b/gobject/gobjectmodule.c
@@ -26,6 +26,9 @@
#include "pygobject-private.h"
+#include "pygenum.h"
+#include "pygflags.h"
+
static PyObject *gerror_exc = NULL;
static const gchar *pyginterface_type_id = "PyGInterface::type";
GQuark pyginterface_type_key = 0;
@@ -1880,7 +1883,7 @@ static PyMethodDef pygobject_functions[] = {
*
* Returns: the stripped constant name.
*/
-static char *
+char *
pyg_constant_strip_prefix(gchar *name, const gchar *strip_prefix)
{
gint prefix_len;
@@ -2158,12 +2161,35 @@ struct _PyGObject_Functions pygobject_api_functions = {
&PyGParamSpec_Type,
pyg_param_spec_new,
pyg_param_spec_from_object,
+
pyg_pyobj_to_unichar_conv,
pyg_parse_constructor_args,
pyg_param_gvalue_as_pyobject,
- pyg_param_gvalue_from_pyobject
+ pyg_param_gvalue_from_pyobject,
+
+ &PyGEnum_Type,
+ pyg_enum_add,
+ pyg_enum_from_gtype,
+
+ &PyGFlags_Type,
+ pyg_flags_add,
+ pyg_flags_from_gtype
};
+#define REGISTER_TYPE(d, type, name) \
+ type.ob_type = &PyType_Type; \
+ type.tp_alloc = PyType_GenericAlloc; \
+ type.tp_new = PyType_GenericNew; \
+ if (PyType_Ready(&type)) \
+ return; \
+ PyDict_SetItemString(d, name, (PyObject *)&type);
+
+#define REGISTER_GTYPE(d, type, name, gtype) \
+ REGISTER_TYPE(d, type, name); \
+ PyDict_SetItemString(type.tp_dict, "__gtype__", \
+ o=pyg_type_wrapper_new(gtype)); \
+ Py_DECREF(o);
+
DL_EXPORT(void)
initgobject(void)
{
@@ -2188,7 +2214,7 @@ initgobject(void)
gerror_exc = PyErr_NewException("gobject.GError", PyExc_RuntimeError,NULL);
PyDict_SetItemString(d, "GError", gerror_exc);
-
+
PyGObject_Type.tp_alloc = PyType_GenericAlloc;
PyGObject_Type.tp_new = PyType_GenericNew;
pygobject_register_class(d, "GObject", G_TYPE_OBJECT,
@@ -2196,55 +2222,23 @@ initgobject(void)
PyDict_SetItemString(PyGObject_Type.tp_dict, "__gdoc__",
pyg_object_descr_doc_get());
- PyGInterface_Type.ob_type = &PyType_Type;
- PyGInterface_Type.tp_alloc = PyType_GenericAlloc;
- PyGInterface_Type.tp_new = PyType_GenericNew;
- if (PyType_Ready(&PyGInterface_Type))
- return;
- PyDict_SetItemString(d, "GInterface", (PyObject *)&PyGInterface_Type);
- PyDict_SetItemString(PyGInterface_Type.tp_dict, "__gtype__",
- o=pyg_type_wrapper_new(G_TYPE_INTERFACE));
- Py_DECREF(o);
+ REGISTER_GTYPE(d, PyGInterface_Type, "GInterface", G_TYPE_INTERFACE);
PyDict_SetItemString(PyGInterface_Type.tp_dict, "__doc__",
pyg_object_descr_doc_get());
PyDict_SetItemString(PyGInterface_Type.tp_dict, "__gdoc__",
pyg_object_descr_doc_get());
pyginterface_type_key = g_quark_from_static_string(pyginterface_type_id);
- PyGBoxed_Type.ob_type = &PyType_Type;
- PyGBoxed_Type.tp_alloc = PyType_GenericAlloc;
- PyGBoxed_Type.tp_new = PyType_GenericNew;
- if (PyType_Ready(&PyGBoxed_Type))
- return;
- PyDict_SetItemString(d, "GBoxed", (PyObject *)&PyGBoxed_Type);
- PyDict_SetItemString(PyGBoxed_Type.tp_dict, "__gtype__",
- o=pyg_type_wrapper_new(G_TYPE_BOXED));
- Py_DECREF(o);
+ REGISTER_GTYPE(d, PyGBoxed_Type, "GBoxed", G_TYPE_BOXED);
+ REGISTER_GTYPE(d, PyGPointer_Type, "GPointer", G_TYPE_POINTER);
+ REGISTER_GTYPE(d, PyGEnum_Type, "GEnum", G_TYPE_ENUM);
+ PyGEnum_Type.tp_base = &PyInt_Type;
+ REGISTER_GTYPE(d, PyGFlags_Type, "GFlags", G_TYPE_FLAGS);
+ PyGFlags_Type.tp_base = &PyInt_Type;
- PyGMainLoop_Type.ob_type = &PyType_Type;
- PyGMainLoop_Type.tp_alloc = PyType_GenericAlloc;
- PyGMainLoop_Type.tp_new = PyType_GenericNew;
- if (PyType_Ready(&PyGMainLoop_Type))
- return;
- PyDict_SetItemString(d, "MainLoop", (PyObject *)&PyGMainLoop_Type);
+ REGISTER_TYPE(d, PyGMainLoop_Type, "GMainLoop");
+ REGISTER_TYPE(d, PyGMainContext_Type, "GMainContext");
- PyGMainContext_Type.ob_type = &PyType_Type;
- PyGMainContext_Type.tp_alloc = PyType_GenericAlloc;
- PyGMainContext_Type.tp_new = PyType_GenericNew;
- if (PyType_Ready(&PyGMainContext_Type))
- return;
- PyDict_SetItemString(d, "MainContext", (PyObject *)&PyGMainContext_Type);
-
- PyGPointer_Type.ob_type = &PyType_Type;
- PyGPointer_Type.tp_alloc = PyType_GenericAlloc;
- PyGPointer_Type.tp_new = PyType_GenericNew;
- if (PyType_Ready(&PyGPointer_Type))
- return;
- PyDict_SetItemString(d, "GPointer", (PyObject *)&PyGPointer_Type);
- PyDict_SetItemString(PyGPointer_Type.tp_dict, "__gtype__",
- o=pyg_type_wrapper_new(G_TYPE_POINTER));
- Py_DECREF(o);
-
/* glib version */
tuple = Py_BuildValue ("(iii)", glib_major_version, glib_minor_version,
glib_micro_version);
@@ -2281,7 +2275,7 @@ initgobject(void)
PyModule_AddIntConstant(m, "PRIORITY_HIGH", G_PRIORITY_HIGH);
PyModule_AddIntConstant(m, "PRIORITY_DEFAULT", G_PRIORITY_DEFAULT);
PyModule_AddIntConstant(m, "PRIORITY_HIGH_IDLE", G_PRIORITY_HIGH_IDLE);
- PyModule_AddIntConstant(m,"PRIORITY_DEFAULT_IDLE",G_PRIORITY_DEFAULT_IDLE);
+ PyModule_AddIntConstant(m, "PRIORITY_DEFAULT_IDLE",G_PRIORITY_DEFAULT_IDLE);
PyModule_AddIntConstant(m, "PRIORITY_LOW", G_PRIORITY_LOW);
PyModule_AddIntConstant(m, "IO_IN", G_IO_IN);
diff --git a/gobject/pygenum.c b/gobject/pygenum.c
new file mode 100644
index 0000000..c9444f0
--- /dev/null
+++ b/gobject/pygenum.c
@@ -0,0 +1,252 @@
+/* -*- Mode: C; c-basic-offset: 4 -*-
+ * pygtk- Python bindings for the GTK toolkit.
+ * Copyright (C) 1998-2003 James Henstridge
+ * Copyright (C) 2004 Johan Dahlin
+ *
+ * pygenum.c: GEnum and GFlag wrappers
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "pygobject-private.h"
+#include "pygenum.h"
+
+static const gchar *pygenum_class_id = "PyGEnum::class";
+static GQuark pygenum_class_key = 0;
+
+#define GET_INT(x) (((PyIntObject*)x)->ob_ival)
+static int
+pyg_enum_compare(PyGEnum *self, PyObject *other)
+{
+ if (!PyInt_CheckExact(other) && ((PyGEnum*)other)->gtype != self->gtype) {
+ PyErr_Warn(PyExc_Warning, "comparing different enum types");
+ return -1;
+ }
+
+ if (GET_INT(self) == GET_INT(other))
+ return 0;
+ else if (GET_INT(self) > GET_INT(other))
+ return -1;
+ else
+ return 1;
+}
+#undef GET_INT
+
+static PyObject *
+pyg_enum_repr(PyGEnum *self)
+{
+ GEnumClass *enum_class;
+ GEnumValue *enum_value;
+ static char tmp[256];
+
+ enum_class = g_type_class_ref(self->gtype);
+ g_assert(G_IS_ENUM_CLASS(enum_class));
+
+ enum_value = g_enum_get_value(enum_class, self->parent.ob_ival);
+ g_assert(enum_value != 0);
+
+ sprintf(tmp, "<enum %s of type %s>", enum_value->value_name, g_type_name(self->gtype));
+
+ g_type_class_unref(enum_class);
+
+ return PyString_FromString(tmp);
+}
+
+static PyObject *
+pyg_enum_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+ static char *kwlist[] = { "value", NULL };
+ long value;
+ PyObject *pytc, *values, *ret;
+ GType gtype;
+ GEnumClass *eclass;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "l", kwlist, &value))
+ return NULL;
+
+ pytc = PyObject_GetAttrString((PyObject *)type, "__gtype__");
+ if (!pytc)
+ return NULL;
+
+ if (!PyObject_TypeCheck(pytc, &PyGEnum_Type)) {
+ Py_DECREF(pytc);
+ PyErr_SetString(PyExc_TypeError,
+ "__gtype__ attribute not a typecode");
+ return NULL;
+ }
+
+ gtype = pyg_type_from_object(pytc);
+ Py_DECREF(pytc);
+
+ eclass = G_ENUM_CLASS(g_type_class_ref(gtype));
+
+ if (value < 0 || value > eclass->n_values) {
+ PyErr_SetString(PyExc_ValueError, "value out of range");
+ g_type_class_unref(eclass);
+ return NULL;
+ }
+
+ values = PyObject_GetAttrString((PyObject *)type, "__enum_values__");
+ if (!values) {
+ g_type_class_unref(eclass);
+ return NULL;
+ }
+
+ if (!PyTuple_Check(values) || PyTuple_Size(values) != eclass->n_values) {
+ PyErr_SetString(PyExc_TypeError, "__enum_values__ badly formed");
+ Py_DECREF(values);
+ g_type_class_unref(eclass);
+ return NULL;
+ }
+
+ g_type_class_unref(eclass);
+
+ ret = PyTuple_GetItem(values, value);
+ Py_INCREF(ret);
+ Py_DECREF(values);
+ return ret;
+}
+
+PyObject*
+pyg_enum_from_gtype (GType gtype, int value)
+{
+ PyObject *pyclass, *values, *retval;
+
+ g_return_val_if_fail(gtype != G_TYPE_INVALID, NULL);
+
+ pyclass = (PyObject*)g_type_get_qdata(gtype, pygenum_class_key);
+ g_assert(pyclass != NULL);
+
+ values = PyDict_GetItemString(((PyTypeObject *)pyclass)->tp_dict,
+ "__enum_values__");
+ retval = PyTuple_GetItem(values, value);
+ Py_INCREF(retval);
+
+ return retval;
+}
+
+PyObject *
+pyg_enum_add (PyObject * module,
+ const char * typename,
+ const char * strip_prefix,
+ GType gtype)
+{
+ PyObject *instance_dict, *stub, *values;
+ GEnumClass *eclass;
+ int i;
+
+ g_return_val_if_fail(module != NULL, NULL);
+ g_return_val_if_fail(typename != NULL, NULL);
+ g_return_val_if_fail(g_type_is_a(gtype, G_TYPE_ENUM), NULL);
+
+ instance_dict = PyDict_New();
+ stub = PyObject_CallFunction((PyObject *)&PyType_Type, "s(O)O",
+ typename, (PyObject *)&PyGEnum_Type,
+ instance_dict);
+ Py_DECREF(instance_dict);
+ if (!stub) {
+ PyErr_SetString(PyExc_RuntimeError, "can't create const");
+ return NULL;
+ }
+
+ PyDict_SetItemString(((PyTypeObject *)stub)->tp_dict,
+ "__module__",
+ PyString_FromString(PyModule_GetName(module)));
+
+ if (!pygenum_class_key)
+ pygenum_class_key = g_quark_from_static_string(pygenum_class_id);
+
+ g_type_set_qdata(gtype, pygenum_class_key, stub);
+
+ /* Add it to the module name space */
+ PyModule_AddObject(module, (char*)typename, stub);
+ Py_INCREF(stub);
+
+ /* Register enum values */
+ eclass = G_ENUM_CLASS(g_type_class_ref(gtype));
+
+ values = PyTuple_New(eclass->n_values);
+ for (i = 0; i < eclass->n_values; i++) {
+ PyObject *item;
+
+ item = ((PyTypeObject *)stub)->tp_alloc((PyTypeObject *)stub, 0);
+ ((PyIntObject*)item)->ob_ival = eclass->values[i].value;
+ ((PyGEnum*)item)->gtype = gtype;
+
+ PyTuple_SetItem(values, i, item);
+
+ PyModule_AddObject(module,
+ pyg_constant_strip_prefix(eclass->values[i].value_name,
+ strip_prefix),
+ item);
+ Py_INCREF(item);
+ }
+
+ PyDict_SetItemString(((PyTypeObject *)stub)->tp_dict,
+ "__enum_values__", values);
+ Py_DECREF(values);
+
+ g_type_class_unref(eclass);
+
+ return stub;
+}
+
+PyTypeObject PyGEnum_Type = {
+ PyObject_HEAD_INIT(NULL)
+ 0,
+ "gobject.GEnum",
+ sizeof(PyGEnum),
+ 0,
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ (cmpfunc)pyg_enum_compare, /* tp_compare */
+ (reprfunc)pyg_enum_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ (reprfunc)pyg_enum_repr, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ &PyInt_Type, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ pyg_enum_new, /* tp_new */
+};
+
diff --git a/gobject/pygenum.h b/gobject/pygenum.h
new file mode 100644
index 0000000..cfa9a92
--- /dev/null
+++ b/gobject/pygenum.h
@@ -0,0 +1,54 @@
+/* -*- Mode: C; c-basic-offset: 4 -*-
+ * pygtk- Python bindings for the GTK toolkit.
+ * Copyright (C) 1998-2003 James Henstridge
+ * Copyright (C) 2004 Johan Dahlin
+ *
+ * pygenum.c: GEnum and GFlag wrappers
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#ifndef __PYGENUM_H__
+#define __PYGENUM_H__
+
+#include <Python.h>
+#include <glib-object.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#define PyGEnum_Check(x) (g_type_is_a(((PyGFlags*)x)->gtype, G_TYPE_ENUM))
+
+typedef struct {
+ PyIntObject parent;
+ GType gtype;
+} PyGEnum;
+
+PyTypeObject PyGEnum_Type;
+
+PyObject * pyg_enum_add (PyObject * module,
+ const char * typename,
+ const char * strip_prefix,
+ GType gtype);
+PyObject * pyg_enum_from_gtype (GType gtype,
+ int value);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __PYGENUM_H__ */
diff --git a/gobject/pygflags.c b/gobject/pygflags.c
new file mode 100644
index 0000000..2c37cc1
--- /dev/null
+++ b/gobject/pygflags.c
@@ -0,0 +1,367 @@
+/* -*- Mode: C; c-basic-offset: 4 -*-
+ * pygtk- Python bindings for the GTK toolkit.
+ * Copyright (C) 1998-2003 James Henstridge
+ * Copyright (C) 2004 Johan Dahlin
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "pygobject-private.h"
+#include "pygflags.h"
+
+static const gchar *pygflags_class_id = "PyGFlags::class";
+static GQuark pygflags_class_key = 0;
+
+#define GET_INT_VALUE(x) (((PyIntObject*)x)->ob_ival)
+static int
+pyg_flags_compare(PyGFlags *self, PyObject *other)
+{
+ if (!PyInt_CheckExact(other) && ((PyGFlags*)other)->gtype != self->gtype) {
+ PyErr_Warn(PyExc_Warning, "comparing different flags types");
+ return -1;
+ }
+
+ if (GET_INT_VALUE(self) == GET_INT_VALUE(other))
+ return 0;
+ else if (GET_INT_VALUE(self) > GET_INT_VALUE(other))
+ return -1;
+ else
+ return 1;
+}
+
+static char *
+generate_repr(GType gtype, int value)
+{
+ GFlagsClass *flags_class;
+ char *retval = NULL, *tmp;
+ int i;
+
+ flags_class = g_type_class_ref(gtype);
+ g_assert(G_IS_FLAGS_CLASS(flags_class));
+
+ for (i = 0; i < flags_class->n_values; i++) {
+ if ((value & flags_class->values[i].value) == flags_class->values[i].value) {
+ if (retval) {
+ tmp = g_strdup_printf("%s | %s", retval, flags_class->values[i].value_name);
+ g_free(retval);
+ retval = tmp;
+ } else {
+ retval = g_strdup_printf("%s", flags_class->values[i].value_name);
+ }
+ }
+ }
+
+ g_type_class_unref(flags_class);
+
+ return retval;
+}
+
+static PyObject *
+pyg_flags_repr(PyGFlags *self)
+{
+ char *tmp, *retval;
+ PyObject *pyretval;
+
+ tmp = generate_repr(self->gtype,
+ self->parent.ob_ival);
+
+ retval = g_strdup_printf("<flag %s of type %s>", tmp,
+ g_type_name(self->gtype));
+ g_free(tmp);
+
+ pyretval = PyString_FromString(retval);
+ g_free(retval);
+
+ return pyretval;
+}
+
+static PyObject *
+pyg_flags_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+ static char *kwlist[] = { "value", NULL };
+ long value;
+ PyObject *pytc, *values, *ret;
+ GType gtype;
+ GFlagsClass *eclass;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "l", kwlist, &value))
+ return NULL;
+
+ pytc = PyObject_GetAttrString((PyObject *)type, "__gtype__");
+ if (!pytc)
+ return NULL;
+
+ if (!PyObject_TypeCheck(pytc, &PyGFlags_Type)) {
+ Py_DECREF(pytc);
+ PyErr_SetString(PyExc_TypeError,
+ "__gtype__ attribute not a typecode");
+ return NULL;
+ }
+
+ gtype = pyg_type_from_object(pytc);
+ Py_DECREF(pytc);
+
+ eclass = G_FLAGS_CLASS(g_type_class_ref(gtype));
+
+ if (value < 0 || value > eclass->n_values) {
+ PyErr_SetString(PyExc_ValueError, "value out of range");
+ g_type_class_unref(eclass);
+ return NULL;
+ }
+
+ values = PyObject_GetAttrString((PyObject *)type, "__flags_values__");
+ if (!values) {
+ g_type_class_unref(eclass);
+ return NULL;
+ }
+
+ if (!PyTuple_Check(values) || PyTuple_Size(values) != eclass->n_values) {
+ PyErr_SetString(PyExc_TypeError, "__flags_values__ badly formed");
+ Py_DECREF(values);
+ g_type_class_unref(eclass);
+ return NULL;
+ }
+
+ g_type_class_unref(eclass);
+
+ ret = PyTuple_GetItem(values, value);
+ Py_INCREF(ret);
+ Py_DECREF(values);
+ return ret;
+}
+
+PyObject*
+pyg_flags_from_gtype (GType gtype, int value)
+{
+ PyObject *pyclass, *values, *retval;
+
+ g_return_val_if_fail(gtype != G_TYPE_INVALID, NULL);
+
+ pyclass = (PyObject*)g_type_get_qdata(gtype, pygflags_class_key);
+ g_assert(pyclass != NULL);
+
+ values = PyDict_GetItemString(((PyTypeObject *)pyclass)->tp_dict,
+ "__flags_values__");
+ retval = PyTuple_GetItem(values, value);
+ if (!retval) {
+ PyErr_Clear();
+
+ retval = ((PyTypeObject *)pyclass)->tp_alloc((PyTypeObject *)pyclass, 0);
+ g_assert(retval != NULL);
+
+ ((PyIntObject*)retval)->ob_ival = value;
+ ((PyGFlags*)retval)->gtype = gtype;
+ }
+
+ Py_INCREF(retval);
+ return retval;
+}
+
+PyObject *
+pyg_flags_add (PyObject * module,
+ const char * typename,
+ const char * strip_prefix,
+ GType gtype)
+{
+ PyObject *instance_dict, *stub, *values;
+ GFlagsClass *eclass;
+ int i;
+
+ g_return_val_if_fail(module != NULL, NULL);
+ g_return_val_if_fail(typename != NULL, NULL);
+ g_return_val_if_fail(g_type_is_a(gtype, G_TYPE_FLAGS), NULL);
+
+ instance_dict = PyDict_New();
+ stub = PyObject_CallFunction((PyObject *)&PyType_Type, "s(O)O",
+ typename, (PyObject *)&PyGFlags_Type,
+ instance_dict);
+ Py_DECREF(instance_dict);
+ if (!stub) {
+ PyErr_SetString(PyExc_RuntimeError, "can't create const");
+ return NULL;
+ }
+
+ PyDict_SetItemString(((PyTypeObject *)stub)->tp_dict,
+ "__module__",
+ PyString_FromString(PyModule_GetName(module)));
+
+ /* Add it to the module name space */
+ PyModule_AddObject(module, (char*)typename, stub);
+ Py_INCREF(stub);
+
+ if (!pygflags_class_key)
+ pygflags_class_key = g_quark_from_static_string(pygflags_class_id);
+
+ g_type_set_qdata(gtype, pygflags_class_key, stub);
+
+ /* Register flag values */
+ eclass = G_FLAGS_CLASS(g_type_class_ref(gtype));
+
+ values = PyTuple_New(eclass->n_values);
+ for (i = 0; i < eclass->n_values; i++) {
+ PyObject *item;
+
+ item = ((PyTypeObject *)stub)->tp_alloc((PyTypeObject *)stub, 0);
+ ((PyIntObject*)item)->ob_ival = eclass->values[i].value;
+ ((PyGFlags*)item)->gtype = gtype;
+
+ PyTuple_SetItem(values, i, item);
+
+ PyModule_AddObject(module,
+ pyg_constant_strip_prefix(eclass->values[i].value_name,
+ strip_prefix),
+ item);
+ Py_INCREF(item);
+ }
+
+ PyDict_SetItemString(((PyTypeObject *)stub)->tp_dict,
+ "__flags_values__", values);
+ Py_DECREF(values);
+
+ g_type_class_unref(eclass);
+
+ return stub;
+}
+
+static PyObject *
+pyg_flags_and(PyGFlags *a, PyGFlags *b)
+{
+ if (!PyGFlags_Check(a) || !PyGFlags_Check(b))
+ return PyInt_Type.tp_as_number->nb_and((PyObject*)a,
+ (PyObject*)b);
+
+ return pyg_flags_from_gtype(a->gtype,
+ GET_INT_VALUE(a) & GET_INT_VALUE(b));
+}
+
+static PyObject *
+pyg_flags_or(PyGFlags *a, PyGFlags *b)
+{
+ if (!PyGFlags_Check(a) || !PyGFlags_Check(b))
+ return PyInt_Type.tp_as_number->nb_or((PyObject*)a,
+ (PyObject*)b);
+
+ return pyg_flags_from_gtype(a->gtype, GET_INT_VALUE(a) | GET_INT_VALUE(b));
+}
+
+static PyObject *
+pyg_flags_xor(PyGFlags *a, PyGFlags *b)
+{
+ if (!PyGFlags_Check(a) || !PyGFlags_Check(b))
+ return PyInt_Type.tp_as_number->nb_xor((PyObject*)a,
+ (PyObject*)b);
+
+ return pyg_flags_from_gtype(a->gtype,
+ GET_INT_VALUE(a) ^ GET_INT_VALUE(b));
+
+}
+
+static PyObject *
+pyg_flags_warn (PyObject *self, PyObject *args)
+{
+ if (PyErr_Warn(PyExc_Warning, "unsupported arithmetic operation for flags type"))
+ return NULL;
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+static PyNumberMethods pyg_flags_as_number = {
+ (binaryfunc)pyg_flags_warn, /* nb_add */
+ (binaryfunc)pyg_flags_warn, /* nb_subtract */
+ (binaryfunc)pyg_flags_warn, /* nb_multiply */
+ (binaryfunc)pyg_flags_warn, /* nb_divide */
+ (binaryfunc)pyg_flags_warn, /* nb_remainder */
+ (binaryfunc)pyg_flags_warn, /* nb_divmod */
+ (ternaryfunc)pyg_flags_warn, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* nb_positive */
+ 0, /* nb_absolute */
+ 0, /* nb_nonzero */
+ 0, /* nb_invert */
+ 0, /* nb_lshift */
+ 0, /* nb_rshift */
+ (binaryfunc)pyg_flags_and, /* nb_and */
+ (binaryfunc)pyg_flags_xor, /* nb_xor */
+ (binaryfunc)pyg_flags_or, /* nb_or */
+ 0, /* nb_coerce */
+ 0, /* nb_int */
+ 0, /* nb_long */
+ 0, /* nb_float */
+ 0, /* nb_oct */
+ 0, /* nb_hex */
+ 0, /* nb_inplace_add */
+ 0, /* nb_inplace_subtract */
+ 0, /* nb_inplace_multiply */
+ 0, /* nb_inplace_divide */
+ 0, /* nb_inplace_remainder */
+ 0, /* nb_inplace_power */
+ 0, /* nb_inplace_lshift */
+ 0, /* nb_inplace_rshift */
+ 0, /* nb_inplace_and */
+ 0, /* nb_inplace_xor */
+ 0, /* nb_inplace_or */
+ 0, /* nb_floor_divide */
+ 0, /* nb_true_divide */
+ 0, /* nb_inplace_floor_divide */
+ 0, /* nb_inplace_true_divide */
+};
+
+PyTypeObject PyGFlags_Type = {
+ PyObject_HEAD_INIT(NULL)
+ 0,
+ "gobject.GFlags",
+ sizeof(PyGFlags),
+ 0,
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ (cmpfunc)pyg_flags_compare, /* tp_compare */
+ (reprfunc)pyg_flags_repr, /* tp_repr */
+ &pyg_flags_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ (reprfunc)pyg_flags_repr, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ &PyInt_Type, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ pyg_flags_new, /* tp_new */
+};
diff --git a/gobject/pygflags.h b/gobject/pygflags.h
new file mode 100644
index 0000000..6bf93cd
--- /dev/null
+++ b/gobject/pygflags.h
@@ -0,0 +1,52 @@
+/* -*- Mode: C; c-basic-offset: 4 -*-
+ * pygtk- Python bindings for the GTK toolkit.
+ * Copyright (C) 1998-2003 James Henstridge
+ * Copyright (C) 2004 Johan Dahlin
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#ifndef __PYGFLAGS_H__
+#define __PYGFLAGS_H__
+
+#include <Python.h>
+#include <glib-object.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+typedef struct {
+ PyIntObject parent;
+ GType gtype;
+} PyGFlags;
+
+PyTypeObject PyGFlags_Type;
+
+#define PyGFlags_Check(x) (g_type_is_a(((PyGFlags*)x)->gtype, G_TYPE_FLAGS))
+
+PyObject * pyg_flags_add (PyObject * module,
+ const char * typename,
+ const char * strip_prefix,
+ GType gtype);
+PyObject * pyg_flags_from_gtype (GType gtype,
+ int value);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __PYGFLAGS_H__ */
diff --git a/gobject/pygobject-private.h b/gobject/pygobject-private.h
index 38eff22..da340c7 100644
--- a/gobject/pygobject-private.h
+++ b/gobject/pygobject-private.h
@@ -8,7 +8,6 @@
#define _INSIDE_PYGOBJECT_
#include "pygobject.h"
-
/* from gobjectmodule.c */
extern struct _PyGObject_Functions pygobject_api_functions;
#define pyg_block_threads() G_STMT_START { \
@@ -108,4 +107,6 @@ PyObject * pyg_pointer_new (GType pointer_type, gpointer pointer);
extern PyTypeObject PyGParamSpec_Type;
PyObject *pyg_param_spec_new (GParamSpec *pspec);
+extern char * pyg_constant_strip_prefix(gchar *name, const gchar *strip_prefix);
+
#endif
diff --git a/gobject/pygobject.h b/gobject/pygobject.h
index 594e285..9292cec 100644
--- a/gobject/pygobject.h
+++ b/gobject/pygobject.h
@@ -29,15 +29,15 @@ typedef struct {
gboolean free_on_dealloc;
} PyGBoxed;
+#define pyg_boxed_get(v,t) ((t *)((PyGBoxed *)(v))->boxed)
+#define pyg_boxed_check(v,typecode) (PyObject_TypeCheck(v, &PyGBoxed_Type) && ((PyGBoxed *)(v))->gtype == typecode)
+
typedef struct {
PyObject_HEAD
gpointer pointer;
GType gtype;
} PyGPointer;
-#define pyg_boxed_get(v,t) ((t *)((PyGBoxed *)(v))->boxed)
-#define pyg_boxed_check(v,typecode) (PyObject_TypeCheck(v, &PyGBoxed_Type) && ((PyGBoxed *)(v))->gtype == typecode)
-
#define pyg_pointer_get(v,t) ((t *)((PyGPointer *)(v))->pointer)
#define pyg_pointer_check(v,typecode) (PyObject_TypeCheck(v, &PyGPointer_Type) && ((PyGPointer *)(v))->gtype == typecode)
@@ -117,13 +117,25 @@ struct _PyGObject_Functions {
GParameter *params,
guint *nparams,
PyObject **py_args);
- PyObject* (* param_gvalue_as_pyobject) (const GValue* gvalue,
+ PyObject *(* param_gvalue_as_pyobject) (const GValue* gvalue,
gboolean copy_boxed,
const GParamSpec* pspec);
int (* gvalue_from_param_pyobject) (GValue* value,
PyObject* py_obj,
const GParamSpec* pspec);
-
+ PyTypeObject *enum_type;
+ PyObject *(*enum_add)(PyObject *module,
+ const char *typename,
+ const char *strip_prefix,
+ GType gtype);
+ PyObject* (*enum_from_gtype)(GType gtype, int value);
+
+ PyTypeObject *flags_type;
+ PyObject *(*flags_add)(PyObject *module,
+ const char *typename,
+ const char *strip_prefix,
+ GType gtype);
+ PyObject* (*flags_from_gtype)(GType gtype, int value);
};
#ifndef _INSIDE_PYGOBJECT_
@@ -168,6 +180,13 @@ struct _PyGObject_Functions *_PyGObject_API;
#define pyg_parse_constructor_args (_PyGObject_API->parse_constructor_args)
#define pyg_param_gvalue_from_pyobject (_PyGObject_API->value_from_pyobject)
#define pyg_param_gvalue_as_pyobject (_PyGObject_API->value_as_pyobject)
+#define PyGEnum_Type (*_PyGObject_API->enum_type)
+#define pyg_enum_add (_PyGObject_API->enum_add)
+#define pyg_enum_from_gtype (_PyGObject_API->enum_from_gtype)
+#define PyGFlags_Type (*_PyGObject_API->flags_type)
+#define pyg_flags_add (_PyGObject_API->flags_add)
+#define pyg_flags_from_gtype (_PyGObject_API->flags_from_gtype)
+
#define pyg_block_threads() G_STMT_START { \
if (_PyGObject_API->block_threads != NULL) \
(* _PyGObject_API->block_threads)(); \
diff --git a/gobject/pygtype.c b/gobject/pygtype.c
index e28c84c..eff5e0d 100644
--- a/gobject/pygtype.c
+++ b/gobject/pygtype.c
@@ -22,6 +22,9 @@
#include "pygobject-private.h"
+#include "pygenum.h"
+#include "pygflags.h"
+
/* -------------- __gtype__ objects ---------------------------- */
typedef struct {
@@ -652,9 +655,9 @@ pyg_value_as_pyobject(const GValue *value, gboolean copy_boxed)
return PyLong_FromUnsignedLongLong(val);
}
case G_TYPE_ENUM:
- return PyInt_FromLong(g_value_get_enum(value));
+ return pyg_enum_from_gtype(G_VALUE_TYPE(value), g_value_get_enum(value));
case G_TYPE_FLAGS:
- return PyInt_FromLong(g_value_get_flags(value));
+ return pyg_flags_from_gtype(G_VALUE_TYPE(value), g_value_get_flags(value));
case G_TYPE_FLOAT:
return PyFloat_FromDouble(g_value_get_float(value));
case G_TYPE_DOUBLE:
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8324aef..c592b96 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,5 +1,6 @@
tests = \
conversion.py \
+ enum.py \
signal.py
check-local:
diff --git a/tests/common.py b/tests/common.py
new file mode 100644
index 0000000..ff2eef9
--- /dev/null
+++ b/tests/common.py
@@ -0,0 +1,16 @@
+import os
+import sys
+
+# Don't insert before . (first in list)
+sys.path.insert(1, os.path.join('..'))
+
+path = os.path.abspath(os.path.join('..', 'gtk'))
+import gobject
+import atk
+import pango
+import gtk
+from gtk import gdk
+from gtk import glade
+
+import ltihooks
+ltihooks.uninstall()
diff --git a/tests/conversion.py b/tests/conversion.py
index 1fe8c22..dece7bd 100644
--- a/tests/conversion.py
+++ b/tests/conversion.py
@@ -2,7 +2,7 @@
import unittest
-import gtk
+from common import gtk
class Tests(unittest.TestCase):
diff --git a/tests/runtests.py b/tests/runtests.py
index f3559f5..83b6013 100644
--- a/tests/runtests.py
+++ b/tests/runtests.py
@@ -4,7 +4,7 @@ import os
import sys
import unittest
-SKIP_FILES = ['runtests']
+SKIP_FILES = ['common', 'runtests']
dir = os.path.split(os.path.abspath(__file__))[0]
os.chdir(dir)
diff --git a/tests/signal.py b/tests/signal.py
index ce32801..65eda26 100644
--- a/tests/signal.py
+++ b/tests/signal.py
@@ -2,7 +2,7 @@
import unittest
-import gobject
+from common import gobject
class C(gobject.GObject):
__gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,