diff options
author | Johan Dahlin <johan@src.gnome.org> | 2004-07-18 19:30:34 +0000 |
---|---|---|
committer | Johan Dahlin <johan@src.gnome.org> | 2004-07-18 19:30:34 +0000 |
commit | 0caf54f414b108603c1e0f57f0e31489fdc31d32 (patch) | |
tree | befb5168c006019807ca94ce25012aab9ff50929 | |
parent | 3b9951be52eeac3d76755808eafcdca864bc4e74 (diff) | |
download | pygobject-0caf54f414b108603c1e0f57f0e31489fdc31d32.tar.gz pygobject-0caf54f414b108603c1e0f57f0e31489fdc31d32.tar.xz pygobject-0caf54f414b108603c1e0f57f0e31489fdc31d32.zip |
New getters
* gobject/pygflags.c (pyg_flags_get_value_nicks)
(pyg_flags_get_value_names): New getters
* gobject/pygenum.c (pyg_enum_get_value_nick)
(pyg_enum_get_value_name): New getters
* gobject/gobjectmodule.c (pyg_param_spec_getattr): add enum_class
and flags_class properties.
-rw-r--r-- | gobject/gobjectmodule.c | 23 | ||||
-rw-r--r-- | gobject/pygenum.c | 44 | ||||
-rw-r--r-- | gobject/pygenum.h | 16 | ||||
-rw-r--r-- | gobject/pygflags.c | 46 | ||||
-rw-r--r-- | gobject/pygflags.h | 14 | ||||
-rw-r--r-- | gobject/pygobject.c | 12 |
6 files changed, 128 insertions, 27 deletions
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c index 6159ca9..267ed2b 100644 --- a/gobject/gobjectmodule.c +++ b/gobject/gobjectmodule.c @@ -193,7 +193,30 @@ pyg_param_spec_getattr(PyGParamSpec *self, const gchar *attr) */ Py_INCREF(Py_None); return Py_None; + } else if (!strcmp(attr, "enum_class")) { + if (G_IS_PARAM_SPEC_ENUM(self->pspec)) { + GQuark quark; + PyObject *pyclass; + + quark = g_quark_from_static_string("PyGEnum::class"); + pyclass = (PyObject*)g_type_get_qdata(G_ENUM_CLASS_TYPE(G_PARAM_SPEC_ENUM(self->pspec)->enum_class), quark); + g_assert(pyclass != NULL); + + return pyclass; + } + } else if (!strcmp(attr, "flags_class")) { + if (G_IS_PARAM_SPEC_FLAGS(self->pspec)) { + GQuark quark; + PyObject *pyclass; + + quark = g_quark_from_static_string("PyGFlags::class"); + pyclass = (PyObject*)g_type_get_qdata(G_FLAGS_CLASS_TYPE(G_PARAM_SPEC_FLAGS(self->pspec)->flags_class), quark); + g_assert(pyclass != NULL); + + return pyclass; + } } + PyErr_SetString(PyExc_AttributeError, attr); return NULL; } diff --git a/gobject/pygenum.c b/gobject/pygenum.c index c9444f0..350de97 100644 --- a/gobject/pygenum.c +++ b/gobject/pygenum.c @@ -208,6 +208,48 @@ pyg_enum_add (PyObject * module, return stub; } +static PyObject * +pyg_enum_get_value_name(PyGEnum *self, void *closure) +{ + GEnumClass *enum_class; + GEnumValue *enum_value; + PyObject *retval; + + 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); + + retval = PyString_FromString(enum_value->value_name); + g_type_class_unref(enum_class); + + return retval; +} + +static PyObject * +pyg_enum_get_value_nick(PyGEnum *self, void *closure) +{ + GEnumClass *enum_class; + GEnumValue *enum_value; + PyObject *retval; + + 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); + + retval = PyString_FromString(enum_value->value_nick); + g_type_class_unref(enum_class); + + return retval; +} + +static PyGetSetDef pyg_enum_getsets[] = { + { "value_name", (getter)pyg_enum_get_value_name, (setter)0 }, + { "value_nick", (getter)pyg_enum_get_value_nick, (setter)0 }, + { NULL, 0, 0 } +}; + PyTypeObject PyGEnum_Type = { PyObject_HEAD_INIT(NULL) 0, @@ -239,7 +281,7 @@ PyTypeObject PyGEnum_Type = { 0, /* tp_iternext */ 0, /* tp_methods */ 0, /* tp_members */ - 0, /* tp_getset */ + pyg_enum_getsets, /* tp_getset */ &PyInt_Type, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ diff --git a/gobject/pygenum.h b/gobject/pygenum.h index cfa9a92..2f9d0d1 100644 --- a/gobject/pygenum.h +++ b/gobject/pygenum.h @@ -38,14 +38,14 @@ typedef struct { 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); +extern PyTypeObject PyGEnum_Type; + +extern PyObject * pyg_enum_add (PyObject * module, + const char * typename, + const char * strip_prefix, + GType gtype); +extern PyObject * pyg_enum_from_gtype (GType gtype, + int value); #ifdef __cplusplus } diff --git a/gobject/pygflags.c b/gobject/pygflags.c index 2c37cc1..f5f3091 100644 --- a/gobject/pygflags.c +++ b/gobject/pygflags.c @@ -283,6 +283,50 @@ pyg_flags_warn (PyObject *self, PyObject *args) return Py_None; } +static PyObject * +pyg_flags_get_value_names(PyGFlags *self, void *closure) +{ + GFlagsClass *flags_class; + PyObject *retval; + int i; + + flags_class = g_type_class_ref(self->gtype); + g_assert(G_IS_FLAGS_CLASS(flags_class)); + + retval = PyTuple_New(flags_class->n_values); + for (i = 0; i < flags_class->n_values; i++) + PyTuple_SetItem(retval, i, PyString_FromString(flags_class->values[i].value_name)); + + g_type_class_unref(flags_class); + + return retval; +} + +static PyObject * +pyg_flags_get_value_nicks(PyGFlags *self, void *closure) +{ + GFlagsClass *flags_class; + PyObject *retval; + int i; + + flags_class = g_type_class_ref(self->gtype); + g_assert(G_IS_FLAGS_CLASS(flags_class)); + + retval = PyTuple_New(flags_class->n_values); + for (i = 0; i < flags_class->n_values; i++) + PyTuple_SetItem(retval, i, PyString_FromString(flags_class->values[i].value_nick)); + + g_type_class_unref(flags_class); + + return retval; +} + +static PyGetSetDef pyg_flags_getsets[] = { + { "value_names", (getter)pyg_flags_get_value_names, (setter)0 }, + { "value_nicks", (getter)pyg_flags_get_value_nicks, (setter)0 }, + { NULL, 0, 0 } +}; + static PyNumberMethods pyg_flags_as_number = { (binaryfunc)pyg_flags_warn, /* nb_add */ (binaryfunc)pyg_flags_warn, /* nb_subtract */ @@ -355,7 +399,7 @@ PyTypeObject PyGFlags_Type = { 0, /* tp_iternext */ 0, /* tp_methods */ 0, /* tp_members */ - 0, /* tp_getset */ + pyg_flags_getsets, /* tp_getset */ &PyInt_Type, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ diff --git a/gobject/pygflags.h b/gobject/pygflags.h index 6bf93cd..584cdbf 100644 --- a/gobject/pygflags.h +++ b/gobject/pygflags.h @@ -34,16 +34,16 @@ typedef struct { GType gtype; } PyGFlags; -PyTypeObject PyGFlags_Type; +extern 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); +extern PyObject * pyg_flags_add (PyObject * module, + const char * typename, + const char * strip_prefix, + GType gtype); +extern PyObject * pyg_flags_from_gtype (GType gtype, + int value); #ifdef __cplusplus } diff --git a/gobject/pygobject.c b/gobject/pygobject.c index 6c1a6f0..8b33744 100644 --- a/gobject/pygobject.c +++ b/gobject/pygobject.c @@ -255,9 +255,8 @@ pygobject_new_with_interfaces(GType gtype) return NULL; } -#if 1 /* Workaround python tp_(get|set)attr slot inheritance bug. - * Fixes pygtk bug #144135. */ + * Fixes bug #144135. */ if (!type->tp_getattr && py_parent_type->tp_getattr) { type->tp_getattro = NULL; type->tp_getattr = py_parent_type->tp_getattr; @@ -266,19 +265,12 @@ pygobject_new_with_interfaces(GType gtype) type->tp_setattro = NULL; type->tp_setattr = py_parent_type->tp_setattr; } -#endif -#if 0 - type->tp_dealloc = (destructor)pygobject_dealloc; - type->tp_traverse = (traverseproc)pygobject_traverse; - type->tp_clear = (inquiry)pygobject_clear; - type->tp_flags |= Py_TPFLAGS_HAVE_GC; -#endif - if (PyType_Ready(type) < 0) { g_warning ("couldn't make the type `%s' ready", type->tp_name); return NULL; } + /* insert type name in module dict */ modules = PyImport_GetModuleDict(); if ((module = PyDict_GetItemString(modules, mod_name)) != NULL) { |