[pygobject/py3k: 1/20] Use richcompare slot rather than old compare slot and Py_TYPE macro in preparation for py3k support



commit 13a5da14842caa6a80e6ed7237422b984a152cd8
Author: John Ehresman <jpe wingware com>
Date:   Fri Apr 2 16:08:07 2010 -0400

    Use richcompare slot rather than old compare slot and Py_TYPE macro in preparation for py3k support

 codegen/codegen.py          |    7 +--
 gio/gappinfo.override       |    4 +-
 gio/gdrive.override         |    4 +-
 gio/gfile.override          |    6 +-
 gio/gfileattribute.override |    5 +-
 gio/gicon.override          |    6 +-
 gio/gmount.override         |    6 +-
 gio/gvolume.override        |    4 +-
 glib/pygiochannel.c         |   17 +++++---
 glib/pyglib.c               |   92 +++++++++++++++++++++++++++++++++++++++++++
 glib/pyglib.h               |    2 +
 glib/pygmaincontext.c       |   17 +++++---
 glib/pygmainloop.c          |   17 +++++---
 glib/pygoptioncontext.c     |   18 +++++---
 glib/pygoptiongroup.c       |   21 +++++-----
 gobject/pygboxed.c          |   18 ++++++---
 gobject/pygobject.c         |   32 ++++++++++++---
 gobject/pygparamspec.c      |   19 ++++++---
 gobject/pygpointer.c        |   17 +++++---
 gobject/pygtype.c           |   24 +++++++++--
 20 files changed, 251 insertions(+), 85 deletions(-)
---
diff --git a/codegen/codegen.py b/codegen/codegen.py
index 73324a5..7a1f7e6 100755
--- a/codegen/codegen.py
+++ b/codegen/codegen.py
@@ -76,7 +76,7 @@ class FileOutput:
     # handle writing to the file, and keep track of the line number ...
     def write(self, str):
         self.fp.write(str)
-        self.lineno = self.lineno + string.count(str, '\n')
+        self.lineno = self.lineno + str.count('\n')
     def writelines(self, sequence):
         for line in sequence:
             self.write(line)
@@ -96,8 +96,7 @@ class FileOutput:
 class Wrapper:
     type_tmpl = (
         'PyTypeObject G_GNUC_INTERNAL Py%(typename)s_Type = {\n'
-        '    PyObject_HEAD_INIT(NULL)\n'
-        '    0,                                 /* ob_size */\n'
+        '    PyVarObject_HEAD_INIT(NULL, 0)'
         '    "%(classname)s",                   /* tp_name */\n'
         '    sizeof(%(tp_basicsize)s),          /* tp_basicsize */\n'
         '    0,                                 /* tp_itemsize */\n'
@@ -106,7 +105,7 @@ class Wrapper:
         '    (printfunc)0,                      /* tp_print */\n'
         '    (getattrfunc)%(tp_getattr)s,       /* tp_getattr */\n'
         '    (setattrfunc)%(tp_setattr)s,       /* tp_setattr */\n'
-        '    (cmpfunc)%(tp_compare)s,           /* tp_compare */\n'
+        '    NULL, //%(tp_compare)s,                    /* tp_compare */\n'
         '    (reprfunc)%(tp_repr)s,             /* tp_repr */\n'
         '    (PyNumberMethods*)%(tp_as_number)s,     /* tp_as_number */\n'
         '    (PySequenceMethods*)%(tp_as_sequence)s, /* tp_as_sequence */\n'
diff --git a/gio/gappinfo.override b/gio/gappinfo.override
index 414769b..ddfd28d 100644
--- a/gio/gappinfo.override
+++ b/gio/gappinfo.override
@@ -31,7 +31,7 @@ _wrap__install_app_info_meta(PyObject *self, PyObject *args)
 	return NULL;
 
     Py_INCREF(metaclass);
-    PyGAppInfo_Type.ob_type = (PyTypeObject*)metaclass;
+    Py_TYPE(&PyGAppInfo_Type) = (PyTypeObject*)metaclass;
 
     Py_INCREF(Py_None);
     return Py_None;
@@ -199,7 +199,7 @@ _wrap_g_app_info_tp_repr(PyGObject *self)
     PyObject *result;
 
     representation = g_strdup_printf("<%s at %p: %s>",
-				     self->ob_type->tp_name, self,
+				     Py_TYPE(self)->tp_name, self,
 				     name ? name : "UNKNOWN NAME");
 
     result = PyString_FromString(representation);
diff --git a/gio/gdrive.override b/gio/gdrive.override
index 7961856..8362ae0 100644
--- a/gio/gdrive.override
+++ b/gio/gdrive.override
@@ -154,11 +154,11 @@ _wrap_g_drive_tp_repr(PyGObject *self)
     PyObject *result;
 
     if (name) {
-	representation = g_strdup_printf("<%s at %p: %s>", self->ob_type->tp_name, self, name);
+	representation = g_strdup_printf("<%s at %p: %s>", Py_TYPE(self)->tp_name, self, name);
 	g_free(name);
     }
     else
-	representation = g_strdup_printf("<%s at %p: UNKNOWN NAME>", self->ob_type->tp_name, self);
+	representation = g_strdup_printf("<%s at %p: UNKNOWN NAME>", Py_TYPE(self)->tp_name, self);
 
     result = PyString_FromString(representation);
     g_free(representation);
diff --git a/gio/gfile.override b/gio/gfile.override
index f793ef8..3733192 100644
--- a/gio/gfile.override
+++ b/gio/gfile.override
@@ -62,7 +62,7 @@ _wrap__install_file_meta(PyObject *self, PyObject *args)
 	return NULL;
 
     Py_INCREF(metaclass);
-    PyGFile_Type.ob_type = (PyTypeObject*)metaclass;
+    Py_TYPE(&PyGFile_Type) = (PyTypeObject*)metaclass;
 
     Py_INCREF(Py_None);
     return Py_None;
@@ -1498,11 +1498,11 @@ _wrap_g_file_tp_repr(PyGObject *self)
     PyObject *result;
 
     if (uri) {
-	representation = g_strdup_printf("<%s at %p: %s>", self->ob_type->tp_name, self, uri);
+	representation = g_strdup_printf("<%s at %p: %s>", Py_TYPE(self)->tp_name, self, uri);
 	g_free(uri);
     }
     else
-	representation = g_strdup_printf("<%s at %p: UNKNOWN URI>", self->ob_type->tp_name, self);
+	representation = g_strdup_printf("<%s at %p: UNKNOWN URI>", Py_TYPE(self)->tp_name, self);
 
     result = PyString_FromString(representation);
     g_free(representation);
diff --git a/gio/gfileattribute.override b/gio/gfileattribute.override
index e51ec63..5f3e552 100644
--- a/gio/gfileattribute.override
+++ b/gio/gfileattribute.override
@@ -83,8 +83,7 @@ static const PyGetSetDef pyg_file_attribute_info_getsets[] = {
 };
 
 PyTypeObject PyGFileAttributeInfo_Type = {
-    PyObject_HEAD_INIT(NULL)
-    0,                                  /* ob_size */
+    PyVarObject_HEAD_INIT(NULL, 0)
     "gio.FileAttributeInfo",            /* tp_name */
     sizeof(PyGFileAttributeInfo),      /* tp_basicsize */
     0,                                  /* tp_itemsize */
@@ -93,7 +92,7 @@ PyTypeObject PyGFileAttributeInfo_Type = {
     (printfunc)0,                       /* tp_print */
     (getattrfunc)0,                     /* tp_getattr */
     (setattrfunc)0,                     /* tp_setattr */
-    (cmpfunc)0,                         /* tp_compare */
+    NULL,                               /* tp_compare */
     (reprfunc)0,                        /* tp_repr */
     0,                                  /* tp_as_number */
     0,                                  /* tp_as_sequence */
diff --git a/gio/gicon.override b/gio/gicon.override
index c429556..9d27495 100644
--- a/gio/gicon.override
+++ b/gio/gicon.override
@@ -177,11 +177,11 @@ _wrap_g_file_icon_tp_repr(PyGObject *self)
     PyObject *result;
 
     if (uri) {
-	representation = g_strdup_printf("<%s at %p: %s>", self->ob_type->tp_name, self, uri);
+	representation = g_strdup_printf("<%s at %p: %s>", Py_TYPE(self)->tp_name, self, uri);
 	g_free(uri);
     }
     else
-	representation = g_strdup_printf("<%s at %p: UNKNOWN URI>", self->ob_type->tp_name, self);
+	representation = g_strdup_printf("<%s at %p: UNKNOWN URI>", Py_TYPE(self)->tp_name, self);
 
     result = PyString_FromString(representation);
     g_free(representation);
@@ -275,7 +275,7 @@ _wrap_g_themed_icon_tp_repr(PyGObject *self)
     GString *representation = g_string_new(NULL);
     PyObject *result;
 
-    g_string_append_printf(representation, "<%s at %p: ", self->ob_type->tp_name, self);
+    g_string_append_printf(representation, "<%s at %p: ", Py_TYPE(self)->tp_name, self);
 
     if (names) {
 	gboolean first_name = TRUE;
diff --git a/gio/gmount.override b/gio/gmount.override
index 9fb0828..54d5dd2 100644
--- a/gio/gmount.override
+++ b/gio/gmount.override
@@ -327,15 +327,15 @@ _wrap_g_mount_tp_repr(PyGObject *self)
     if (name) {
 	if (uuid) {
 	    representation = g_strdup_printf("<%s at %p: %s (%s)>",
-					     self->ob_type->tp_name, self, name, uuid);
+					     Py_TYPE(self)->tp_name, self, name, uuid);
 	}
 	else {
 	    representation = g_strdup_printf("<%s at %p: %s>",
-					     self->ob_type->tp_name, self, name);
+					     Py_TYPE(self)->tp_name, self, name);
 	}
     }
     else
-	representation = g_strdup_printf("<%s at %p: UNKNOWN NAME>", self->ob_type->tp_name, self);
+	representation = g_strdup_printf("<%s at %p: UNKNOWN NAME>", Py_TYPE(self)->tp_name, self);
 
     g_free(name);
     g_free(uuid);
diff --git a/gio/gvolume.override b/gio/gvolume.override
index 89c27e4..83b855d 100644
--- a/gio/gvolume.override
+++ b/gio/gvolume.override
@@ -148,11 +148,11 @@ _wrap_g_volume_tp_repr(PyGObject *self)
     PyObject *result;
 
     if (name) {
-	representation = g_strdup_printf("<%s at %p: %s>", self->ob_type->tp_name, self, name);
+	representation = g_strdup_printf("<%s at %p: %s>", Py_TYPE(self)->tp_name, self, name);
 	g_free(name);
     }
     else
-	representation = g_strdup_printf("<%s at %p: UNKNOWN NAME>", self->ob_type->tp_name, self);
+	representation = g_strdup_printf("<%s at %p: UNKNOWN NAME>", Py_TYPE(self)->tp_name, self);
 
     result = PyString_FromString(representation);
     g_free(representation);
diff --git a/glib/pygiochannel.c b/glib/pygiochannel.c
index 5b57011..fbaa9bd 100644
--- a/glib/pygiochannel.c
+++ b/glib/pygiochannel.c
@@ -43,12 +43,17 @@ py_io_channel_next(PyGIOChannel *self)
     return ret_obj;
 }
 
-static int
-py_io_channel_compare(PyGIOChannel *self, PyGIOChannel *v)
+static PyObject*
+py_io_channel_richcompare(PyObject *self, PyObject *other, int op)
 {
-    if (self->channel == v->channel) return 0;
-    if (self->channel > v->channel) return -1;
-    return 1;
+    if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGIOChannel_Type)
+        return _pyglib_generic_ptr_richcompare(((PyGIOChannel*)self)->channel,
+                                               ((PyGIOChannel*)other)->channel,
+                                               op);
+    else {
+       Py_INCREF(Py_NotImplemented);
+       return Py_NotImplemented;
+    }
 }
 
 static PyObject*
@@ -731,7 +736,7 @@ pyglib_iochannel_register_types(PyObject *d)
     PyGIOChannel_Type.tp_members = py_io_channel_members;
     PyGIOChannel_Type.tp_methods = py_io_channel_methods;
     PyGIOChannel_Type.tp_hash = (hashfunc)py_io_channel_hash;
-    PyGIOChannel_Type.tp_compare = (cmpfunc)py_io_channel_compare;
+    PyGIOChannel_Type.tp_richcompare = py_io_channel_richcompare;
     PyGIOChannel_Type.tp_iter = (getiterfunc)py_io_channel_get_iter;
     PyGIOChannel_Type.tp_iternext = (iternextfunc)py_io_channel_next;
 
diff --git a/glib/pyglib.c b/glib/pyglib.c
index a1bdbb9..118497e 100644
--- a/glib/pyglib.c
+++ b/glib/pyglib.c
@@ -574,4 +574,96 @@ _pyglib_handler_marshal(gpointer user_data)
     return res;
 }
 
+PyObject* 
+_pyglib_generic_ptr_richcompare(void* a, void *b, int op)
+{
+    PyObject *res;	
+	
+    switch (op) {
+
+      case Py_EQ:
+	res = (a == b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+
+      case Py_NE:
+	res = (a != b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+
+      case Py_LT:
+	res = (a < b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+	  
+      case Py_LE:
+	res = (a <= b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+
+      case Py_GT:
+	res = (a > b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+	  
+      case Py_GE:
+	res = (a >= b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+	  
+      default:
+	res = Py_NotImplemented;
+	Py_INCREF(res);
+	break;
+    }
+
+    return res;
+}
+
+PyObject* 
+_pyglib_generic_long_richcompare(long a, long b, int op)
+{
+    PyObject *res;	
+    
+    switch (op) {
+
+      case Py_EQ:
+	res = (a == b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+
+      case Py_NE:
+	res = (a != b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+
+
+      case Py_LT:
+	res = (a < b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+	  
+      case Py_LE:
+	res = (a <= b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+
+      case Py_GT:
+	res = (a > b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+	  
+      case Py_GE:
+	res = (a >= b) ? Py_True : Py_False;
+	Py_INCREF(res);
+	break;
+	  
+      default:
+	res = Py_NotImplemented;
+	Py_INCREF(res);
+	break;
+    }
+
+    return res;
+}
 
diff --git a/glib/pyglib.h b/glib/pyglib.h
index 84bb36c..24e1ed2 100644
--- a/glib/pyglib.h
+++ b/glib/pyglib.h
@@ -53,6 +53,8 @@ PyObject * pyglib_float_from_timeval(GTimeVal timeval);
 
 /* Private: for gobject <-> glib interaction only. */
 void _pyglib_notify_on_enabling_threads(PyGLibThreadsEnabledFunc callback);
+PyObject* _pyglib_generic_ptr_richcompare(void* a, void *b, int op);
+PyObject* _pyglib_generic_long_richcompare(long a, long b, int op);
 
 #define pyglib_begin_allow_threads		\
     G_STMT_START {                              \
diff --git a/glib/pygmaincontext.c b/glib/pygmaincontext.c
index 186215a..2b02c6c 100644
--- a/glib/pygmaincontext.c
+++ b/glib/pygmaincontext.c
@@ -51,12 +51,17 @@ pyg_main_context_dealloc(PyGMainContext *self)
     PyObject_Del(self);
 }
 
-static int
-pyg_main_context_compare(PyGMainContext *self, PyGMainContext *v)
+static PyObject*
+pyg_main_context_richcompare(PyObject *self, PyObject *other, int op)
 {
-    if (self->context == v->context) return 0;
-    if (self->context > v->context) return -1;
-    return 1;
+    if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGMainContext_Type)
+	return _pyglib_generic_ptr_richcompare(((PyGMainContext*)self)->context,
+					       ((PyGMainContext*)other)->context,
+					       op);
+    else {
+	Py_INCREF(Py_NotImplemented);
+	return Py_NotImplemented;
+    }
 }
 
 static PyObject *
@@ -91,7 +96,7 @@ void
 pyglib_maincontext_register_types(PyObject *d)
 {
     PyGMainContext_Type.tp_dealloc = (destructor)pyg_main_context_dealloc;
-    PyGMainContext_Type.tp_compare = (cmpfunc)pyg_main_context_compare;
+    PyGMainContext_Type.tp_richcompare = pyg_main_context_richcompare;
     PyGMainContext_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
     PyGMainContext_Type.tp_methods = _PyGMainContext_methods;
     PyGMainContext_Type.tp_init = (initproc)pyg_main_context_init;
diff --git a/glib/pygmainloop.c b/glib/pygmainloop.c
index d9f048c..de74971 100644
--- a/glib/pygmainloop.c
+++ b/glib/pygmainloop.c
@@ -285,12 +285,17 @@ pyg_main_loop_dealloc(PyGMainLoop *self)
     PyObject_Del(self);
 }
 
-static int
-pyg_main_loop_compare(PyGMainLoop *self, PyGMainLoop *v)
+static PyObject*
+pyg_main_loop_richcompare(PyObject *self, PyObject *other, int op)
 {
-    if (self->loop == v->loop) return 0;
-    if (self->loop > v->loop) return -1;
-    return 1;
+    if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGMainLoop_Type)
+        return _pyglib_generic_ptr_richcompare(((PyGMainLoop*)self)->loop,
+                                               ((PyGMainLoop*)other)->loop,
+                                               op);
+    else {
+       Py_INCREF(Py_NotImplemented);
+       return Py_NotImplemented;
+    }
 }
 
 static PyObject *
@@ -346,7 +351,7 @@ void
 pyglib_mainloop_register_types(PyObject *d)
 {
     PyGMainLoop_Type.tp_dealloc = (destructor)pyg_main_loop_dealloc;
-    PyGMainLoop_Type.tp_compare = (cmpfunc)pyg_main_loop_compare;
+    PyGMainLoop_Type.tp_richcompare = pyg_main_loop_richcompare;
     PyGMainLoop_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
     PyGMainLoop_Type.tp_methods = _PyGMainLoop_methods;
     PyGMainLoop_Type.tp_init = (initproc)pyg_main_loop_init;
diff --git a/glib/pygoptioncontext.c b/glib/pygoptioncontext.c
index 92ba901..b93026b 100644
--- a/glib/pygoptioncontext.c
+++ b/glib/pygoptioncontext.c
@@ -272,13 +272,17 @@ pyg_option_context_add_group(PyGOptionContext *self,
     return Py_None;
 }
 
-static int
-pyg_option_context_compare(PyGOptionContext *self, PyGOptionContext *context)
+static PyObject*
+pyg_option_context_richcompare(PyObject *self, PyObject *other, int op)
 {
-    if (self->context == context->context) return 0;
-    if (self->context > context->context)
-        return 1;
-    return -1;
+    if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGOptionContext_Type)
+        return _pyglib_generic_ptr_richcompare(((PyGOptionContext*)self)->context,
+                                               ((PyGOptionContext*)other)->context,
+                                               op);
+    else {
+       Py_INCREF(Py_NotImplemented);
+       return Py_NotImplemented;
+    }
 }
 
 static PyObject *
@@ -304,7 +308,7 @@ void
 pyglib_option_context_register_types(PyObject *d)
 {
     PyGOptionContext_Type.tp_dealloc = (destructor)pyg_option_context_dealloc;
-    PyGOptionContext_Type.tp_compare = (cmpfunc)pyg_option_context_compare;
+    PyGOptionContext_Type.tp_richcompare = pyg_option_context_richcompare;
     PyGOptionContext_Type.tp_flags = Py_TPFLAGS_DEFAULT;
     PyGOptionContext_Type.tp_methods = pyg_option_context_methods;
     PyGOptionContext_Type.tp_init = (initproc)pyg_option_context_init;
diff --git a/glib/pygoptiongroup.c b/glib/pygoptiongroup.c
index 70e4529..ea3830b 100644
--- a/glib/pygoptiongroup.c
+++ b/glib/pygoptiongroup.c
@@ -240,16 +240,17 @@ pyg_option_group_set_translation_domain(PyGOptionGroup *self,
     return Py_None;
 }
 
-static int
-pyg_option_group_compare(PyGOptionGroup *self, PyGOptionGroup *group)
+static PyObject*
+pyg_option_group_richcompare(PyObject *self, PyObject *other, int op)
 {
-    if (self->group == group->group)
-	return 0;
-
-    if (self->group > group->group)
-        return 1;
-
-    return -1;
+    if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGOptionGroup_Type)
+	return _pyglib_generic_ptr_richcompare(((PyGOptionGroup*)self)->group,
+					       ((PyGOptionGroup*)other)->group,
+					       op);
+    else {
+	Py_INCREF(Py_NotImplemented);
+	return Py_NotImplemented;
+    }
 }
 
 static PyMethodDef pyg_option_group_methods[] = {
@@ -262,7 +263,7 @@ void
 pyglib_option_group_register_types(PyObject *d)
 {
     PyGOptionGroup_Type.tp_dealloc = (destructor)pyg_option_group_dealloc;
-    PyGOptionGroup_Type.tp_compare = (cmpfunc)pyg_option_group_compare;
+    PyGOptionGroup_Type.tp_richcompare = pyg_option_group_richcompare;
     PyGOptionGroup_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
     PyGOptionGroup_Type.tp_methods = pyg_option_group_methods;
     PyGOptionGroup_Type.tp_init = (initproc)pyg_option_group_init;
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;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]