Commit a8eb5671 authored by Boxiang Sun's avatar Boxiang Sun

fix the bug in complex constructor

Complex constructor should call __complex__ before call __float__.
And should not call PyNumber_Float in that special method.
Also add error checking after calling PyNumber_Float.
parent 92666536
...@@ -712,22 +712,6 @@ template <ExceptionStyle S> static Box* try_special_method(Box* self) noexcept(S ...@@ -712,22 +712,6 @@ template <ExceptionStyle S> static Box* try_special_method(Box* self) noexcept(S
return None; return None;
} }
static BoxedString* float_str = internStringImmortal("__float__");
if (PyObject_HasAttr((PyObject*)self, float_str)) {
Box* r_f = callattrInternal<S, NOT_REWRITABLE>(self, float_str, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL,
NULL, NULL, NULL);
if (!PyFloat_Check(r_f)) {
if (S == CAPI) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_TypeError, "__float__ returned non-float (type %.200s)", r_f->cls->tp_name);
return NULL;
} else {
raiseExcHelper(TypeError, "__float__ returned non-float (type %.200s)", r_f->cls->tp_name);
}
}
return (BoxedFloat*)r_f;
}
static BoxedString* complex_str = internStringImmortal("__complex__"); static BoxedString* complex_str = internStringImmortal("__complex__");
if (PyObject_HasAttr((PyObject*)self, complex_str)) { if (PyObject_HasAttr((PyObject*)self, complex_str)) {
Box* r = callattrInternal<S, NOT_REWRITABLE>(self, complex_str, CLASS_OR_INST, NULL, ArgPassSpec(0), NULL, NULL, Box* r = callattrInternal<S, NOT_REWRITABLE>(self, complex_str, CLASS_OR_INST, NULL, ArgPassSpec(0), NULL, NULL,
...@@ -754,6 +738,7 @@ template <ExceptionStyle S> static Box* try_special_method(Box* self) noexcept(S ...@@ -754,6 +738,7 @@ template <ExceptionStyle S> static Box* try_special_method(Box* self) noexcept(S
return static_cast<BoxedComplex*>(r); return static_cast<BoxedComplex*>(r);
} }
return None; return None;
} }
...@@ -777,16 +762,11 @@ template <ExceptionStyle S> Box* _complexNew(Box* real, Box* imag) noexcept(S == ...@@ -777,16 +762,11 @@ template <ExceptionStyle S> Box* _complexNew(Box* real, Box* imag) noexcept(S ==
} }
Box* _real = try_special_method<S>(real); Box* _real = try_special_method<S>(real);
Box* _imag = try_special_method<S>(imag);
if (_real != None && _real != NULL) { if (_real != None && _real != NULL) {
real = _real; real = _real;
} }
if (_imag != None && _imag != NULL) {
imag = _imag;
}
double real_f, imag_f; double real_f, imag_f;
bool real_is_complex = false, imag_is_complex = false; bool real_is_complex = false, imag_is_complex = false;
if (real == None || real == NULL) { if (real == None || real == NULL) {
...@@ -795,7 +775,10 @@ template <ExceptionStyle S> Box* _complexNew(Box* real, Box* imag) noexcept(S == ...@@ -795,7 +775,10 @@ template <ExceptionStyle S> Box* _complexNew(Box* real, Box* imag) noexcept(S ==
real_f = ((BoxedComplex*)real)->real; real_f = ((BoxedComplex*)real)->real;
real_is_complex = true; real_is_complex = true;
} else { } else {
real_f = ((BoxedFloat*)PyNumber_Float(real))->d; Box* res = PyNumber_Float(real);
if (!res)
throwCAPIException();
real_f = ((BoxedFloat*)res)->d;
} }
if (imag == None || imag == NULL) { if (imag == None || imag == NULL) {
imag_f = 0.0; imag_f = 0.0;
...@@ -803,7 +786,10 @@ template <ExceptionStyle S> Box* _complexNew(Box* real, Box* imag) noexcept(S == ...@@ -803,7 +786,10 @@ template <ExceptionStyle S> Box* _complexNew(Box* real, Box* imag) noexcept(S ==
imag_f = ((BoxedComplex*)imag)->real; imag_f = ((BoxedComplex*)imag)->real;
imag_is_complex = true; imag_is_complex = true;
} else { } else {
imag_f = ((BoxedFloat*)PyNumber_Float(imag))->d; Box* res = PyNumber_Float(imag);
if (!res)
throwCAPIException();
imag_f = ((BoxedFloat*)res)->d;
} }
if (imag_is_complex) { if (imag_is_complex) {
...@@ -817,6 +803,7 @@ template <ExceptionStyle S> Box* _complexNew(Box* real, Box* imag) noexcept(S == ...@@ -817,6 +803,7 @@ template <ExceptionStyle S> Box* _complexNew(Box* real, Box* imag) noexcept(S ==
return new BoxedComplex(real_f, imag_f); return new BoxedComplex(real_f, imag_f);
} }
template <ExceptionStyle S> Box* complexNew(BoxedClass* _cls, Box* _real, Box* _imag) noexcept(S == CAPI) { template <ExceptionStyle S> Box* complexNew(BoxedClass* _cls, Box* _real, Box* _imag) noexcept(S == CAPI) {
if (!isSubclass(_cls->cls, type_cls)) { if (!isSubclass(_cls->cls, type_cls)) {
if (S == CAPI) { if (S == CAPI) {
......
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