diff --git a/src/Base/Tools.cpp b/src/Base/Tools.cpp index 8957f0c126..edbf8416d0 100644 --- a/src/Base/Tools.cpp +++ b/src/Base/Tools.cpp @@ -153,12 +153,17 @@ std::string Base::Tools::escapedUnicodeFromUtf8(const char *s) return escapedstr; PyObject* escaped = PyUnicode_AsUnicodeEscapeString(unicode); + if (escaped) { +#if PY_MAJOR_VERSION >= 3 + escapedstr = std::string(PyBytes_AsString(escaped)); +#else escapedstr = std::string(PyString_AsString(escaped)); +#endif Py_DECREF(escaped); } - Py_DECREF(unicode); + return escapedstr; } @@ -170,7 +175,11 @@ std::string Base::Tools::escapedUnicodeToUtf8(const std::string& s) PyObject* unicode = PyUnicode_DecodeUnicodeEscape(s.c_str(), s.size(), "strict"); if (!unicode) return string; - +#if PY_MAJOR_VERSION >= 3 + if (PyUnicode_Check(unicode)) { + string = PyUnicode_AsUTF8(unicode); + } +#else if (PyUnicode_Check(unicode)) { PyObject* value = PyUnicode_AsUTF8String(unicode); string = PyString_AsString(value); @@ -179,7 +188,7 @@ std::string Base::Tools::escapedUnicodeToUtf8(const std::string& s) else if (PyString_Check(unicode)) { string = PyString_AsString(unicode); } - +#endif Py_DECREF(unicode); return string; } diff --git a/src/Base/UnitPyImp.cpp b/src/Base/UnitPyImp.cpp index cdedd53c1f..33370d6b9d 100644 --- a/src/Base/UnitPyImp.cpp +++ b/src/Base/UnitPyImp.cpp @@ -194,11 +194,13 @@ 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*/) { @@ -277,10 +279,12 @@ PyObject * UnitPy::number_or_handler (PyObject* /*self*/, PyObject* /*other*/) return 0; } +#if PY_MAJOR_VERSION < 3 int UnitPy::number_coerce_handler (PyObject** /*self*/, PyObject** /*other*/) { return 1; } +#endif PyObject * UnitPy::number_int_handler (PyObject* /*self*/) { @@ -288,11 +292,13 @@ PyObject * UnitPy::number_int_handler (PyObject* /*self*/) return 0; } +#if PY_MAJOR_VERSION < 3 PyObject * UnitPy::number_long_handler (PyObject* /*self*/) { PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); return 0; } +#endif PyObject * UnitPy::number_float_handler (PyObject* /*self*/) { @@ -300,6 +306,7 @@ PyObject * UnitPy::number_float_handler (PyObject* /*self*/) return 0; } +#if PY_MAJOR_VERSION < 3 PyObject * UnitPy::number_oct_handler (PyObject* /*self*/) { PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); @@ -311,3 +318,4 @@ PyObject * UnitPy::number_hex_handler (PyObject* /*self*/) PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); return 0; } +#endif diff --git a/src/Base/UnitsApi.cpp b/src/Base/UnitsApi.cpp index cb7c83bfb4..3e4cd3f7c9 100644 --- a/src/Base/UnitsApi.cpp +++ b/src/Base/UnitsApi.cpp @@ -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(PyInt_AsLong(ArgObj)); +#else + else if (PyLong_Check(ArgObj)) { + return static_cast(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(PyInt_AsLong(ArgObj)); +#else + else if (PyLong_Check(ArgObj)) { + d = static_cast(PyLong_AsLong(ArgObj)); +#endif } else { throw Base::UnitsMismatchError("Wrong parameter type!"); diff --git a/src/Base/VectorPyImp.cpp b/src/Base/VectorPyImp.cpp index 0b9bcc34ef..b1b3bf4f2b 100644 --- a/src/Base/VectorPyImp.cpp +++ b/src/Base/VectorPyImp.cpp @@ -131,8 +131,14 @@ PyObject* VectorPy::number_multiply_handler(PyObject *self, PyObject *other) double b = PyFloat_AsDouble(other); return new VectorPy(a * b); } - else if (PyInt_Check(other)) { - long b = PyInt_AsLong(other); +#if PY_MAJOR_VERSION < 3 + else if (PyInt_Check(other)) { + Base::Vector3d a = static_cast(self) ->value(); + long b = PyInt_AsLong(other); +#else + else if (PyLong_Check(other)) { + long b = PyLong_AsLong(other); +#endif return new VectorPy(a * (double)b); } else { @@ -146,8 +152,13 @@ PyObject* VectorPy::number_multiply_handler(PyObject *self, PyObject *other) double b = PyFloat_AsDouble(self); return new VectorPy(a * b); } +#if PY_MAJOR_VERSION >= 3 + else if (PyLong_Check(self)) { + long b = PyLong_AsLong(self); +#else else if (PyInt_Check(self)) { long b = PyInt_AsLong(self); +#endif return new VectorPy(a * (double)b); } else { @@ -580,11 +591,13 @@ int VectorPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) return 0; } +#if PY_MAJOR_VERSION < 3 PyObject * VectorPy::number_divide_handler (PyObject* /*self*/, PyObject* /*other*/) { PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); return 0; } +#endif PyObject * VectorPy::number_remainder_handler (PyObject* /*self*/, PyObject* /*other*/) { @@ -663,10 +676,12 @@ PyObject * VectorPy::number_or_handler (PyObject* /*self*/, PyObject* /*other*/) return 0; } +#if PY_MAJOR_VERSION < 3 int VectorPy::number_coerce_handler (PyObject ** /*self*/, PyObject ** /*other*/) { return 1; } +#endif PyObject * VectorPy::number_int_handler (PyObject* /*self*/) { @@ -674,11 +689,13 @@ PyObject * VectorPy::number_int_handler (PyObject* /*self*/) return 0; } +#if PY_MAJOR_VERSION < 3 PyObject * VectorPy::number_long_handler (PyObject* /*self*/) { PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); return 0; } +#endif PyObject * VectorPy::number_float_handler (PyObject* /*self*/) { @@ -686,6 +703,7 @@ PyObject * VectorPy::number_float_handler (PyObject* /*self*/) return 0; } +#if PY_MAJOR_VERSION < 3 PyObject * VectorPy::number_oct_handler (PyObject* /*self*/) { PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); @@ -697,3 +715,4 @@ PyObject * VectorPy::number_hex_handler (PyObject* /*self*/) PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); return 0; } +#endif diff --git a/src/Base/swigpyrun.inl b/src/Base/swigpyrun.inl index f71e7a6e6f..0eedaaa87a 100644 --- a/src/Base/swigpyrun.inl +++ b/src/Base/swigpyrun.inl @@ -88,7 +88,11 @@ void cleanupSWIG_T(const char* TypeName) PyObject *key, *value; pos = 0; while (PyDict_Next(dict, &pos, &key, &value)) { +#if PY_MAJOR_VERSION >= 3 + if (value != Py_None && PyUnicode_Check(key)) { +#else if (value != Py_None && PyString_Check(key)) { +#endif void* ptr = 0; if (SWIG_ConvertPtr(value, &ptr, 0, 0) == 0) PyDict_SetItem(dict, key, Py_None);