Commit 2a3b70eb authored by Boxiang Sun's avatar Boxiang Sun

return NotImplemented in intXXXFloat for float subtypes

parent 94020486
......@@ -430,7 +430,7 @@ extern "C" Box* intAdd(BoxedInt* lhs, Box* rhs) {
if (PyInt_Check(rhs)) {
BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs);
return add_i64_i64(lhs->n, rhs_int->n);
} else if (PyFloat_Check(rhs)) {
} else if (PyFloat_CheckExact(rhs)) {
BoxedFloat* rhs_float = static_cast<BoxedFloat*>(rhs);
return boxFloat(lhs->n + rhs_float->d);
} else {
......@@ -559,7 +559,7 @@ extern "C" Box* intDiv(BoxedInt* lhs, Box* rhs) {
if (PyInt_Check(rhs)) {
return intDivInt(lhs, static_cast<BoxedInt*>(rhs));
} else if (PyFloat_Check(rhs)) {
} else if (PyFloat_CheckExact(rhs)) {
return intDivFloat(lhs, static_cast<BoxedFloat*>(rhs));
} else {
return NotImplemented;
......@@ -601,7 +601,7 @@ extern "C" Box* intFloordiv(BoxedInt* lhs, Box* rhs) {
if (PyInt_Check(rhs)) {
return intFloordivInt(lhs, static_cast<BoxedInt*>(rhs));
} else if (PyFloat_Check(rhs)) {
} else if (PyFloat_CheckExact(rhs)) {
return intFloordivFloat(lhs, static_cast<BoxedFloat*>(rhs));
} else {
return NotImplemented;
......@@ -647,7 +647,7 @@ extern "C" Box* intTruediv(BoxedInt* lhs, Box* rhs) {
if (PyInt_Check(rhs)) {
return intTruedivInt(lhs, static_cast<BoxedInt*>(rhs));
} else if (PyFloat_Check(rhs)) {
} else if (PyFloat_CheckExact(rhs)) {
return intTruedivFloat(lhs, static_cast<BoxedFloat*>(rhs));
} else {
return NotImplemented;
......@@ -790,7 +790,7 @@ extern "C" Box* intMul(BoxedInt* lhs, Box* rhs) {
if (PyInt_Check(rhs)) {
BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs);
return intMulInt(lhs, rhs_int);
} else if (PyFloat_Check(rhs)) {
} else if (PyFloat_CheckExact(rhs)) {
BoxedFloat* rhs_float = static_cast<BoxedFloat*>(rhs);
return intMulFloat(lhs, rhs_float);
} else {
......@@ -842,7 +842,7 @@ extern "C" Box* intPow(BoxedInt* lhs, Box* rhs, Box* mod) {
if (PyLong_Check(rhs))
return intPowLong(lhs, static_cast<BoxedLong*>(rhs), mod);
else if (PyFloat_Check(rhs))
else if (PyFloat_CheckExact(rhs))
return intPowFloat(lhs, static_cast<BoxedFloat*>(rhs), mod);
else if (!PyInt_Check(rhs))
return NotImplemented;
......@@ -937,7 +937,7 @@ extern "C" Box* intSub(BoxedInt* lhs, Box* rhs) {
if (PyInt_Check(rhs)) {
BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs);
return intSubInt(lhs, rhs_int);
} else if (PyFloat_Check(rhs)) {
} else if (PyFloat_CheckExact(rhs)) {
BoxedFloat* rhs_float = static_cast<BoxedFloat*>(rhs);
return intSubFloat(lhs, rhs_float);
} else {
......
......@@ -141,3 +141,35 @@ for x in data:
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)
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