py3: Base: files R-Z ported to python3

issue 0000995
This commit is contained in:
Yorik van Havre
2017-05-18 19:31:32 +02:00
committed by wmayer
parent 769bc97f63
commit d4b7100bb2
5 changed files with 67 additions and 7 deletions

View File

@@ -142,9 +142,14 @@ QString UnitsApi::schemaTranslate(const Base::Quantity& quant, double &factor, Q
double UnitsApi::toDbl(PyObject *ArgObj, const Base::Unit &u)
{
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_Check(ArgObj)) {
QString str = QString::fromUtf8(PyUnicode_AsUTF8(ArgObj));
#else
if (PyString_Check(ArgObj)) {
// Parse the string
QString str = QString::fromLatin1(PyString_AsString(ArgObj));
#endif
// Parse the string
Quantity q = Quantity::parse(str);
if (q.getUnit() == u)
return q.getValue();
@@ -153,8 +158,13 @@ double UnitsApi::toDbl(PyObject *ArgObj, const Base::Unit &u)
else if (PyFloat_Check(ArgObj)) {
return PyFloat_AsDouble(ArgObj);
}
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(ArgObj)) {
return static_cast<double>(PyInt_AsLong(ArgObj));
#else
else if (PyLong_Check(ArgObj)) {
return static_cast<double>(PyLong_AsLong(ArgObj));
#endif
}
else {
throw Base::UnitsMismatchError("Wrong parameter type!");
@@ -164,17 +174,27 @@ double UnitsApi::toDbl(PyObject *ArgObj, const Base::Unit &u)
Quantity UnitsApi::toQuantity(PyObject *ArgObj, const Base::Unit &u)
{
double d;
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_Check(ArgObj)) {
QString str = QString::fromUtf8(PyUnicode_AsUTF8(ArgObj));
#else
if (PyString_Check(ArgObj)) {
// Parse the string
QString str = QString::fromLatin1(PyString_AsString(ArgObj));
#endif
// Parse the string
Quantity q = Quantity::parse(str);
d = q.getValue();
}
else if (PyFloat_Check(ArgObj)) {
d = PyFloat_AsDouble(ArgObj);
}
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(ArgObj)) {
d = static_cast<double>(PyInt_AsLong(ArgObj));
#else
else if (PyLong_Check(ArgObj)) {
d = static_cast<double>(PyLong_AsLong(ArgObj));
#endif
}
else {
throw Base::UnitsMismatchError("Wrong parameter type!");