From 84dbf637c5c3ac4296181dd93de4fb5ffdc4b582 Mon Sep 17 00:00:00 2001 From: Kimball Thurston Date: Fri, 4 May 2018 02:53:03 +1200 Subject: [PATCH] initial port of PyIlmBase to python 3 --- PyIlmBase/PyIex/iexmodule.cpp | 5 ++++- PyIlmBase/PyImath/PyImathBox.cpp | 16 ++++++++++++++++ PyIlmBase/PyImath/PyImathFixedArray.h | 9 +++++++++ PyIlmBase/PyImath/PyImathFixedArray2D.h | 9 +++++++++ PyIlmBase/PyImath/PyImathFixedMatrix.h | 9 +++++++++ PyIlmBase/PyImath/PyImathFixedVArray.cpp | 10 ++++++++++ PyIlmBase/PyImath/PyImathLine.cpp | 8 ++++++++ PyIlmBase/PyImath/PyImathPlane.cpp | 12 ++++++++++++ PyIlmBase/PyImath/PyImathUtil.cpp | 4 ++++ PyIlmBase/PyImathNumpy/imathnumpymodule.cpp | 14 +++++++++++++- 10 files changed, 94 insertions(+), 2 deletions(-) diff --git a/PyIlmBase/PyIex/iexmodule.cpp b/PyIlmBase/PyIex/iexmodule.cpp index 63f2a29b94..a1bef52c2f 100644 --- a/PyIlmBase/PyIex/iexmodule.cpp +++ b/PyIlmBase/PyIex/iexmodule.cpp @@ -61,8 +61,11 @@ registerBaseExc() std::string name = "BaseExc"; std::string module = "iex"; std::string baseName = "RuntimeError"; +#if PY_MAJOR_VERSION > 2 + std::string baseModule = "builtins"; +#else std::string baseModule = "__builtin__"; - +#endif // if module != baseModule, the type object isn't used object exc_class = createExceptionProxy(name, module, baseName, baseModule, 0); scope().attr(name.c_str()) = exc_class; diff --git a/PyIlmBase/PyImath/PyImathBox.cpp b/PyIlmBase/PyImath/PyImathBox.cpp index 06b311ee9f..a9d55e5ea8 100644 --- a/PyIlmBase/PyImath/PyImathBox.cpp +++ b/PyIlmBase/PyImath/PyImathBox.cpp @@ -191,13 +191,21 @@ static std::string Box2_repr(const Box &box) PyObject *minObj = converter (box.min); PyObject *minReprObj = PyObject_Repr (minObj); +#if PY_MAJOR_VERSION > 2 + std::string minReprStr = PyUnicode_AsUTF8 (minReprObj); +#else std::string minReprStr = PyString_AsString (minReprObj); +#endif Py_DECREF (minReprObj); Py_DECREF (minObj); PyObject *maxObj = converter (box.max); PyObject *maxReprObj = PyObject_Repr (maxObj); +#if PY_MAJOR_VERSION > 2 + std::string maxReprStr = PyUnicode_AsUTF8 (maxReprObj); +#else std::string maxReprStr = PyString_AsString (maxReprObj); +#endif Py_DECREF (maxReprObj); Py_DECREF (maxObj); @@ -214,13 +222,21 @@ static std::string Box3_repr(const Box &box) PyObject *minObj = converter (box.min); PyObject *minReprObj = PyObject_Repr (minObj); +#if PY_MAJOR_VERSION > 2 + std::string minReprStr = PyUnicode_AsUTF8 (minReprObj); +#else std::string minReprStr = PyString_AsString (minReprObj); +#endif Py_DECREF (minReprObj); Py_DECREF (minObj); PyObject *maxObj = converter (box.max); PyObject *maxReprObj = PyObject_Repr (maxObj); +#if PY_MAJOR_VERSION > 2 + std::string maxReprStr = PyUnicode_AsUTF8 (maxReprObj); +#else std::string maxReprStr = PyString_AsString (maxReprObj); +#endif Py_DECREF (maxReprObj); Py_DECREF (maxObj); diff --git a/PyIlmBase/PyImath/PyImathFixedArray.h b/PyIlmBase/PyImath/PyImathFixedArray.h index ed03c64359..7a3d89d951 100644 --- a/PyIlmBase/PyImath/PyImathFixedArray.h +++ b/PyIlmBase/PyImath/PyImathFixedArray.h @@ -240,7 +240,11 @@ class FixedArray void extract_slice_indices(PyObject *index, size_t &start, size_t &end, Py_ssize_t &step, size_t &slicelength) const { if (PySlice_Check(index)) { +#if PY_MAJOR_VERSION > 2 + PyObject *slice = index; +#else PySliceObject *slice = reinterpret_cast(index); +#endif Py_ssize_t s,e,sl; if (PySlice_GetIndicesEx(slice,_length,&s,&e,&step,&sl) == -1) { boost::python::throw_error_already_set(); @@ -252,8 +256,13 @@ class FixedArray start = s; end = e; slicelength = sl; +#if PY_MAJOR_VERSION > 2 + } else if (PyLong_Check(index)) { + size_t i = canonical_index(PyLong_AsSsize_t(index)); +#else } else if (PyInt_Check(index)) { size_t i = canonical_index(PyInt_AsSsize_t(index)); +#endif start = i; end = i+1; step = 1; slicelength = 1; } else { PyErr_SetString(PyExc_TypeError, "Object is not a slice"); diff --git a/PyIlmBase/PyImath/PyImathFixedArray2D.h b/PyIlmBase/PyImath/PyImathFixedArray2D.h index 00af09191d..8c628f1695 100644 --- a/PyIlmBase/PyImath/PyImathFixedArray2D.h +++ b/PyIlmBase/PyImath/PyImathFixedArray2D.h @@ -193,7 +193,11 @@ class FixedArray2D void extract_slice_indices(PyObject *index, size_t length, size_t &start, size_t &end, Py_ssize_t &step, size_t &slicelength) const { if (PySlice_Check(index)) { +#if PY_MAJOR_VERSION > 2 + PyObject *slice = index; +#else PySliceObject *slice = reinterpret_cast(index); +#endif Py_ssize_t s, e, sl; if (PySlice_GetIndicesEx(slice,length,&s,&e,&step,&sl) == -1) { boost::python::throw_error_already_set(); @@ -204,8 +208,13 @@ class FixedArray2D start = s; end = e; slicelength = sl; +#if PY_MAJOR_VERSION > 2 + } else if (PyLong_Check(index)) { + size_t i = canonical_index(PyLong_AsSsize_t(index), length); +#else } else if (PyInt_Check(index)) { size_t i = canonical_index(PyInt_AsSsize_t(index), length); +#endif start = i; end = i+1; step = 1; slicelength = 1; } else { PyErr_SetString(PyExc_TypeError, "Object is not a slice"); diff --git a/PyIlmBase/PyImath/PyImathFixedMatrix.h b/PyIlmBase/PyImath/PyImathFixedMatrix.h index 0f556d163d..b8f9aae464 100644 --- a/PyIlmBase/PyImath/PyImathFixedMatrix.h +++ b/PyIlmBase/PyImath/PyImathFixedMatrix.h @@ -131,12 +131,21 @@ class FixedMatrix void extract_slice_indices(PyObject *index, Py_ssize_t &start, Py_ssize_t &end, Py_ssize_t &step, Py_ssize_t &slicelength) const { if (PySlice_Check(index)) { +#if PY_MAJOR_VERSION > 2 + PyObject *slice = index; +#else PySliceObject *slice = reinterpret_cast(index); +#endif if (PySlice_GetIndicesEx(slice,_rows,&start,&end,&step,&slicelength) == -1) { boost::python::throw_error_already_set(); } +#if PY_MAJOR_VERSION > 2 + } else if (PyLong_Check(index)) { + ssize_t i = convert_index(PyLong_AsSsize_t(index)); +#else } else if (PyInt_Check(index)) { int i = convert_index(PyInt_AS_LONG(index)); +#endif start = i; end = i+1; step = 1; slicelength = 1; } else { PyErr_SetString(PyExc_TypeError, "Object is not a slice"); diff --git a/PyIlmBase/PyImath/PyImathFixedVArray.cpp b/PyIlmBase/PyImath/PyImathFixedVArray.cpp index 9bb3341ecb..7d827d86ed 100644 --- a/PyIlmBase/PyImath/PyImathFixedVArray.cpp +++ b/PyIlmBase/PyImath/PyImathFixedVArray.cpp @@ -243,7 +243,11 @@ extract_slice_indices (PyObject* index, size_t& start, size_t& end, { if (PySlice_Check (index)) { +#if PY_MAJOR_VERSION > 2 + PyObject* slice = index; +#else PySliceObject* slice = reinterpret_cast(index); +#endif Py_ssize_t s, e, sl; if (PySlice_GetIndicesEx(slice, totalLength, &s, &e, &step, &sl) == -1) { @@ -259,9 +263,15 @@ extract_slice_indices (PyObject* index, size_t& start, size_t& end, end = e; sliceLength = sl; } +#if PY_MAJOR_VERSION > 2 + else if (PyLong_Check (index)) + { + size_t i = canonical_index (PyLong_AsSsize_t(index), totalLength); +#else else if (PyInt_Check (index)) { size_t i = canonical_index (PyInt_AsSsize_t(index), totalLength); +#endif start = i; end = i + 1; step = 1; diff --git a/PyIlmBase/PyImath/PyImathLine.cpp b/PyIlmBase/PyImath/PyImathLine.cpp index 69d038c15f..e7dfca50b8 100644 --- a/PyIlmBase/PyImath/PyImathLine.cpp +++ b/PyIlmBase/PyImath/PyImathLine.cpp @@ -427,13 +427,21 @@ static std::string Line3_repr(const Line3 &v) PyObject *v1Obj = V3::wrap (v1); PyObject *v1ReprObj = PyObject_Repr (v1Obj); +#if PY_MAJOR_VERSION > 2 + std::string v1ReprStr = PyUnicode_AsUTF8 (v1ReprObj); +#else std::string v1ReprStr = PyString_AsString (v1ReprObj); +#endif Py_DECREF (v1ReprObj); Py_DECREF (v1Obj); PyObject *v2Obj = V3::wrap (v2); PyObject *v2ReprObj = PyObject_Repr (v2Obj); +#if PY_MAJOR_VERSION > 2 + std::string v2ReprStr = PyUnicode_AsUTF8 (v2ReprObj); +#else std::string v2ReprStr = PyString_AsString (v2ReprObj); +#endif Py_DECREF (v2ReprObj); Py_DECREF (v2Obj); diff --git a/PyIlmBase/PyImath/PyImathPlane.cpp b/PyIlmBase/PyImath/PyImathPlane.cpp index e0280573cc..a31f24594e 100644 --- a/PyIlmBase/PyImath/PyImathPlane.cpp +++ b/PyIlmBase/PyImath/PyImathPlane.cpp @@ -195,7 +195,11 @@ static std::string Plane3_str(const Plane3 &plane) PyObject *normalObj = V3::wrap (plane.normal); PyObject *normalReprObj = PyObject_Repr (normalObj); +#if PY_MAJOR_VERSION > 2 + std::string normalReprStr = PyUnicode_AsUTF8 (normalReprObj); +#else std::string normalReprStr = PyString_AsString (normalReprObj); +#endif Py_DECREF (normalReprObj); Py_DECREF (normalObj); @@ -217,7 +221,11 @@ std::string Plane3_repr(const Plane3 &plane) { PyObject *normalObj = V3::wrap (plane.normal); PyObject *normalReprObj = PyObject_Repr (normalObj); +#if PY_MAJOR_VERSION > 2 + std::string normalReprStr = PyUnicode_AsUTF8 (normalReprObj); +#else std::string normalReprStr = PyString_AsString (normalReprObj); +#endif Py_DECREF (normalReprObj); Py_DECREF (normalObj); @@ -233,7 +241,11 @@ std::string Plane3_repr(const Plane3 &plane) { PyObject *normalObj = V3::wrap (plane.normal); PyObject *normalReprObj = PyObject_Repr (normalObj); +#if PY_MAJOR_VERSION > 2 + std::string normalReprStr = PyUnicode_AsUTF8 (normalReprObj); +#else std::string normalReprStr = PyString_AsString (normalReprObj); +#endif Py_DECREF (normalReprObj); Py_DECREF (normalObj); diff --git a/PyIlmBase/PyImath/PyImathUtil.cpp b/PyIlmBase/PyImath/PyImathUtil.cpp index 01ab8adbee..dc105173a5 100644 --- a/PyIlmBase/PyImath/PyImathUtil.cpp +++ b/PyIlmBase/PyImath/PyImathUtil.cpp @@ -58,6 +58,9 @@ extern "C" PyThreadState *_PyThreadState_Current; static bool pyHaveLock() { +#if PY_MAJOR_VERSION > 2 + return PyGILState_Check() != 0; +#else // This is very much dependent on the current Python // implementation of this functionality. If we switch versions of // Python and the implementation changes, we'll have to change @@ -72,6 +75,7 @@ pyHaveLock() // If the interpreter is initialized the gil is held if the // current thread's thread state is the current thread state return myThreadState != 0 && myThreadState == _PyThreadState_Current; +#endif } PyReleaseLock::PyReleaseLock() diff --git a/PyIlmBase/PyImathNumpy/imathnumpymodule.cpp b/PyIlmBase/PyImathNumpy/imathnumpymodule.cpp index 78f3b27a4e..431a2b709a 100644 --- a/PyIlmBase/PyImathNumpy/imathnumpymodule.cpp +++ b/PyIlmBase/PyImathNumpy/imathnumpymodule.cpp @@ -104,7 +104,13 @@ arrayToNumpy_int(IntArray &va) object retval = object(handle<>(a)); return retval; } - +#if PY_MAJOR_VERSION > 2 +static void *apply_import() +{ + import_array(); + return 0; +} +#endif BOOST_PYTHON_MODULE(imathnumpy) { handle<> imath(PyImport_ImportModule("imath")); @@ -115,7 +121,13 @@ BOOST_PYTHON_MODULE(imathnumpy) if (PyErr_Occurred()) throw_error_already_set(); scope().attr("numpy") = numpy; +#if PY_MAJOR_VERSION > 2 + // seems like numpy expects this to be used in a scenario + // where there is a return value in python3... + (void)apply_import(); +#else import_array(); +#endif scope().attr("__doc__") = "Array wrapping module to overlay imath array data with numpy arrays";