Commit c062260b authored by Stefan Behnel's avatar Stefan Behnel

Test and fix "__eq__" override with extern base type (tested with 'list').

parent 4a196738
...@@ -1830,17 +1830,20 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1830,17 +1830,20 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln("case Py_%s: {" % cmp_type) code.putln("case Py_%s: {" % cmp_type)
if cmp_method == '__eq__': if cmp_method == '__eq__':
eq_entry = entry eq_entry = entry
code.putln("if (o1 == o2) return __Pyx_NewRef(Py_True);") # Python itself does not do this optimisation, it seems...
#code.putln("if (o1 == o2) return __Pyx_NewRef(Py_True);")
elif cmp_method == '__ne__': elif cmp_method == '__ne__':
has_ne = True has_ne = True
code.putln("if (o1 == o2) return __Pyx_NewRef(Py_False);") # Python itself does not do this optimisation, it seems...
#code.putln("if (o1 == o2) return __Pyx_NewRef(Py_False);")
code.putln("return %s(o1, o2);" % entry.func_cname) code.putln("return %s(o1, o2);" % entry.func_cname)
code.putln("}") code.putln("}")
if eq_entry and not has_ne and not extern_parent: if eq_entry and not has_ne and not extern_parent:
code.putln("case Py_NE: {") code.putln("case Py_NE: {")
code.putln("PyObject *ret;") code.putln("PyObject *ret;")
code.putln("if (o1 == o2) return __Pyx_NewRef(Py_False);") # Python itself does not do this optimisation, it seems...
#code.putln("if (o1 == o2) return __Pyx_NewRef(Py_False);")
code.putln("ret = %s(o1, o2);" % eq_entry.func_cname) code.putln("ret = %s(o1, o2);" % eq_entry.func_cname)
code.putln("if (likely(ret && ret != Py_NotImplemented)) {") code.putln("if (likely(ret && ret != Py_NotImplemented)) {")
code.putln("int b = __Pyx_PyObject_IsTrue(ret); Py_DECREF(ret);") code.putln("int b = __Pyx_PyObject_IsTrue(ret); Py_DECREF(ret);")
......
...@@ -424,3 +424,21 @@ class ClassLtGt(X): ...@@ -424,3 +424,21 @@ class ClassLtGt(X):
if isinstance(other, X): if isinstance(other, X):
return self.x > other.x return self.x > other.x
return NotImplemented return NotImplemented
@cython.cclass
class List(list):
"""
>>> l = [1, 2, 3, 4]
>>> notl = List(l)
>>> notl == l
False
>>> notl != l # implemented by base type
False
>>> notl == notl
True
>>> notl != notl # implemented by base type
False
"""
def __eq__(self, other):
return self is other or list(self) != list(other)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment