summaryrefslogtreecommitdiffstats
path: root/glib
diff options
context:
space:
mode:
authorJohn Ehresman <jpe@wingware.com>2010-04-02 16:08:07 -0400
committerJohn Ehresman <jpe@wingware.com>2010-04-15 12:13:34 -0400
commit13a5da14842caa6a80e6ed7237422b984a152cd8 (patch)
treeaa4139bf93ffd7730850057c9f4de975068294a8 /glib
parent681832c3cd040433a488a400693b68f213bf7078 (diff)
downloadpygobject-13a5da14842caa6a80e6ed7237422b984a152cd8.tar.gz
pygobject-13a5da14842caa6a80e6ed7237422b984a152cd8.tar.xz
pygobject-13a5da14842caa6a80e6ed7237422b984a152cd8.zip
Use richcompare slot rather than old compare slot and Py_TYPE macro in preparation for py3k support
Diffstat (limited to 'glib')
-rw-r--r--glib/pygiochannel.c17
-rw-r--r--glib/pyglib.c92
-rw-r--r--glib/pyglib.h2
-rw-r--r--glib/pygmaincontext.c17
-rw-r--r--glib/pygmainloop.c17
-rw-r--r--glib/pygoptioncontext.c18
-rw-r--r--glib/pygoptiongroup.c21
7 files changed, 149 insertions, 35 deletions
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;