diff options
author | Manish Singh <yosh@gimp.org> | 2005-11-03 19:15:23 +0000 |
---|---|---|
committer | Johan Dahlin <johan@src.gnome.org> | 2005-11-03 19:15:23 +0000 |
commit | 55d03f1c08d4f7624732fab642b4c25a0e76e9d3 (patch) | |
tree | 2a1828f654b93eba33dbbc8bfc22367ff7294340 /gobject | |
parent | d0449bfb71c391fd5b4ac9bc7d344639eda80332 (diff) | |
download | pygobject-55d03f1c08d4f7624732fab642b4c25a0e76e9d3.tar.gz |
reviewed by: Johan Dahlin <jdahlin@async.com.br>
2005-11-03 Manish Singh <yosh@gimp.org>
reviewed by: Johan Dahlin <jdahlin@async.com.br>
* gobject/gobjectmodule.c: (pyg_integer_richcompare):
* gobject/pygenum.c: (pyg_enum_richcompare):
* gobject/pygflags.c: (pyg_flags_richcompare):
* gobject/pygobject-private.h:
Prepare for Python 2.5 richcompare changes, fixes #320455.
Diffstat (limited to 'gobject')
-rw-r--r-- | gobject/gobjectmodule.c | 23 | ||||
-rw-r--r-- | gobject/pygenum.c | 31 | ||||
-rw-r--r-- | gobject/pygflags.c | 25 | ||||
-rw-r--r-- | gobject/pygobject-private.h | 4 |
4 files changed, 54 insertions, 29 deletions
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c index 98956880..f2a7d2e0 100644 --- a/gobject/gobjectmodule.c +++ b/gobject/gobjectmodule.c @@ -2619,6 +2619,29 @@ pyg_set_object_has_new_constructor(GType type) g_type_set_qdata(type, pygobject_has_updated_constructor_key, GINT_TO_POINTER(1)); } +#define GET_INT(x) (((PyIntObject*)x)->ob_ival) +PyObject * +pyg_integer_richcompare(PyObject *v, PyObject *w, int op) +{ + PyObject *result; + gboolean t; + + switch (op) { + case Py_EQ: t = GET_INT(v) == GET_INT(w); break; + case Py_NE: t = GET_INT(v) != GET_INT(w); break; + case Py_LE: t = GET_INT(v) <= GET_INT(w); break; + case Py_GE: t = GET_INT(v) >= GET_INT(w); break; + case Py_LT: t = GET_INT(v) < GET_INT(w); break; + case Py_GT: t = GET_INT(v) > GET_INT(w); break; + default: g_assert_not_reached(); + } + + result = t ? Py_True : Py_False; + Py_INCREF(result); + return result; +} +#undef GET_INT + static void _log_func(const gchar *log_domain, GLogLevelFlags log_level, diff --git a/gobject/pygenum.c b/gobject/pygenum.c index aceba9d2..aa8907eb 100644 --- a/gobject/pygenum.c +++ b/gobject/pygenum.c @@ -27,25 +27,22 @@ #include "pygobject-private.h" - -#define GET_INT(x) (((PyIntObject*)x)->ob_ival) -static int -pyg_enum_compare(PyGEnum *self, PyObject *other) +static PyObject * +pyg_enum_richcompare(PyGEnum *self, PyObject *other, int op) { - if (!PyInt_CheckExact(other) && ((PyGEnum*)other)->gtype != self->gtype) { + if (!PyInt_Check(other)) { + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; + } + + if (PyObject_TypeCheck(other, &PyGEnum_Type) && ((PyGEnum*)other)->gtype != self->gtype) { PyErr_Warn(PyExc_Warning, "comparing different enum types"); - return -1; + return NULL; } - - if (GET_INT(self) == GET_INT(other)) - return 0; - else if (GET_INT(self) < GET_INT(other)) - return -1; - else - return 1; + + return pyg_integer_richcompare((PyObject *)self, other, op); } -#undef GET_INT - + static PyObject * pyg_enum_repr(PyGEnum *self) { @@ -293,7 +290,7 @@ PyTypeObject PyGEnum_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ - (cmpfunc)pyg_enum_compare, /* tp_compare */ + 0, /* tp_compare */ (reprfunc)pyg_enum_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -308,7 +305,7 @@ PyTypeObject PyGEnum_Type = { 0, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ - 0, /* tp_richcompare */ + (richcmpfunc)pyg_enum_richcompare, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ diff --git a/gobject/pygflags.c b/gobject/pygflags.c index 51dd59ad..00d9dd38 100644 --- a/gobject/pygflags.c +++ b/gobject/pygflags.c @@ -28,20 +28,21 @@ #include "pygobject-private.h" #define GET_INT_VALUE(x) (((PyIntObject*)x)->ob_ival) -static int -pyg_flags_compare(PyGFlags *self, PyObject *other) + +static PyObject * +pyg_flags_richcompare(PyGFlags *self, PyObject *other, int op) { - if (!PyInt_CheckExact(other) && ((PyGFlags*)other)->gtype != self->gtype) { + if (!PyInt_Check(other)) { + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; + } + + if (PyObject_TypeCheck(other, &PyGFlags_Type) && ((PyGFlags*)other)->gtype != self->gtype) { PyErr_Warn(PyExc_Warning, "comparing different flags types"); - return -1; + return NULL; } - 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; + return pyg_integer_richcompare((PyObject *)self, other, op); } static char * @@ -434,7 +435,7 @@ PyTypeObject PyGFlags_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ - (cmpfunc)pyg_flags_compare, /* tp_compare */ + 0, /* tp_compare */ (reprfunc)pyg_flags_repr, /* tp_repr */ &pyg_flags_as_number, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -449,7 +450,7 @@ PyTypeObject PyGFlags_Type = { 0, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ - 0, /* tp_richcompare */ + (richcmpfunc)pyg_flags_richcompare, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ diff --git a/gobject/pygobject-private.h b/gobject/pygobject-private.h index 17546c21..69a940f3 100644 --- a/gobject/pygobject-private.h +++ b/gobject/pygobject-private.h @@ -64,6 +64,10 @@ int pygobject_construct (PyGObject *self, ...); void pyg_set_object_has_new_constructor (GType gtype); +PyObject *pyg_integer_richcompare(PyObject *v, + PyObject *w, + int op); + /* from pygtype.h */ extern PyTypeObject PyGTypeWrapper_Type; |