implement true divide handler of the number protocol for Quantity

This commit is contained in:
wmayer
2018-10-27 15:27:27 +02:00
parent fb1b60a825
commit 1106404b1e
5 changed files with 11 additions and 10 deletions

View File

@@ -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*/)
{

View File

@@ -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<QuantityPy*>(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<QuantityPy*>(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)
{

View File

@@ -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*/)
{

View File

@@ -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)
{

View File

@@ -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*/