summaryrefslogtreecommitdiffstats
path: root/Objects/descrobject.c
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2009-02-01 10:28:51 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2009-02-01 10:28:51 (GMT)
commit211c6258294bf683935bff73a61ce3dd84070988 (patch)
tree8dc2a5a5fdcd1f7bc0e176bce006b12e1b588a05 /Objects/descrobject.c
parent776e7014e97a79cc2bd5fffd15908e83ff0273cf (diff)
downloadcpython-211c6258294bf683935bff73a61ce3dd84070988.zip
cpython-211c6258294bf683935bff73a61ce3dd84070988.tar.gz
cpython-211c6258294bf683935bff73a61ce3dd84070988.tar.bz2
Issue #1717, stage 2: remove uses of tp_compare in Modules and most
Objects.
Diffstat (limited to 'Objects/descrobject.c')
-rw-r--r--Objects/descrobject.c76
1 files changed, 61 insertions, 15 deletions
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index 9f4c2a6..a623925 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -774,12 +774,6 @@ proxy_traverse(PyObject *self, visitproc visit, void *arg)
return 0;
}
-static int
-proxy_compare(proxyobject *v, PyObject *w)
-{
- return PyObject_Compare(v->dict, w);
-}
-
static PyObject *
proxy_richcompare(proxyobject *v, PyObject *w, int op)
{
@@ -796,7 +790,7 @@ PyTypeObject PyDictProxy_Type = {
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
- (cmpfunc)proxy_compare, /* tp_compare */
+ 0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
&proxy_as_sequence, /* tp_as_sequence */
@@ -844,12 +838,17 @@ PyDictProxy_New(PyObject *dict)
/* This has no reason to be in this file except that adding new files is a
bit of a pain */
+/* forward */
+static PyTypeObject wrappertype;
+
typedef struct {
PyObject_HEAD
PyWrapperDescrObject *descr;
PyObject *self;
} wrapperobject;
+#define Wrapper_Check(v) (Py_TYPE(v) == &wrappertype)
+
static void
wrapper_dealloc(wrapperobject *wp)
{
@@ -861,13 +860,60 @@ wrapper_dealloc(wrapperobject *wp)
Py_TRASHCAN_SAFE_END(wp)
}
-static int
-wrapper_compare(wrapperobject *a, wrapperobject *b)
+#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
+
+static PyObject *
+wrapper_richcompare(PyObject *a, PyObject *b, int op)
{
- if (a->descr == b->descr)
- return PyObject_Compare(a->self, b->self);
- else
- return (a->descr < b->descr) ? -1 : 1;
+ int result;
+ PyObject *v;
+ PyWrapperDescrObject *a_descr, *b_descr;
+
+ assert(a != NULL && b != NULL);
+
+ /* both arguments should be wrapperobjects */
+ if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
+ v = Py_NotImplemented;
+ Py_INCREF(v);
+ return v;
+ }
+
+ /* compare by descriptor address; if the descriptors are the same,
+ compare by the objects they're bound to */
+ a_descr = ((wrapperobject *)a)->descr;
+ b_descr = ((wrapperobject *)b)->descr;
+ if (a_descr == b_descr) {
+ a = ((wrapperobject *)a)->self;
+ b = ((wrapperobject *)b)->self;
+ return PyObject_RichCompare(a, b, op);
+ }
+
+ result = a_descr - b_descr;
+ switch (op) {
+ case Py_EQ:
+ v = TEST_COND(result == 0);
+ break;
+ case Py_NE:
+ v = TEST_COND(result != 0);
+ break;
+ case Py_LE:
+ v = TEST_COND(result <= 0);
+ break;
+ case Py_GE:
+ v = TEST_COND(result >= 0);
+ break;
+ case Py_LT:
+ v = TEST_COND(result < 0);
+ break;
+ case Py_GT:
+ v = TEST_COND(result > 0);
+ break;
+ default:
+ PyErr_BadArgument();
+ return NULL;
+ }
+ Py_INCREF(v);
+ return v;
}
static long
@@ -977,7 +1023,7 @@ static PyTypeObject wrappertype = {
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
- (cmpfunc)wrapper_compare, /* tp_compare */
+ 0, /* tp_compare */
(reprfunc)wrapper_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
@@ -992,7 +1038,7 @@ static PyTypeObject wrappertype = {
0, /* tp_doc */
wrapper_traverse, /* tp_traverse */
0, /* tp_clear */
- 0, /* tp_richcompare */
+ wrapper_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */