py3: Base: files I-R ported to python3

PyObjectBase.cpp and PyObjectBase.h not included
issue 0000995
This commit is contained in:
Yorik van Havre
2017-05-17 21:59:39 +02:00
committed by wmayer
parent ef22d5202d
commit 2becb8a0e8
3 changed files with 74 additions and 4 deletions

View File

@@ -221,9 +221,14 @@ PyObject * QuantityPy::number_int_handler (PyObject *self)
}
QuantityPy* q = static_cast<QuantityPy*>(self);
#if PY_MAJOR_VERSION < 3
return PyInt_FromLong((long)q->getValue());
#else
return PyLong_FromLong((long)q->getValue());
#endif
}
#if PY_MAJOR_VERSION < 3
PyObject * QuantityPy::number_long_handler (PyObject *self)
{
if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
@@ -234,6 +239,7 @@ PyObject * QuantityPy::number_long_handler (PyObject *self)
QuantityPy* q = static_cast<QuantityPy*>(self);
return PyInt_FromLong((long)q->getValue());
}
#endif
PyObject * QuantityPy::number_negative_handler (PyObject *self)
{
@@ -274,7 +280,7 @@ PyObject* QuantityPy::number_add_handler(PyObject *self, PyObject *other)
if (!PyObject_TypeCheck(self, &(QuantityPy::Type)) ||
!PyObject_TypeCheck(other, &(QuantityPy::Type))) {
std::stringstream ret;
ret << self->ob_type->tp_name << " and " << other->ob_type->tp_name
ret << Py_TYPE(self)->tp_name << " and " << Py_TYPE(other)->tp_name
<< " cannot be mixed in Quantity.__add__.\n"
<< "Make sure to use matching types.";
PyErr_SetString(PyExc_TypeError, ret.str().c_str());
@@ -297,7 +303,7 @@ PyObject* QuantityPy::number_subtract_handler(PyObject *self, PyObject *other)
if (!PyObject_TypeCheck(self, &(QuantityPy::Type)) ||
!PyObject_TypeCheck(other, &(QuantityPy::Type))) {
std::stringstream ret;
ret << self->ob_type->tp_name << " and " << other->ob_type->tp_name
ret << Py_TYPE(self)->tp_name << " and " << Py_TYPE(other)->tp_name
<< " cannot be mixed in Quantity.__sub__.\n"
<< "Make sure to use matching types.";
PyErr_SetString(PyExc_TypeError, ret.str().c_str());
@@ -328,9 +334,15 @@ PyObject* QuantityPy::number_multiply_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);
#else
else if (PyLong_Check(other)) {
Base::Quantity *a = static_cast<QuantityPy*>(self) ->getQuantityPtr();
double b = (double)PyLong_AsLong(other);
#endif
return new QuantityPy(new Quantity(*a*b) );
}
}
@@ -340,9 +352,15 @@ PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other)
double b = PyFloat_AsDouble(self);
return new QuantityPy(new Quantity(*a*b) );
}
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(self)) {
Base::Quantity *a = static_cast<QuantityPy*>(other) ->getQuantityPtr();
double b = (double)PyInt_AsLong(self);
#else
else if (PyLong_Check(self)) {
Base::Quantity *a = static_cast<QuantityPy*>(other) ->getQuantityPtr();
double b = (double)PyLong_AsLong(self);
#endif
return new QuantityPy(new Quantity(*a*b) );
}
}
@@ -351,6 +369,7 @@ 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))) {
@@ -379,6 +398,7 @@ PyObject * QuantityPy::number_divide_handler (PyObject *self, PyObject *other)
return 0;
}
}
#endif
PyObject * QuantityPy::number_remainder_handler (PyObject *self, PyObject *other)
{
@@ -398,8 +418,13 @@ PyObject * QuantityPy::number_remainder_handler (PyObject *self, PyObject *other
else if (PyFloat_Check(other)) {
d2 = PyFloat_AsDouble(other);
}
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(other)) {
d2 = (double)PyInt_AsLong(other);
#else
else if (PyLong_Check(other)) {
d2 = (double)PyLong_AsLong(other);
#endif
}
else {
PyErr_SetString(PyExc_TypeError, "Expected quantity or number");
@@ -443,9 +468,15 @@ PyObject * QuantityPy::number_power_handler (PyObject *self, PyObject *other, Py
double b = PyFloat_AsDouble(other);
return new QuantityPy(new Quantity(a->pow(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);
#else
else if (PyLong_Check(other)) {
Base::Quantity *a = static_cast<QuantityPy*>(self) ->getQuantityPtr();
double b = (double)PyLong_AsLong(other);
#endif
return new QuantityPy(new Quantity(a->pow(b)));
}
else {
@@ -621,6 +652,7 @@ PyObject * QuantityPy::number_or_handler (PyObject* /*self*/, PyObject* /*other*
return 0;
}
#if PY_MAJOR_VERSION < 3
int QuantityPy::number_coerce_handler (PyObject** /*self*/, PyObject** /*other*/)
{
return 1;
@@ -637,3 +669,4 @@ PyObject * QuantityPy::number_hex_handler (PyObject* /*self*/)
PyErr_SetString(PyExc_TypeError, "hex() argument can't be converted to hex");
return 0;
}
#endif