From 948bd3f646d24c27e6895b536c5b014aabcfb6e9 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 14 May 2016 19:30:43 +0200 Subject: [PATCH] + fixes #0002554: Py Quantity Constructor Angles --- src/Base/QuantityPyImp.cpp | 115 ++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/src/Base/QuantityPyImp.cpp b/src/Base/QuantityPyImp.cpp index 25ee34ad9..183bb2779 100644 --- a/src/Base/QuantityPyImp.cpp +++ b/src/Base/QuantityPyImp.cpp @@ -53,7 +53,28 @@ int QuantityPy::PyInit(PyObject* args, PyObject* kwd) { Quantity *self = getQuantityPtr(); + PyErr_Clear(); // set by PyArg_ParseTuple() + PyObject *object; + if (PyArg_ParseTuple(args,"O!",&(Base::QuantityPy::Type), &object)) { + // Note: must be static_cast, not reinterpret_cast + *self = *(static_cast(object)->getQuantityPtr()); + return 0; + } + + PyErr_Clear(); // set by PyArg_ParseTuple() double f = DOUBLE_MAX; + if (PyArg_ParseTuple(args,"dO!",&f,&(Base::UnitPy::Type), &object)) { + // Note: must be static_cast, not reinterpret_cast + *self = Quantity(f,*(static_cast(object)->getUnitPtr())); + return 0; + } + + PyErr_Clear(); // set by PyArg_ParseTuple() + if (PyArg_ParseTuple(args,"dO!",&f,&(Base::QuantityPy::Type), &object)) { + PyErr_SetString(PyExc_TypeError, "Second argument must be a Unit not a Quantity"); + return -1; + } + int i1=0; int i2=0; int i3=0; @@ -63,25 +84,12 @@ int QuantityPy::PyInit(PyObject* args, PyObject* kwd) int i7=0; int i8=0; if (PyArg_ParseTuple(args, "|diiiiiiii", &f,&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8)) { - if(f!=DOUBLE_MAX) + if (f != DOUBLE_MAX) { *self = Quantity(f,Unit(i1,i2,i3,i4,i5,i6,i7,i8)); + } return 0; } - PyErr_Clear(); // set by PyArg_ParseTuple() - PyObject *object; - - if (PyArg_ParseTuple(args,"O!",&(Base::QuantityPy::Type), &object)) { - // Note: must be static_cast, not reinterpret_cast - *self = *(static_cast(object)->getQuantityPtr()); - return 0; - } - PyErr_Clear(); // set by PyArg_ParseTuple() - if (PyArg_ParseTuple(args,"dO!",&f,&(Base::UnitPy::Type), &object)) { - // Note: must be static_cast, not reinterpret_cast - *self = Quantity(f,*(static_cast(object)->getUnitPtr())); - return 0; - } PyErr_Clear(); // set by PyArg_ParseTuple() char* string; if (PyArg_ParseTuple(args,"et", "utf-8", &string)) { @@ -89,7 +97,8 @@ int QuantityPy::PyInit(PyObject* args, PyObject* kwd) PyMem_Free(string); try { *self = Quantity::parse(qstr); - }catch(const Base::Exception& e) { + } + catch(const Base::Exception& e) { PyErr_SetString(PyExc_ValueError, e.what()); return-1; } @@ -97,7 +106,7 @@ int QuantityPy::PyInit(PyObject* args, PyObject* kwd) return 0; } - PyErr_SetString(PyExc_TypeError, "Either three floats, tuple or Vector expected"); + PyErr_SetString(PyExc_TypeError, "Either quantity, float with units or string expected"); return -1; } @@ -463,72 +472,72 @@ PyObject* QuantityPy::richCompare(PyObject *v, PyObject *w, int op) const Quantity * u2 = static_cast(w)->getQuantityPtr(); PyObject *res=0; - if (op == Py_NE) { - res = (!(*u1 == *u2)) ? Py_True : Py_False; + if (op == Py_NE) { + res = (!(*u1 == *u2)) ? Py_True : Py_False; Py_INCREF(res); return res; - } - else if (op == Py_LT) { - res = (*u1 < *u2) ? Py_True : Py_False; + } + else if (op == Py_LT) { + res = (*u1 < *u2) ? Py_True : Py_False; Py_INCREF(res); return res; - } - else if (op == Py_LE) { - res = (*u1 < *u2)||(*u1 == *u2) ? Py_True : Py_False; + } + else if (op == Py_LE) { + res = (*u1 < *u2)||(*u1 == *u2) ? Py_True : Py_False; Py_INCREF(res); return res; - } - else if (op == Py_GT) { - res = (!(*u1 < *u2))&&(!(*u1 == *u2)) ? Py_True : Py_False; + } + else if (op == Py_GT) { + res = (!(*u1 < *u2))&&(!(*u1 == *u2)) ? Py_True : Py_False; Py_INCREF(res); return res; - } - else if (op == Py_GE) { - res = (!(*u1 < *u2)) ? Py_True : Py_False; + } + else if (op == Py_GE) { + res = (!(*u1 < *u2)) ? Py_True : Py_False; Py_INCREF(res); return res; - } - else if (op == Py_EQ) { - res = (*u1 == *u2) ? Py_True : Py_False; + } + else if (op == Py_EQ) { + res = (*u1 == *u2) ? Py_True : Py_False; Py_INCREF(res); return res; - } + } } else if (PyNumber_Check(v) && PyNumber_Check(w)) { // Try to get floating numbers double u1 = PyFloat_AsDouble(v); double u2 = PyFloat_AsDouble(w); PyObject *res=0; - if (op == Py_NE) { - res = (u1 != u2) ? Py_True : Py_False; + if (op == Py_NE) { + res = (u1 != u2) ? Py_True : Py_False; Py_INCREF(res); return res; - } - else if (op == Py_LT) { - res = (u1 < u2) ? Py_True : Py_False; + } + else if (op == Py_LT) { + res = (u1 < u2) ? Py_True : Py_False; Py_INCREF(res); return res; - } - else if (op == Py_LE) { - res = (u1 <= u2) ? Py_True : Py_False; + } + else if (op == Py_LE) { + res = (u1 <= u2) ? Py_True : Py_False; Py_INCREF(res); return res; - } - else if (op == Py_GT) { - res = (u1 > u2) ? Py_True : Py_False; + } + else if (op == Py_GT) { + res = (u1 > u2) ? Py_True : Py_False; Py_INCREF(res); return res; - } - else if (op == Py_GE) { - res = (u1 >= u2) ? Py_True : Py_False; + } + else if (op == Py_GE) { + res = (u1 >= u2) ? Py_True : Py_False; Py_INCREF(res); return res; - } - else if (op == Py_EQ) { - res = (u1 == u2) ? Py_True : Py_False; + } + else if (op == Py_EQ) { + res = (u1 == u2) ? Py_True : Py_False; Py_INCREF(res); return res; - } + } } // This always returns False