From 3ce05cd4ff3d0ee8c9c038ac410cfea38375f747 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 14 Feb 2014 17:14:39 +0100 Subject: [PATCH] + fixes #0001410: Base::Quantity::getValueAs does not properly check the dimension --- src/Base/QuantityPy.xml | 2 +- src/Base/QuantityPyImp.cpp | 91 +++++++++++++++++++++++++------------- 2 files changed, 62 insertions(+), 31 deletions(-) diff --git a/src/Base/QuantityPy.xml b/src/Base/QuantityPy.xml index 73099d02534a..d68ff8d0e598 100644 --- a/src/Base/QuantityPy.xml +++ b/src/Base/QuantityPy.xml @@ -41,7 +41,7 @@ Quantity(string) -- arbitrary mixture of numbers and chars defining a Quantity Following parameters are allowed: getValueAs('m/s') # unit string to parse - getValueAs(2.45,1) # translatrion value and unit signatur + getValueAs(2.45,1) # translatrion value and unit signature getValueAs(FreeCAD.Units.Pascal) # predefined standard units getValueAs(Qantity('N/m^2')) # a quantity getValueAs(Unit(0,1,0,0,0,0,0,0)) # a unit diff --git a/src/Base/QuantityPyImp.cpp b/src/Base/QuantityPyImp.cpp index ee0d92f54bf0..93a78847e805 100644 --- a/src/Base/QuantityPyImp.cpp +++ b/src/Base/QuantityPyImp.cpp @@ -117,45 +117,76 @@ PyObject* QuantityPy::getUserPreferred(PyObject *args) PyObject* QuantityPy::getValueAs(PyObject *args) { Quantity quant; + quant.setInvalid(); - double f = DOUBLE_MAX; - int i1=0; - int i2=0; - int i3=0; - int i4=0; - int i5=0; - int i6=0; - int i7=0; - int i8=0; - if (PyArg_ParseTuple(args, "d|iiiiiiii", &f,&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8)) { - if(f!=DOUBLE_MAX) - quant = Quantity(f,Unit(i1,i2,i3,i4,i5,i6,i7,i8)); - - }else{ - PyErr_Clear(); // set by PyArg_ParseTuple() - + // first try Quantity + if (!quant.isValid()) { PyObject *object; - if (PyArg_ParseTuple(args,"O!",&(Base::QuantityPy::Type), &object)) { // Note: must be static_cast, not reinterpret_cast quant = * static_cast(object)->getQuantityPtr(); - - }else{ - PyErr_Clear(); // set by PyArg_ParseTuple() - const char* string; - if (PyArg_ParseTuple(args,"s", &string)) { - - quant = Quantity::parse(QString::fromLatin1(string)); - - }else{ - PyErr_SetString(PyExc_TypeError, "Either three floats, tuple or Vector expected"); - return 0; + } + } + + if (!quant.isValid()) { + PyObject *object; + PyErr_Clear(); + if (PyArg_ParseTuple(args,"O!",&(Base::UnitPy::Type), &object)) { + // Note: must be static_cast, not reinterpret_cast + quant.setUnit(*static_cast(object)->getUnitPtr()); + quant.setValue(1.0); + } + } + + if (!quant.isValid()) { + PyObject *object; + double value; + PyErr_Clear(); + if (PyArg_ParseTuple(args,"dO!",&value, &(Base::UnitPy::Type), &object)) { + // Note: must be static_cast, not reinterpret_cast + quant.setUnit(*static_cast(object)->getUnitPtr()); + quant.setValue(value); + } + } + + if (!quant.isValid()) { + double f = DOUBLE_MAX; + int i1=0; + int i2=0; + int i3=0; + int i4=0; + int i5=0; + int i6=0; + int i7=0; + int i8=0; + PyErr_Clear(); + if (PyArg_ParseTuple(args, "d|iiiiiiii", &f,&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8)) { + if (f!=DOUBLE_MAX) { + quant = Quantity(f,Unit(i1,i2,i3,i4,i5,i6,i7,i8)); } } } - quant = getQuantityPtr()->getValueAs(quant); - return new QuantityPy(new Quantity(quant) ); + if (!quant.isValid()) { + PyErr_Clear(); + const char* string; + if (PyArg_ParseTuple(args,"s", &string)) { + quant = Quantity::parse(QString::fromLatin1(string)); + } + } + + if (!quant.isValid()) { + PyErr_SetString(PyExc_TypeError, "Either quantity, string, float or unit expected"); + return 0; + } + + if (getQuantityPtr()->getUnit() != quant.getUnit() && quant.isQuantity()) { + PyErr_SetString(PyExc_ValueError, "Unit mismatch"); + return 0; + } + + quant = getQuantityPtr()->getValueAs(quant); + return new QuantityPy(new Quantity(quant)); } PyObject * QuantityPy::number_float_handler (PyObject *self)