Commit 1ef88c12 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #652 from Daetalus/master

For issue #254, add lt, le, gt, ge to old-style class
parents 5ebb8cab 3974e784
...@@ -741,6 +741,22 @@ Box* _instanceBinary(Box* _inst, Box* other, const char* attr) { ...@@ -741,6 +741,22 @@ Box* _instanceBinary(Box* _inst, Box* other, const char* attr) {
return runtimeCall(func, ArgPassSpec(1), other, NULL, NULL, NULL, NULL); return runtimeCall(func, ArgPassSpec(1), other, NULL, NULL, NULL, NULL);
} }
Box* instanceGt(Box* _inst, Box* other) {
return _instanceBinary(_inst, other, "__gt__");
}
Box* instanceGe(Box* _inst, Box* other) {
return _instanceBinary(_inst, other, "__ge__");
}
Box* instanceLt(Box* _inst, Box* other) {
return _instanceBinary(_inst, other, "__lt__");
}
Box* instanceLe(Box* _inst, Box* other) {
return _instanceBinary(_inst, other, "__le__");
}
Box* instanceEq(Box* _inst, Box* other) { Box* instanceEq(Box* _inst, Box* other) {
return _instanceBinary(_inst, other, "__eq__"); return _instanceBinary(_inst, other, "__eq__");
} }
...@@ -856,6 +872,10 @@ void setupClassobj() { ...@@ -856,6 +872,10 @@ void setupClassobj() {
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("__eq__", new BoxedFunction(boxRTFunction((void*)instanceEq, UNKNOWN, 2)));
instance_cls->giveAttr("__ne__", new BoxedFunction(boxRTFunction((void*)instanceNe, UNKNOWN, 2))); instance_cls->giveAttr("__ne__", new BoxedFunction(boxRTFunction((void*)instanceNe, UNKNOWN, 2)));
instance_cls->giveAttr("__lt__", new BoxedFunction(boxRTFunction((void*)instanceLt, UNKNOWN, 2)));
instance_cls->giveAttr("__le__", new BoxedFunction(boxRTFunction((void*)instanceLe, UNKNOWN, 2)));
instance_cls->giveAttr("__gt__", new BoxedFunction(boxRTFunction((void*)instanceGt, UNKNOWN, 2)));
instance_cls->giveAttr("__ge__", new BoxedFunction(boxRTFunction((void*)instanceGe, UNKNOWN, 2)));
instance_cls->freeze(); instance_cls->freeze();
instance_cls->tp_getattro = instance_getattro; instance_cls->tp_getattro = instance_getattro;
......
...@@ -128,6 +128,61 @@ print issubclass(OldStyleClass, object) ...@@ -128,6 +128,61 @@ print issubclass(OldStyleClass, object)
print isinstance(OldStyleClass(), OldStyleClass) print isinstance(OldStyleClass(), OldStyleClass)
print issubclass(OldStyleClass, OldStyleClass) print issubclass(OldStyleClass, OldStyleClass)
class LessEqualTest:
def __init__(self, a):
self._a = a
def __lt__(self, other):
print "lt"
return self._a < other._a
def __le__(self, other):
print "le"
return self._a <= other._a
ca = LessEqualTest(3)
cb = LessEqualTest(2)
cc = LessEqualTest(2)
print(ca < cb)
print(cb < ca)
print(ca <= cb)
print(ca <= cc)
# CompareA only defines __lt__ and __le__ but appears on the right-hand-side
print(ca > cb)
print(ca >= cb)
print(cb > ca)
print(cb > cc)
class GreatEqualTest:
def __init__(self, a):
self._a = a
def __gt__(self, other):
print "gt"
return self._a > other._a
def __ge__(self, other):
print "ge"
return self._a >= other._a
cd = GreatEqualTest(4)
ce = GreatEqualTest(5)
cf = GreatEqualTest(4)
print(cd >= ce)
print(cd > ce)
print(cd >= cf)
print(cd > cf)
# CompareB only defines __gt__ and __ge__ but appears on the right-hand-side
print(cd <= ce)
print(cd < ce)
print(cd <= cf)
print(cd < cf)
class GetattrTest: class GetattrTest:
def __getattr__(self, attr): def __getattr__(self, attr):
print "getattr", attr print "getattr", attr
......
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