Commit d660c034 authored by Marius Wachtler's avatar Marius Wachtler

Add __eq__ and __ne__ support for old style classes

parent 49fcc87c
...@@ -511,6 +511,26 @@ static PyObject* instance_index(PyObject* self) noexcept { ...@@ -511,6 +511,26 @@ static PyObject* instance_index(PyObject* self) noexcept {
return res; return res;
} }
Box* instanceEq(Box* _inst, Box* other) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
Box* func = _instanceGetattribute(inst, boxStrConstant("__eq__"), false);
if (!func)
return NotImplemented;
return runtimeCall(func, ArgPassSpec(1), other, NULL, NULL, NULL, NULL);
}
Box* instanceNe(Box* _inst, Box* other) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
Box* func = _instanceGetattribute(inst, boxStrConstant("__ne__"), false);
if (!func)
return NotImplemented;
return runtimeCall(func, ArgPassSpec(1), other, NULL, NULL, NULL, NULL);
}
Box* instanceCall(Box* _inst, Box* _args, Box* _kwargs) { Box* instanceCall(Box* _inst, Box* _args, Box* _kwargs) {
assert(_inst->cls == instance_cls); assert(_inst->cls == instance_cls);
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst); BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
...@@ -560,6 +580,8 @@ void setupClassobj() { ...@@ -560,6 +580,8 @@ void setupClassobj() {
instance_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)instanceHash, UNKNOWN, 1))); instance_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)instanceHash, UNKNOWN, 1)));
instance_cls->giveAttr("__call__", instance_cls->giveAttr("__call__",
new BoxedFunction(boxRTFunction((void*)instanceCall, UNKNOWN, 1, 0, true, true))); new BoxedFunction(boxRTFunction((void*)instanceCall, UNKNOWN, 1, 0, true, true)));
instance_cls->giveAttr("__eq__", new BoxedFunction(boxRTFunction((void*)instanceEq, UNKNOWN, 2)));
instance_cls->giveAttr("__ne__", new BoxedFunction(boxRTFunction((void*)instanceNe, UNKNOWN, 2)));
instance_cls->freeze(); instance_cls->freeze();
instance_cls->tp_getattro = instance_getattro; instance_cls->tp_getattro = instance_getattro;
......
...@@ -55,6 +55,14 @@ class E(): ...@@ -55,6 +55,14 @@ class E():
print "f", a print "f", a
return f return f
def __eq__(self, other):
print "eq"
return self.n == other.n
def __ne__(self, other):
print "ne"
return self.n != other.n
e = E(1) e = E(1)
print e print e
print e.n print e.n
...@@ -63,6 +71,8 @@ print e[1] ...@@ -63,6 +71,8 @@ print e[1]
print e[1:2] print e[1:2]
print len(e) print len(e)
print e()("test") print e()("test")
print e == E(1)
print e != E(1)
def str2(): def str2():
return "str2" return "str2"
...@@ -82,6 +92,8 @@ class F: ...@@ -82,6 +92,8 @@ class F:
print bool(F(0)) print bool(F(0))
print bool(F(1)) print bool(F(1))
print F(0) == F(0)
print F(0) != F(0)
f = F(0) f = F(0)
try: try:
......
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