summaryrefslogtreecommitdiffstats
path: root/Objects/cellobject.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/cellobject.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/cellobject.c')
-rw-r--r--Objects/cellobject.c62
1 files changed, 51 insertions, 11 deletions
diff --git a/Objects/cellobject.c b/Objects/cellobject.c
index 50a3897..c2194b7 100644
--- a/Objects/cellobject.c
+++ b/Objects/cellobject.c
@@ -51,16 +51,56 @@ cell_dealloc(PyCellObject *op)
PyObject_GC_Del(op);
}
-static int
-cell_compare(PyCellObject *a, PyCellObject *b)
+#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
+
+static PyObject *
+cell_richcompare(PyObject *a, PyObject *b, int op)
{
- if (a->ob_ref == NULL) {
- if (b->ob_ref == NULL)
- return 0;
- return -1;
- } else if (b->ob_ref == NULL)
- return 1;
- return PyObject_Compare(a->ob_ref, b->ob_ref);
+ int result;
+ PyObject *v;
+
+ /* neither argument should be NULL, unless something's gone wrong */
+ assert(a != NULL && b != NULL);
+
+ /* both arguments should be instances of PyCellObject */
+ if (!PyCell_Check(a) || !PyCell_Check(b)) {
+ v = Py_NotImplemented;
+ Py_INCREF(v);
+ return v;
+ }
+
+ /* compare cells by contents; empty cells come before anything else */
+ a = ((PyCellObject *)a)->ob_ref;
+ b = ((PyCellObject *)b)->ob_ref;
+ if (a != NULL && b != NULL)
+ return PyObject_RichCompare(a, b, op);
+
+ result = (b == NULL) - (a == NULL);
+ 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 PyObject *
@@ -114,7 +154,7 @@ PyTypeObject PyCell_Type = {
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
- (cmpfunc)cell_compare, /* tp_compare */
+ 0, /* tp_compare */
(reprfunc)cell_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
@@ -129,7 +169,7 @@ PyTypeObject PyCell_Type = {
0, /* tp_doc */
(traverseproc)cell_traverse, /* tp_traverse */
(inquiry)cell_clear, /* tp_clear */
- 0, /* tp_richcompare */
+ cell_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */