+ fixes #0001619: Python script generating 3D model works in V0.13, error in V0.14

This commit is contained in:
wmayer 2014-07-27 14:08:31 +02:00
parent d163316e63
commit a1da4bb4fb
2 changed files with 62 additions and 39 deletions

View File

@ -102,13 +102,13 @@ Quantity Quantity::pow(const Quantity &p) const
Quantity Quantity::operator +(const Quantity &p) const Quantity Quantity::operator +(const Quantity &p) const
{ {
if(this->_Unit != p._Unit) if(this->_Unit != p._Unit)
throw Base::Exception("Quantity::operator +(): Unit missmatch in plus operation"); throw Base::Exception("Quantity::operator +(): Unit mismatch in plus operation");
return Quantity(this->_Value + p._Value,this->_Unit); return Quantity(this->_Value + p._Value,this->_Unit);
} }
Quantity Quantity::operator -(const Quantity &p) const Quantity Quantity::operator -(const Quantity &p) const
{ {
if(this->_Unit != p._Unit) if(this->_Unit != p._Unit)
throw Base::Exception("Quantity::operator +(): Unit missmatch in plus operation"); throw Base::Exception("Quantity::operator +(): Unit mismatch in minus operation");
return Quantity(this->_Value - p._Value,this->_Unit); return Quantity(this->_Value - p._Value,this->_Unit);
} }

View File

@ -258,45 +258,56 @@ PyObject * QuantityPy::number_absolute_handler (PyObject *self)
PyObject* QuantityPy::number_add_handler(PyObject *self, PyObject *other) PyObject* QuantityPy::number_add_handler(PyObject *self, PyObject *other)
{ {
if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) { if (!PyObject_TypeCheck(self, &(QuantityPy::Type)) ||
PyErr_SetString(PyExc_TypeError, "First arg must be Quantity"); !PyObject_TypeCheck(other, &(QuantityPy::Type))) {
return 0; std::stringstream ret;
} ret << self->ob_type->tp_name << " and " << other->ob_type->tp_name
if (!PyObject_TypeCheck(other, &(QuantityPy::Type))) { << " cannot be mixed in Quantity.__add__.\n"
PyErr_SetString(PyExc_TypeError, "Second arg must be Quantity"); << "Make sure to use matching types.";
PyErr_SetString(PyExc_TypeError, ret.str().c_str());
return 0; return 0;
} }
try {
Base::Quantity *a = static_cast<QuantityPy*>(self)->getQuantityPtr(); Base::Quantity *a = static_cast<QuantityPy*>(self)->getQuantityPtr();
Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr(); Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr();
return new QuantityPy(new Quantity(*a+*b) ); return new QuantityPy(new Quantity(*a+*b) );
}
catch (const Base::Exception& e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
return 0;
}
} }
PyObject* QuantityPy::number_subtract_handler(PyObject *self, PyObject *other) PyObject* QuantityPy::number_subtract_handler(PyObject *self, PyObject *other)
{ {
if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) { if (!PyObject_TypeCheck(self, &(QuantityPy::Type)) ||
PyErr_SetString(PyExc_TypeError, "First arg must be Quantity"); !PyObject_TypeCheck(other, &(QuantityPy::Type))) {
return 0; std::stringstream ret;
} ret << self->ob_type->tp_name << " and " << other->ob_type->tp_name
if (!PyObject_TypeCheck(other, &(QuantityPy::Type))) { << " cannot be mixed in Quantity.__sub__.\n"
PyErr_SetString(PyExc_TypeError, "Second arg must be Quantity"); << "Make sure to use matching types.";
PyErr_SetString(PyExc_TypeError, ret.str().c_str());
return 0; return 0;
} }
try {
Base::Quantity *a = static_cast<QuantityPy*>(self)->getQuantityPtr(); Base::Quantity *a = static_cast<QuantityPy*>(self)->getQuantityPtr();
Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr(); Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr();
return new QuantityPy(new Quantity(*a-*b) ); return new QuantityPy(new Quantity(*a-*b) );
}
catch (const Base::Exception& e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
return 0;
}
} }
PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other) PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other)
{ {
if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) { if (PyObject_TypeCheck(self, &(QuantityPy::Type))) {
PyErr_SetString(PyExc_TypeError, "First arg must be Quantity");
return 0;
}
if (PyObject_TypeCheck(other, &(QuantityPy::Type))) { if (PyObject_TypeCheck(other, &(QuantityPy::Type))) {
Base::Quantity *a = static_cast<QuantityPy*>(self) ->getQuantityPtr(); Base::Quantity *a = static_cast<QuantityPy*>(self) ->getQuantityPtr();
Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr(); Base::Quantity *b = static_cast<QuantityPy*>(other)->getQuantityPtr();
return new QuantityPy(new Quantity(*a * *b) ); return new QuantityPy(new Quantity(*a * *b) );
} }
else if (PyFloat_Check(other)) { else if (PyFloat_Check(other)) {
@ -309,10 +320,22 @@ PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other)
double b = (double)PyInt_AsLong(other); double b = (double)PyInt_AsLong(other);
return new QuantityPy(new Quantity(*a*b) ); return new QuantityPy(new Quantity(*a*b) );
} }
else { }
else if (PyObject_TypeCheck(other, &(QuantityPy::Type))) {
if (PyFloat_Check(self)) {
Base::Quantity *a = static_cast<QuantityPy*>(other) ->getQuantityPtr();
double b = PyFloat_AsDouble(self);
return new QuantityPy(new Quantity(*a*b) );
}
else if (PyInt_Check(self)) {
Base::Quantity *a = static_cast<QuantityPy*>(other) ->getQuantityPtr();
double b = (double)PyInt_AsLong(self);
return new QuantityPy(new Quantity(*a*b) );
}
}
PyErr_SetString(PyExc_TypeError, "A Quantity can only be multiplied by Quantity or number"); PyErr_SetString(PyExc_TypeError, "A Quantity can only be multiplied by Quantity or number");
return 0; return 0;
}
} }
PyObject * QuantityPy::number_divide_handler (PyObject *self, PyObject *other) PyObject * QuantityPy::number_divide_handler (PyObject *self, PyObject *other)