@@ -637,16 +637,26 @@ do_richcompare_on_scalars(PyObject *self, PyObject *other, int cmp_op) {
637
637
descr_from_basic_scalar (self , & self_descr );
638
638
pyscalar_other = descr_from_basic_scalar (other , & other_descr );
639
639
640
- if (NPY_LIKELY (pyscalar_other > 0 )) {
641
- data_self = scalar_value (self , NULL );
642
- item_self = self_descr -> f -> getitem (data_self , NULL );
643
-
644
- ret = PyObject_RichCompare (item_self , other , cmp_op );
645
- Py_DECREF (item_self );
640
+ /* If either of the operands are complex AND the operator is not equality,
641
+ * python's built-in richcompare cannot be used as it is not supported */
642
+ if ((self_descr -> type_num < NPY_CFLOAT &&
643
+ (other_descr != NULL && other_descr -> type_num < NPY_CFLOAT ))
644
+ || (cmp_op == Py_EQ || cmp_op == Py_NE )) {
645
+ if (NPY_LIKELY (pyscalar_other > 0 )) {
646
+ data_self = scalar_value (self , NULL );
647
+ item_self = self_descr -> f -> getitem (data_self , NULL );
648
+
649
+ ret = PyObject_RichCompare (item_self , other , cmp_op );
650
+ Py_DECREF (item_self );
646
651
652
+ }
653
+ }
654
+ if (NPY_LIKELY (self_descr != NULL )) {
655
+ Py_DECREF (self_descr );
656
+ }
657
+ if (other_descr != NULL ) {
658
+ Py_DECREF (other_descr );
647
659
}
648
- Py_DECREF (self_descr );
649
- Py_DECREF (other_descr );
650
660
651
661
return ret ;
652
662
}
0 commit comments