Skip to content

Commit

Permalink
let numeric operation can find correct function
Browse files Browse the repository at this point in the history
  • Loading branch information
Boxiang Sun committed Feb 17, 2016
1 parent 746fc43 commit 989a329
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/runtime/float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ extern "C" Box* floatDiv(BoxedFloat* lhs, Box* rhs) {
assert(PyFloat_Check(lhs));
if (PyInt_Check(rhs)) {
return floatDivInt(lhs, static_cast<BoxedInt*>(rhs));
} else if (PyFloat_Check(rhs)) {
} else if (rhs->cls == float_cls) {
return floatDivFloat(lhs, static_cast<BoxedFloat*>(rhs));
} else if (PyLong_Check(rhs)) {
double rhs_f = PyLong_AsDouble(rhs);
Expand All @@ -178,7 +178,7 @@ extern "C" Box* floatTruediv(BoxedFloat* lhs, Box* rhs) {
assert(PyFloat_Check(lhs));
if (PyInt_Check(rhs)) {
return floatDivInt(lhs, static_cast<BoxedInt*>(rhs));
} else if (PyFloat_Check(rhs)) {
} else if (rhs->cls == float_cls) {
return floatDivFloat(lhs, static_cast<BoxedFloat*>(rhs));
} else if (PyLong_Check(rhs)) {
double rhs_f = PyLong_AsDouble(rhs);
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,9 @@ extern "C" Box* intDiv(BoxedInt* lhs, Box* rhs) {
if (!PyInt_Check(lhs))
raiseExcHelper(TypeError, "descriptor '__div__' requires a 'int' object but received a '%s'", getTypeName(lhs));

if (PyInt_Check(rhs)) {
if (rhs->cls == int_cls) {
return intDivInt(lhs, static_cast<BoxedInt*>(rhs));
} else if (PyFloat_Check(rhs)) {
} else if (rhs->cls == float_cls) {
return intDivFloat(lhs, static_cast<BoxedFloat*>(rhs));
} else {
return NotImplemented;
Expand Down Expand Up @@ -641,9 +641,9 @@ extern "C" Box* intTruediv(BoxedInt* lhs, Box* rhs) {
raiseExcHelper(TypeError, "descriptor '__truediv__' requires a 'int' object but received a '%s'",
getTypeName(lhs));

if (PyInt_Check(rhs)) {
if (rhs->cls == int_cls) {
return intTruedivInt(lhs, static_cast<BoxedInt*>(rhs));
} else if (PyFloat_Check(rhs)) {
} else if (rhs->cls == float_cls) {
return intTruedivFloat(lhs, static_cast<BoxedFloat*>(rhs));
} else {
return NotImplemented;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/long.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ Box* longTrueDiv(BoxedLong* v1, Box* _v2) {
getTypeName(v1));

BoxedLong* v2;
if (PyInt_Check(_v2) || PyLong_Check(_v2)) {
if (PyInt_Check(_v2) || _v2->cls == long_cls) {
v2 = (BoxedLong*)PyNumber_Long(_v2);
if (!v2) {
throwCAPIException();
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/objmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4565,6 +4565,13 @@ Box* binopInternal(Box* lhs, Box* rhs, int op_type, bool inplace, BinopRewriteAr
assert(rewrite_args->out_rtn);
rewrite_args->out_success = true;
}

if (rhs->cls != lhs->cls && isSubclass(rhs->cls, lhs->cls)) {
BoxedString* rop_name = getReverseOpName(op_type);
Box* rrtn = callattrInternal1<CXX, REWRITABLE>(rhs, rop_name, CLASS_ONLY, NULL, ArgPassSpec(1), lhs);
if (rrtn != NULL && rrtn != NotImplemented)
return rrtn;
}
return lrtn;
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/tests/float.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,35 @@ class F2(float):
arg2=y)))
except Exception as e:
print(e.message)


class Foo1(float):
def __rdiv__(self, other):
print("float custom operation called")
return self / other


class Foo2(long):
def __rdiv__(self, other):
print("long custom operation called")
return self / other


class Foo3(int):
def __rdiv__(self, other):
print("int custom operation called")
return self / other

a = Foo1(1.5)
b = Foo2(1L)
c = Foo3(1)

print(1.5 / a)
print(1.5 / b)
print(1.5 / c)
print(1 / a)
print(1 / b)
print(1 / c)
print(1L / a)
print(1L / b)
print(1L / c)

0 comments on commit 989a329

Please sign in to comment.