Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some minor fixing which found in numpy tests #1064

Merged
merged 3 commits into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 9 additions & 53 deletions src/runtime/float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,59 +838,15 @@ Box* floatNonzero(BoxedFloat* self) {
return boxBool(floatNonzeroUnboxed(self));
}

template <ExceptionStyle S> static BoxedFloat* _floatNew(Box* a) noexcept(S == CAPI) {
if (a->cls == float_cls) {
return static_cast<BoxedFloat*>(a);
} else if (PyInt_Check(a)) {
return new BoxedFloat(static_cast<BoxedInt*>(a)->n);

} else if (PyLong_Check(a)) {
double a_f = PyLong_AsDouble(a);
if (a_f == -1.0 && PyErr_Occurred()) {
if (S == CAPI)
return NULL;
else
throwCAPIException();
}

// Make sure that we're not in an error state when we return a non-NULL value.
assert(!PyErr_Occurred());
return new BoxedFloat(a_f);
} else if (a->cls == str_cls || a->cls == unicode_cls) {
BoxedFloat* res = (BoxedFloat*)PyFloat_FromString(a, NULL);

if (!res) {
if (S == CAPI)
return NULL;
else
throwCAPIException();
}

return res;
} else {
static BoxedString* float_str = internStringImmortal("__float__");
Box* r = callattrInternal<S, NOT_REWRITABLE>(a, float_str, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL, NULL,
NULL, NULL);

if (!r) {
if (S == CAPI) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_TypeError, "float() argument must be a string or a number");
return NULL;
} else {
raiseExcHelper(TypeError, "float() argument must be a string or a number");
}
}

if (!PyFloat_Check(r)) {
if (S == CAPI) {
PyErr_Format(TypeError, "__float__ returned non-float (type %s)", r->cls->tp_name);
return NULL;
} else
raiseExcHelper(TypeError, "__float__ returned non-float (type %s)", r->cls->tp_name);
}
return static_cast<BoxedFloat*>(r);
}
template <ExceptionStyle S> static BoxedFloat* _floatNew(Box* x) noexcept(S == CAPI) {
Box* rtn;
if (PyString_CheckExact(x))
rtn = PyFloat_FromString(x, NULL);
else
rtn = PyNumber_Float(x);
if (!rtn && S == CXX)
throwCAPIException();
return static_cast<BoxedFloat*>(rtn);
}

template <ExceptionStyle S> Box* floatNew(BoxedClass* _cls, Box* a) noexcept(S == CAPI) {
Expand Down
14 changes: 7 additions & 7 deletions src/runtime/int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions src/runtime/long.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,39 @@ Box* longStr(BoxedLong* v) {
return _PyLong_Format(v, 10, 0 /* no L */, 0);
}

Box* long__format__(BoxedLong* self, Box* format_spec) noexcept {
if (PyBytes_Check(format_spec))
return _PyLong_FormatAdvanced(self, PyBytes_AS_STRING(format_spec), PyBytes_GET_SIZE(format_spec));
if (PyUnicode_Check(format_spec)) {
/* Convert format_spec to a str */
PyObject* result;
PyObject* str_spec = PyObject_Str(format_spec);

if (str_spec == NULL)
return NULL;

result = _PyLong_FormatAdvanced(self, PyBytes_AS_STRING(str_spec), PyBytes_GET_SIZE(str_spec));

Py_DECREF(str_spec);
return result;
}
PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
return NULL;
}

Box* longFormat(BoxedLong* self, Box* format_spec) {
if (!PyLong_Check(self))
raiseExcHelper(TypeError, "descriptor '__format__' requires a 'long' object but received a '%s'",
getTypeName(self));

Box* res = long__format__(self, format_spec);

if (res == NULL)
throwCAPIException();

return res;
}

Box* longBin(BoxedLong* v) {
if (!PyLong_Check(v))
raiseExcHelper(TypeError, "descriptor '__bin__' requires a 'long' object but received a '%s'", getTypeName(v));
Expand Down Expand Up @@ -1713,6 +1746,7 @@ void setupLong() {
long_cls->giveAttr("__float__", new BoxedFunction(FunctionMetadata::create((void*)longFloat, UNKNOWN, 1)));
long_cls->giveAttr("__repr__", new BoxedFunction(FunctionMetadata::create((void*)longRepr, STR, 1)));
long_cls->giveAttr("__str__", new BoxedFunction(FunctionMetadata::create((void*)longStr, STR, 1)));
long_cls->giveAttr("__format__", new BoxedFunction(FunctionMetadata::create((void*)longFormat, STR, 2)));
long_cls->giveAttr("__bin__", new BoxedFunction(FunctionMetadata::create((void*)longBin, STR, 1)));
long_cls->giveAttr("__hex__", new BoxedFunction(FunctionMetadata::create((void*)longHex, STR, 1)));
long_cls->giveAttr("__oct__", new BoxedFunction(FunctionMetadata::create((void*)longOct, STR, 1)));
Expand Down
32 changes: 32 additions & 0 deletions test/tests/float.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,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)
2 changes: 2 additions & 0 deletions test/tests/long.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,5 @@ class long2(long):
arg2=y)))
except Exception as e:
print(e.message)

print(long.__format__(130L, 'd'))