Commit 71e74437 authored by Marius Wachtler's avatar Marius Wachtler

Add PyNumber_TrueDivide and basic long.__truediv__() support

but both args have to fit into an int...
parent 47ba50be
......@@ -1209,8 +1209,13 @@ extern "C" PyObject* PyNumber_FloorDivide(PyObject*, PyObject*) noexcept {
Py_FatalError("unimplemented");
}
extern "C" PyObject* PyNumber_TrueDivide(PyObject*, PyObject*) noexcept {
Py_FatalError("unimplemented");
extern "C" PyObject* PyNumber_TrueDivide(PyObject* lhs, PyObject* rhs) noexcept {
try {
return binop(lhs, rhs, AST_TYPE::TrueDiv);
} catch (ExcInfo e) {
setCAPIException(e);
return NULL;
}
}
extern "C" PyObject* PyNumber_Remainder(PyObject* lhs, PyObject* rhs) noexcept {
......
......@@ -980,6 +980,44 @@ Box* longRdiv(BoxedLong* v1, Box* _v2) {
}
}
Box* longTrueDiv(BoxedLong* v1, Box* _v2) {
if (!isSubclass(v1->cls, long_cls))
raiseExcHelper(TypeError, "descriptor '__truediv__' requires a 'long' object but received a '%s'",
getTypeName(v1));
// We only support args which fit into an int for now...
int overflow = 0;
long lhs = PyLong_AsLongAndOverflow(v1, &overflow);
if (overflow)
return NotImplemented;
long rhs = PyLong_AsLongAndOverflow(_v2, &overflow);
if (overflow)
return NotImplemented;
if (rhs == 0)
raiseExcHelper(ZeroDivisionError, "division by zero");
return boxFloat(lhs / (double)rhs);
}
Box* longRTrueDiv(BoxedLong* v1, Box* _v2) {
if (!isSubclass(v1->cls, long_cls))
raiseExcHelper(TypeError, "descriptor '__rtruediv__' requires a 'long' object but received a '%s'",
getTypeName(v1));
// We only support args which fit into an int for now...
int overflow = 0;
long lhs = PyLong_AsLongAndOverflow(_v2, &overflow);
if (overflow)
return NotImplemented;
long rhs = PyLong_AsLongAndOverflow(v1, &overflow);
if (overflow)
return NotImplemented;
if (rhs == 0)
raiseExcHelper(ZeroDivisionError, "division by zero");
return boxFloat(lhs / (double)rhs);
}
Box* longPow(BoxedLong* v1, Box* _v2) {
if (!isSubclass(v1->cls, long_cls))
raiseExcHelper(TypeError, "descriptor '__pow__' requires a 'long' object but received a '%s'", getTypeName(v1));
......@@ -1074,6 +1112,8 @@ void setupLong() {
long_cls->giveAttr("__div__", new BoxedFunction(boxRTFunction((void*)longDiv, UNKNOWN, 2)));
long_cls->giveAttr("__rdiv__", new BoxedFunction(boxRTFunction((void*)longRdiv, UNKNOWN, 2)));
long_cls->giveAttr("__truediv__", new BoxedFunction(boxRTFunction((void*)longTrueDiv, UNKNOWN, 2)));
long_cls->giveAttr("__rtruediv__", new BoxedFunction(boxRTFunction((void*)longRTrueDiv, UNKNOWN, 2)));
long_cls->giveAttr("__divmod__", new BoxedFunction(boxRTFunction((void*)longDivmod, UNKNOWN, 2)));
......
......@@ -31,10 +31,9 @@ for a in [-5, -1, 1, 5, -2L, -1L, 1L, 2L, 15L]:
test(1L, 2.0)
test(3.0, 2L)
print (2L).__rdiv__(-1)
print (2L).__rdiv__(-1L)
print (-2L).__rdiv__(1L)
print (-2L).__rdiv__(1)
for lhs in [2L, -2L]:
for rhs in [-1, -1L, 1, 2L]:
print lhs.__rdiv__(rhs), lhs.__truediv__(rhs), lhs.__rtruediv__(rhs)
print (1L) << (2L)
print (1L) << (2)
......
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