From 25defd5699bc3f6405930bf42526919c98450f81 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 14 Jul 2014 17:17:41 +0200 Subject: [PATCH] + support comparison with double in quantity number protocol --- src/App/Application.cpp | 2 ++ src/Base/QuantityPyImp.cpp | 40 +++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 5e430e3fd7..76a8b88467 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -237,6 +237,8 @@ Application::Application(ParameterManager * /*pcSysParamMngr*/, PyObject* pUnitsModule = Py_InitModule3("Units", Base::UnitsApi::Methods, "The Unit API"); Base::Interpreter().addType(&Base::QuantityPy ::Type,pUnitsModule,"Quantity"); + // make sure to set the 'nb_true_divide' slot + Base::QuantityPy::Type.tp_as_number->nb_true_divide = Base::QuantityPy::Type.tp_as_number->nb_divide; Base::Interpreter().addType(&Base::UnitPy ::Type,pUnitsModule,"Unit"); Py_INCREF(pUnitsModule); diff --git a/src/Base/QuantityPyImp.cpp b/src/Base/QuantityPyImp.cpp index 6a0a667e6f..3f83b9a21c 100644 --- a/src/Base/QuantityPyImp.cpp +++ b/src/Base/QuantityPyImp.cpp @@ -466,13 +466,47 @@ PyObject* QuantityPy::richCompare(PyObject *v, PyObject *w, int op) 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; + Py_INCREF(res); + return res; + } + 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; + Py_INCREF(res); + return res; + } + 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; + Py_INCREF(res); + return res; + } + else if (op == Py_EQ) { + res = (u1 == u2) ? Py_True : Py_False; + Py_INCREF(res); + return res; + } + } + // This always returns False Py_INCREF(Py_NotImplemented); return Py_NotImplemented; - } Py::Float QuantityPy::getValue(void) const