Permalink
Browse files

3K: core: handle removed tp_compare and PyObject_Compare

Also, implement tp_richcompare for flagsobject.
  • Loading branch information...
1 parent a8fa66a commit 2649c52a599840a7d811e4c171b03877cbf68043 @pv pv committed Dec 6, 2009
@@ -2392,7 +2392,17 @@ OBJECT_compare(PyObject **ip1, PyObject **ip2, PyArrayObject *NPY_UNUSED(ap))
}
return 1;
}
+#if defined(NPY_PY3K)
+ if (PyObject_RichCompareBool(*ip1, *ip2, Py_LT) == 1) {
+ return -1;
+ } else if (PyObject_RichCompareBool(*ip1, *ip2, Py_GT) == 1) {
+ return 1;
+ } else {
+ return 0;
+ }
+#else
return PyObject_Compare(*ip1, *ip2);
+#endif
}
@@ -2553,7 +2563,11 @@ OBJECT_argmax(PyObject **ip, intp n, intp *max_ind, PyArrayObject *NPY_UNUSED(ai
}
for(; i<n; i++) {
ip++;
- if (*ip != NULL && PyObject_Compare(*ip,mp) > 0) {
+#if defined(NPY_PY3K)
+ if (*ip != NULL && PyObject_RichCompareBool(*ip, mp, Py_GT) == 1) {
+#else
+ if (*ip != NULL && PyObject_Compare(*ip, mp) > 0) {
+#endif
mp = *ip;
*max_ind=i;
}
@@ -528,6 +528,35 @@ arrayflags_compare(PyArrayFlagsObject *self, PyArrayFlagsObject *other)
}
}
+
+static PyObject*
+arrayflags_richcompare(PyObject *self, PyObject *other, int cmp_op)
+{
+ PyObject *result = Py_NotImplemented;
+ int cmp;
+
+ if (cmp_op != Py_EQ && cmp_op != Py_NE) {
+ PyErr_SetString(PyExc_TypeError,
+ "undefined comparison for flag object");
+ return NULL;
+ }
+
+ if (PyObject_TypeCheck(other, &PyArrayFlags_Type)) {
+ cmp = arrayflags_compare((PyArrayFlagsObject *)self,
+ (PyArrayFlagsObject *)other);
+ }
+
+ if (cmp_op == Py_EQ) {
+ result = (cmp == 0) ? Py_True : Py_False;
+ }
+ else if (cmp_op == Py_NE) {
+ result = (cmp != 0) ? Py_True : Py_False;
+ }
+
+ Py_INCREF(result);
+ return result;
+}
+
static PyMappingMethods arrayflags_as_mapping = {
#if PY_VERSION_HEX >= 0x02050000
(lenfunc)NULL, /*mp_length*/
@@ -588,7 +617,7 @@ NPY_NO_EXPORT PyTypeObject PyArrayFlags_Type = {
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
- 0, /* tp_richcompare */
+ arrayflags_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
@@ -1298,8 +1298,13 @@ _equivalent_fields(PyObject *field1, PyObject *field2) {
if (field1 == NULL || field2 == NULL) {
return 0;
}
+#if defined(NPY_PY3K)
+ val = PyObject_RichCompareBool(field1, field2, Py_EQ);
+ if (val != 1 || PyErr_Occurred()) {
+#else
val = PyObject_Compare(field1, field2);
if (val != 0 || PyErr_Occurred()) {
+#endif
same = 0;
}
else {
@@ -2810,15 +2815,22 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict))
} \
Py##child##ArrType_Type.tp_hash = Py##parent1##_Type.tp_hash;
+#if defined(NPY_PY3K)
+#define DUAL_INHERIT_COMPARE(child, parent1, parent2)
+#else
+#define DUAL_INHERIT_COMPARE(child, parent1, parent2) \
+ Py##child##ArrType_Type.tp_compare = \
+ Py##parent1##_Type.tp_compare;
+#endif
+
#define DUAL_INHERIT2(child, parent1, parent2) \
Py##child##ArrType_Type.tp_base = &Py##parent1##_Type; \
Py##child##ArrType_Type.tp_bases = \
Py_BuildValue("(OO)", &Py##parent1##_Type, \
&Py##parent2##ArrType_Type); \
Py##child##ArrType_Type.tp_richcompare = \
Py##parent1##_Type.tp_richcompare; \
- Py##child##ArrType_Type.tp_compare = \
- Py##parent1##_Type.tp_compare; \
+ DUAL_INHERIT_COMPARE(child, parent1, parent2) \
Py##child##ArrType_Type.tp_hash = Py##parent1##_Type.tp_hash; \
if (PyType_Ready(&Py##child##ArrType_Type) < 0) { \
PyErr_Print(); \
@@ -60,4 +60,42 @@ static NPY_INLINE int PyInt_Check(PyObject *op) {
#define Py_SIZE(o) (((PyVarObject*)(o))->ob_size)
#endif
+/*
+ * PyObject_Cmp
+ */
+#if defined(NPY_PY3K)
+static NPY_INLINE int
+PyObject_Cmp(PyObject *i1, PyObject *i2, int *cmp)
+{
+ int v;
+ v = PyObject_RichCompareBool(i1, i2, Py_LT);
+ if (v == 0) {
+ *cmp = -1;
+ return 1;
+ }
+ else if (v == -1) {
+ return -1;
+ }
+
+ v = PyObject_RichCompareBool(i1, i2, Py_GT);
+ if (v == 0) {
+ *cmp = 1;
+ return 1;
+ }
+ else if (v == -1) {
+ return -1;
+ }
+
+ v = PyObject_RichCompareBool(i1, i2, Py_EQ);
+ if (v == 0) {
+ *cmp = 0;
+ return 1;
+ }
+ else {
+ *cmp = 0;
+ return -1;
+ }
+}
+#endif
+
#endif /* _NPY_3KCOMPAT_H_ */
@@ -1195,13 +1195,19 @@ add_scalarmath(void)
/**end repeat**/
saved_tables_arrtype[0] = PyLongArrType_Type.tp_as_number;
+#if !defined(NPY_PY3K)
saved_tables_arrtype[1] = PyLongArrType_Type.tp_compare;
+#endif
saved_tables_arrtype[2] = PyLongArrType_Type.tp_richcompare;
saved_tables_arrtype[3] = PyDoubleArrType_Type.tp_as_number;
+#if !defined(NPY_PY3K)
saved_tables_arrtype[4] = PyDoubleArrType_Type.tp_compare;
+#endif
saved_tables_arrtype[5] = PyDoubleArrType_Type.tp_richcompare;
saved_tables_arrtype[6] = PyCDoubleArrType_Type.tp_as_number;
+#if !defined(NPY_PY3K)
saved_tables_arrtype[7] = PyCDoubleArrType_Type.tp_compare;
+#endif
saved_tables_arrtype[8] = PyCDoubleArrType_Type.tp_richcompare;
}
@@ -1293,19 +1299,28 @@ alter_pyscalars(PyObject *NPY_UNUSED(dummy), PyObject *args)
n = PyTuple_GET_SIZE(args);
while(n--) {
obj = PyTuple_GET_ITEM(args, n);
+#if !defined(NPY_PY3K)
if (obj == (PyObject *)(&PyInt_Type)) {
PyInt_Type.tp_as_number = PyLongArrType_Type.tp_as_number;
PyInt_Type.tp_compare = PyLongArrType_Type.tp_compare;
PyInt_Type.tp_richcompare = PyLongArrType_Type.tp_richcompare;
}
- else if (obj == (PyObject *)(&PyFloat_Type)) {
+ else
+#else
+#warning XXX -- is it important to do something with the integers here?
+#endif
+ if (obj == (PyObject *)(&PyFloat_Type)) {
PyFloat_Type.tp_as_number = PyDoubleArrType_Type.tp_as_number;
+#if !defined(NPY_PY3K)
PyFloat_Type.tp_compare = PyDoubleArrType_Type.tp_compare;
+#endif
PyFloat_Type.tp_richcompare = PyDoubleArrType_Type.tp_richcompare;
}
else if (obj == (PyObject *)(&PyComplex_Type)) {
PyComplex_Type.tp_as_number = PyCDoubleArrType_Type.tp_as_number;
+#if !defined(NPY_PY3K)
PyComplex_Type.tp_compare = PyCDoubleArrType_Type.tp_compare;
+#endif
PyComplex_Type.tp_richcompare = \
PyCDoubleArrType_Type.tp_richcompare;
}
@@ -1328,19 +1343,28 @@ restore_pyscalars(PyObject *NPY_UNUSED(dummy), PyObject *args)
n = PyTuple_GET_SIZE(args);
while(n--) {
obj = PyTuple_GET_ITEM(args, n);
+#if !defined(NPY_PY3K)
if (obj == (PyObject *)(&PyInt_Type)) {
PyInt_Type.tp_as_number = saved_tables[0];
PyInt_Type.tp_compare = saved_tables[1];
PyInt_Type.tp_richcompare = saved_tables[2];
}
- else if (obj == (PyObject *)(&PyFloat_Type)) {
+ else
+#else
+#warning XXX -- is it important to do something with the integers here?
+#endif
+ if (obj == (PyObject *)(&PyFloat_Type)) {
PyFloat_Type.tp_as_number = saved_tables[3];
+#if !defined(NPY_PY3K)
PyFloat_Type.tp_compare = saved_tables[4];
+#endif
PyFloat_Type.tp_richcompare = saved_tables[5];
}
else if (obj == (PyObject *)(&PyComplex_Type)) {
PyComplex_Type.tp_as_number = saved_tables[6];
+#if !defined(NPY_PY3K)
PyComplex_Type.tp_compare = saved_tables[7];
+#endif
PyComplex_Type.tp_richcompare = saved_tables[8];
}
else {
@@ -1362,19 +1386,28 @@ use_pythonmath(PyObject *NPY_UNUSED(dummy), PyObject *args)
n = PyTuple_GET_SIZE(args);
while(n--) {
obj = PyTuple_GET_ITEM(args, n);
+#if !defined(NPY_PY3K)
if (obj == (PyObject *)(&PyInt_Type)) {
PyLongArrType_Type.tp_as_number = saved_tables[0];
PyLongArrType_Type.tp_compare = saved_tables[1];
PyLongArrType_Type.tp_richcompare = saved_tables[2];
}
- else if (obj == (PyObject *)(&PyFloat_Type)) {
+ else
+#else
+#warning XXX -- is it important to do something with the integers here?
+#endif
+ if (obj == (PyObject *)(&PyFloat_Type)) {
PyDoubleArrType_Type.tp_as_number = saved_tables[3];
+#if !defined(NPY_PY3K)
PyDoubleArrType_Type.tp_compare = saved_tables[4];
+#endif
PyDoubleArrType_Type.tp_richcompare = saved_tables[5];
}
else if (obj == (PyObject *)(&PyComplex_Type)) {
PyCDoubleArrType_Type.tp_as_number = saved_tables[6];
+#if !defined(NPY_PY3K)
PyCDoubleArrType_Type.tp_compare = saved_tables[7];
+#endif
PyCDoubleArrType_Type.tp_richcompare = saved_tables[8];
}
else {
@@ -1396,19 +1429,28 @@ use_scalarmath(PyObject *NPY_UNUSED(dummy), PyObject *args)
n = PyTuple_GET_SIZE(args);
while(n--) {
obj = PyTuple_GET_ITEM(args, n);
+#if !defined(NPY_PY3K)
if (obj == (PyObject *)(&PyInt_Type)) {
PyLongArrType_Type.tp_as_number = saved_tables_arrtype[0];
PyLongArrType_Type.tp_compare = saved_tables_arrtype[1];
PyLongArrType_Type.tp_richcompare = saved_tables_arrtype[2];
}
- else if (obj == (PyObject *)(&PyFloat_Type)) {
+ else
+#else
+#warning XXX -- is it important to do something with the integers here?
+#endif
+ if (obj == (PyObject *)(&PyFloat_Type)) {
PyDoubleArrType_Type.tp_as_number = saved_tables_arrtype[3];
+#if !defined(NPY_PY3K)
PyDoubleArrType_Type.tp_compare = saved_tables_arrtype[4];
+#endif
PyDoubleArrType_Type.tp_richcompare = saved_tables_arrtype[5];
}
else if (obj == (PyObject *)(&PyComplex_Type)) {
PyCDoubleArrType_Type.tp_as_number = saved_tables_arrtype[6];
+#if !defined(NPY_PY3K)
PyCDoubleArrType_Type.tp_compare = saved_tables_arrtype[7];
+#endif
PyCDoubleArrType_Type.tp_richcompare = saved_tables_arrtype[8];
}
else {
@@ -1444,14 +1486,22 @@ PyMODINIT_FUNC initscalarmath(void) {
add_scalarmath();
+#if !defined(NPY_PY3K)
saved_tables[0] = PyInt_Type.tp_as_number;
saved_tables[1] = PyInt_Type.tp_compare;
saved_tables[2] = PyInt_Type.tp_richcompare;
+#else
+#warning XXX -- is it important to do something with the integers here?
+#endif
saved_tables[3] = PyFloat_Type.tp_as_number;
+#if !defined(NPY_PY3K)
saved_tables[4] = PyFloat_Type.tp_compare;
+#endif
saved_tables[5] = PyFloat_Type.tp_richcompare;
saved_tables[6] = PyComplex_Type.tp_as_number;
+#if !defined(NPY_PY3K)
saved_tables[7] = PyComplex_Type.tp_compare;
+#endif
saved_tables[8] = PyComplex_Type.tp_richcompare;
return;
@@ -14,6 +14,8 @@
#include "numpy/ufuncobject.h"
#include "numpy/npy_math.h"
+#include "npy_3kcompat.h"
+
#include "ufunc_object.h"
/*
@@ -1626,6 +1628,23 @@ OBJECT_@kind@(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func)
NPY_NO_EXPORT void
OBJECT_sign(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func))
{
+#if defined(NPY_PY3K)
+ PyObject *zero = PyLong_FromLong(0);
+ UNARY_LOOP {
+ PyObject *in1 = *(PyObject **)ip1;
+ PyObject **out = (PyObject **)op1;
+ int v;
+ PyObject *ret;
+ PyObject_Cmp(in1, zero, &v);
+ ret = PyLong_FromLong(v);
+ if (PyErr_Occurred()) {
+ return;
+ }
+ Py_XDECREF(*out);
+ *out = ret;
+ }
+ Py_DECREF(zero);
+#else
PyObject *zero = PyInt_FromLong(0);
UNARY_LOOP {
PyObject *in1 = *(PyObject **)ip1;
@@ -1638,6 +1657,7 @@ OBJECT_sign(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func))
*out = ret;
}
Py_DECREF(zero);
+#endif
}
/*

0 comments on commit 2649c52

Please sign in to comment.