From 1106404b1e3902c323960dbcb3067d8a174a800e Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 27 Oct 2018 15:27:27 +0200 Subject: [PATCH] implement true divide handler of the number protocol for Quantity --- src/Base/MatrixPyImp.cpp | 2 -- src/Base/QuantityPyImp.cpp | 9 +++++++-- src/Base/UnitPyImp.cpp | 2 -- src/Base/VectorPyImp.cpp | 2 -- src/Tools/generateTemplates/templateClassPyExport.py | 6 ++++-- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Base/MatrixPyImp.cpp b/src/Base/MatrixPyImp.cpp index 60e92f0031..32c0b6540e 100644 --- a/src/Base/MatrixPyImp.cpp +++ b/src/Base/MatrixPyImp.cpp @@ -752,13 +752,11 @@ int MatrixPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) return 0; } -#if PY_MAJOR_VERSION < 3 PyObject * MatrixPy::number_divide_handler (PyObject* /*self*/, PyObject* /*other*/) { PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); return 0; } -#endif PyObject * MatrixPy::number_remainder_handler (PyObject* /*self*/, PyObject* /*other*/) { diff --git a/src/Base/QuantityPyImp.cpp b/src/Base/QuantityPyImp.cpp index b8962f7793..86729b7e49 100644 --- a/src/Base/QuantityPyImp.cpp +++ b/src/Base/QuantityPyImp.cpp @@ -370,7 +370,6 @@ PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other) return 0; } -#if PY_MAJOR_VERSION < 3 PyObject * QuantityPy::number_divide_handler (PyObject *self, PyObject *other) { if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) { @@ -389,17 +388,23 @@ PyObject * QuantityPy::number_divide_handler (PyObject *self, PyObject *other) double b = PyFloat_AsDouble(other); return new QuantityPy(new Quantity(*a / b) ); } +#if PY_MAJOR_VERSION < 3 else if (PyInt_Check(other)) { Base::Quantity *a = static_cast(self) ->getQuantityPtr(); double b = (double)PyInt_AsLong(other); return new QuantityPy(new Quantity(*a / b) ); } +#endif + else if (PyLong_Check(other)) { + Base::Quantity *a = static_cast(self) ->getQuantityPtr(); + double b = (double)PyLong_AsLong(other); + return new QuantityPy(new Quantity(*a / b) ); + } else { PyErr_SetString(PyExc_TypeError, "A Quantity can only be divided by Quantity or number"); return 0; } } -#endif PyObject * QuantityPy::number_remainder_handler (PyObject *self, PyObject *other) { diff --git a/src/Base/UnitPyImp.cpp b/src/Base/UnitPyImp.cpp index dfa7db15a6..4698d70889 100644 --- a/src/Base/UnitPyImp.cpp +++ b/src/Base/UnitPyImp.cpp @@ -194,13 +194,11 @@ int UnitPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) return 0; } -#if PY_MAJOR_VERSION < 3 PyObject * UnitPy::number_divide_handler (PyObject* /*self*/, PyObject* /*other*/) { PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); return 0; } -#endif PyObject * UnitPy::number_remainder_handler (PyObject* /*self*/, PyObject* /*other*/) { diff --git a/src/Base/VectorPyImp.cpp b/src/Base/VectorPyImp.cpp index ba7406d63d..d153f4e168 100644 --- a/src/Base/VectorPyImp.cpp +++ b/src/Base/VectorPyImp.cpp @@ -672,7 +672,6 @@ int VectorPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) // In generation script allow to more precisely define which slots // of the number protocol should be supported instead of setting all. -#if PY_MAJOR_VERSION < 3 PyObject * VectorPy::number_divide_handler (PyObject* self, PyObject* other) { if (PyObject_TypeCheck(self, &(VectorPy::Type)) && @@ -702,7 +701,6 @@ PyObject * VectorPy::number_divide_handler (PyObject* self, PyObject* other) Py_TYPE(self)->tp_name, Py_TYPE(other)->tp_name); return 0; } -#endif PyObject * VectorPy::number_remainder_handler (PyObject* self, PyObject* other) { diff --git a/src/Tools/generateTemplates/templateClassPyExport.py b/src/Tools/generateTemplates/templateClassPyExport.py index 6107a001ac..6e3244602a 100644 --- a/src/Tools/generateTemplates/templateClassPyExport.py +++ b/src/Tools/generateTemplates/templateClassPyExport.py @@ -116,10 +116,8 @@ public: static PyObject * number_subtract_handler (PyObject *self, PyObject *other); /// callback for the number_multiply_handler static PyObject * number_multiply_handler (PyObject *self, PyObject *other); -#if PY_MAJOR_VERSION < 3 /// callback for the number_divide_handler static PyObject * number_divide_handler (PyObject *self, PyObject *other); -#endif /// callback for the number_remainder_handler static PyObject * number_remainder_handler (PyObject *self, PyObject *other); /// callback for the number_divmod_handler @@ -415,7 +413,11 @@ PyNumberMethods @self.export.Name@::Number[] = { { NULL, /*nb_inplace_xor*/ NULL, /*nb_inplace_or*/ NULL, /*nb_floor_divide*/ +#if PY_MAJOR_VERSION < 3 NULL, /*nb_true_divide*/ +#else + number_divide_handler, /*nb_true_divide*/ +#endif NULL, /*nb_inplace_floor_divide*/ NULL, /*nb_inplace_true_divide*/ NULL /*nb_index*/