Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__lt__, __gt__ and others rich comparisons behave differently as for example __iadd__ #1935

Closed
realead opened this issue Oct 13, 2017 · 1 comment

Comments

@realead
Copy link
Contributor

realead commented Oct 13, 2017

The following looks inconsistent:

A small reproducer with cython 0.27.1:

cdef class MutableFloat:
    cdef double x
    def __cinit__(self, x):
        self.x=x
    def __iadd__(self, MutableFloat other):
        self.x=self.x+other.x
        return self
    def __lt__(self,  MutableFloat other):
        return self.x<other.x
    def __gt__(self, MutableFloat other):
        return self.x>other.x
    def __repr__(self):
        return str(self.x)

and now:

  a,b=MutableFloat(1.0), MutableFloat(2.0)
  a+=b #a=3.0, works
  a<b  #error x unknown
  a>b #error x unknown

when we look at the cythonized functions:

static PyObject *__pyx_pf_7mutable_12MutableFloat_2__iadd__(struct __pyx_obj_7mutable_MutableFloat *__pyx_v_self, struct __pyx_obj_7mutable_MutableFloat *__pyx_v_other); /* proto */
static PyObject *__pyx_pf_7mutable_12MutableFloat_4__lt__(PyObject *__pyx_v_self, struct __pyx_obj_7mutable_MutableFloat *__pyx_v_other); /* proto */
static PyObject *__pyx_pf_7mutable_12MutableFloat_6__gt__(PyObject *__pyx_v_self, struct __pyx_obj_7mutable_MutableFloat *__pyx_v_other); /* proto */

for __iadd__ (and any other functions) self is xxx_MutableFloat but for rich comparisons PyObject *. Maybe this is how it should be, but I didn't find anything about this potential problem in the documentation. Anyway if this is "functioning as designed", the need to declare def __gt__(MutableFloat self, MutableFloat other): looks strange to me.

@scoder
Copy link
Contributor

scoder commented Oct 13, 2017

You are right. Python specifies their signature as self, other.

@scoder scoder added this to the 0.27.2 milestone Oct 13, 2017
@scoder scoder closed this as completed in 628ffd7 Oct 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants