Commit 3ec9cf9c authored by Marius Wachtler's avatar Marius Wachtler

Merge pull request #1064 from Daetalus/numpy_fixing_2

Some minor fixing which found in numpy tests
parents bf7972d1 7819995d
......@@ -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) {
......
......@@ -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 {
......
......@@ -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));
......@@ -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)));
......
......@@ -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)
......@@ -203,3 +203,5 @@ for x in data:
arg2=y)))
except Exception as e:
print(e.message)
print(long.__format__(130L, 'd'))
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