From 13a5da14842caa6a80e6ed7237422b984a152cd8 Mon Sep 17 00:00:00 2001 From: John Ehresman Date: Fri, 2 Apr 2010 16:08:07 -0400 Subject: Use richcompare slot rather than old compare slot and Py_TYPE macro in preparation for py3k support --- gobject/pygboxed.c | 18 ++++++++++++------ gobject/pygobject.c | 32 ++++++++++++++++++++++++++------ gobject/pygparamspec.c | 19 +++++++++++++------ gobject/pygpointer.c | 17 +++++++++++------ gobject/pygtype.c | 24 ++++++++++++++++++++---- 5 files changed, 82 insertions(+), 28 deletions(-) (limited to 'gobject') diff --git a/gobject/pygboxed.c b/gobject/pygboxed.c index 87695eb..e464ded 100644 --- a/gobject/pygboxed.c +++ b/gobject/pygboxed.c @@ -47,14 +47,20 @@ pyg_boxed_dealloc(PyGBoxed *self) Py_TYPE(self)->tp_free((PyObject *)self); } -static int -pyg_boxed_compare(PyGBoxed *self, PyGBoxed *v) +static PyObject* +pyg_boxed_richcompare(PyObject *self, PyObject *other, int op) { - if (self->boxed == v->boxed) return 0; - if (self->boxed > v->boxed) return -1; - return 1; + if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGBoxed_Type) + return _pyglib_generic_ptr_richcompare(((PyGBoxed*)self)->boxed, + ((PyGBoxed*)other)->boxed, + op); + else { + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; + } } + static long pyg_boxed_hash(PyGBoxed *self) { @@ -216,7 +222,7 @@ pygobject_boxed_register_types(PyObject *d) pygboxed_marshal_key = g_quark_from_static_string("PyGBoxed::marshal"); PyGBoxed_Type.tp_dealloc = (destructor)pyg_boxed_dealloc; - PyGBoxed_Type.tp_compare = (cmpfunc)pyg_boxed_compare; + PyGBoxed_Type.tp_richcompare = pyg_boxed_richcompare; PyGBoxed_Type.tp_repr = (reprfunc)pyg_boxed_repr; PyGBoxed_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; PyGBoxed_Type.tp_methods = pygboxed_methods; diff --git a/gobject/pygobject.c b/gobject/pygobject.c index f8d7dd1..e14cf81 100644 --- a/gobject/pygobject.c +++ b/gobject/pygobject.c @@ -783,7 +783,10 @@ static void pygobject_inherit_slots(PyTypeObject *type, PyObject *bases, gboolean check_for_present) { static int slot_offsets[] = { offsetof(PyTypeObject, tp_richcompare), +#if PY_VERSION_HEX < 0x03000000 offsetof(PyTypeObject, tp_compare), +#endif + offsetof(PyTypeObject, tp_richcompare), offsetof(PyTypeObject, tp_hash), offsetof(PyTypeObject, tp_iter), offsetof(PyTypeObject, tp_repr), @@ -1009,12 +1012,29 @@ pygobject_dealloc(PyGObject *self) PyObject_GC_Del(self); } -static int -pygobject_compare(PyGObject *self, PyGObject *v) +static PyObject* +pygobject_richcompare(PyObject *self, PyObject *other, int op) { - if (self->obj == v->obj) return 0; - if (self->obj > v->obj) return -1; - return 1; + int isinst; + + isinst = PyObject_IsInstance(self, (PyObject*)&PyGObject_Type); + if (isinst == -1) + return NULL; + if (!isinst) { + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; + } + isinst = PyObject_IsInstance(other, (PyObject*)&PyGObject_Type); + if (isinst == -1) + return NULL; + if (!isinst) { + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; + } + + return _pyglib_generic_ptr_richcompare(((PyGObject*)self)->obj, + ((PyGObject*)other)->obj, + op); } static long @@ -2240,7 +2260,7 @@ pygobject_object_register_types(PyObject *d) pyobject_copy, pyobject_free); PyGObject_Type.tp_dealloc = (destructor)pygobject_dealloc; - PyGObject_Type.tp_compare = (cmpfunc)pygobject_compare; + PyGObject_Type.tp_richcompare = pygobject_richcompare; PyGObject_Type.tp_repr = (reprfunc)pygobject_repr; PyGObject_Type.tp_hash = (hashfunc)pygobject_hash; PyGObject_Type.tp_setattro = (setattrofunc)pygobject_setattro; diff --git a/gobject/pygparamspec.c b/gobject/pygparamspec.c index be3840b..403ec16 100644 --- a/gobject/pygparamspec.c +++ b/gobject/pygparamspec.c @@ -32,12 +32,17 @@ PYGLIB_DEFINE_TYPE("gobject.GParamSpec", PyGParamSpec_Type, PyGParamSpec); -static int -pyg_param_spec_compare(PyGParamSpec *self, PyGParamSpec *v) +static PyObject* +pyg_param_spec_richcompare(PyObject *self, PyObject *other, int op) { - if (self->pspec == v->pspec) return 0; - if (self->pspec > v->pspec) return -1; - return 1; + if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGParamSpec_Type) + return _pyglib_generic_ptr_richcompare(((PyGParamSpec*)self)->pspec, + ((PyGParamSpec*)other)->pspec, + op); + else { + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; + } } static long @@ -387,9 +392,11 @@ pygobject_paramspec_register_types(PyObject *d) Py_TYPE(&PyGParamSpec_Type) = &PyType_Type; PyGParamSpec_Type.tp_dealloc = (destructor)pyg_param_spec_dealloc; PyGParamSpec_Type.tp_getattr = (getattrfunc)pyg_param_spec_getattr; - PyGParamSpec_Type.tp_compare = (cmpfunc)pyg_param_spec_compare; + PyGParamSpec_Type.tp_richcompare = pyg_param_spec_richcompare; + PyGParamSpec_Type.tp_flags = Py_TPFLAGS_DEFAULT; PyGParamSpec_Type.tp_repr = (reprfunc)pyg_param_spec_repr; PyGParamSpec_Type.tp_hash = (hashfunc)pyg_param_spec_hash; + if (PyType_Ready(&PyGParamSpec_Type)) return; diff --git a/gobject/pygpointer.c b/gobject/pygpointer.c index 5f6417f..ff2590e 100644 --- a/gobject/pygpointer.c +++ b/gobject/pygpointer.c @@ -41,12 +41,17 @@ pyg_pointer_dealloc(PyGPointer *self) Py_TYPE(self)->tp_free((PyObject *)self); } -static int -pyg_pointer_compare(PyGPointer *self, PyGPointer *v) +static PyObject* +pyg_pointer_richcompare(PyObject *self, PyObject *other, int op) { - if (self->pointer == v->pointer) return 0; - if (self->pointer > v->pointer) return -1; - return 1; + if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGPointer_Type) + return _pyglib_generic_ptr_richcompare(((PyGPointer*)self)->pointer, + ((PyGPointer*)other)->pointer, + op); + else { + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; + } } static long @@ -183,7 +188,7 @@ pygobject_pointer_register_types(PyObject *d) pygpointer_class_key = g_quark_from_static_string("PyGPointer::class"); PyGPointer_Type.tp_dealloc = (destructor)pyg_pointer_dealloc; - PyGPointer_Type.tp_compare = (cmpfunc)pyg_pointer_compare; + PyGPointer_Type.tp_richcompare = pyg_pointer_richcompare; PyGPointer_Type.tp_repr = (reprfunc)pyg_pointer_repr; PyGPointer_Type.tp_hash = (hashfunc)pyg_pointer_hash; PyGPointer_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; diff --git a/gobject/pygtype.c b/gobject/pygtype.c index 5550ce8..aff6287 100644 --- a/gobject/pygtype.c +++ b/gobject/pygtype.c @@ -47,6 +47,20 @@ pyg_type_wrapper_compare(PyGTypeWrapper *self, PyGTypeWrapper *v) return 1; } + +static PyObject* +pyg_type_wrapper_richcompare(PyObject *self, PyObject *other, int op) +{ + if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGTypeWrapper_Type) + return _pyglib_generic_long_richcompare(((PyGTypeWrapper*)self)->type, + ((PyGTypeWrapper*)other)->type, + op); + else { + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; + } +} + static long pyg_type_wrapper_hash(PyGTypeWrapper *self) { @@ -1376,9 +1390,11 @@ gclosure_from_pyfunc(PyGObject *object, PyObject *func) { for (l = inst_data->closures; l; l = l->next) { PyGClosure *pyclosure = l->data; - int res; - PyObject_Cmp(pyclosure->callback, func, &res); - if (!res) { + int res = PyObject_RichCompareBool(pyclosure->callback, func, Py_EQ); + if (res == -1) { + PyErr_Clear(); // Is there anything else to do? + } + else if (res) { return (GClosure*)pyclosure; } } @@ -1759,7 +1775,7 @@ void pygobject_type_register_types(PyObject *d) { PyGTypeWrapper_Type.tp_dealloc = (destructor)pyg_type_wrapper_dealloc; - PyGTypeWrapper_Type.tp_compare = (cmpfunc)pyg_type_wrapper_compare; + PyGTypeWrapper_Type.tp_richcompare = pyg_type_wrapper_richcompare; PyGTypeWrapper_Type.tp_repr = (reprfunc)pyg_type_wrapper_repr; PyGTypeWrapper_Type.tp_hash = (hashfunc)pyg_type_wrapper_hash; PyGTypeWrapper_Type.tp_flags = Py_TPFLAGS_DEFAULT; -- cgit