Skip to content

Commit

Permalink
py3: Base: files R-Z ported to python3
Browse files Browse the repository at this point in the history
issue 0000995
  • Loading branch information
yorikvanhavre authored and wwmayer committed May 21, 2017
1 parent 1ec3e5d commit 0e9e49c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
15 changes: 12 additions & 3 deletions src/Base/Tools.cpp
Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Base/UnitPyImp.cpp
Expand Up @@ -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*/)
{
Expand Down Expand Up @@ -277,29 +279,34 @@ 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*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
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*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

#if PY_MAJOR_VERSION < 3
PyObject * UnitPy::number_oct_handler (PyObject* /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
Expand All @@ -311,3 +318,4 @@ PyObject * UnitPy::number_hex_handler (PyObject* /*self*/)
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}
#endif
24 changes: 22 additions & 2 deletions src/Base/UnitsApi.cpp
Expand Up @@ -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();
Expand All @@ -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!");
Expand All @@ -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!");
Expand Down
23 changes: 21 additions & 2 deletions src/Base/VectorPyImp.cpp
Expand Up @@ -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<VectorPy*>(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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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*/)
{
Expand Down Expand Up @@ -663,29 +676,34 @@ 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*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
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*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

#if PY_MAJOR_VERSION < 3
PyObject * VectorPy::number_oct_handler (PyObject* /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
Expand All @@ -697,3 +715,4 @@ PyObject * VectorPy::number_hex_handler (PyObject* /*self*/)
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}
#endif
4 changes: 4 additions & 0 deletions src/Base/swigpyrun.inl
Expand Up @@ -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);
Expand Down

0 comments on commit 0e9e49c

Please sign in to comment.