Skip to content

Commit

Permalink
initial port of PyIlmBase to python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
kdt3rd authored and nickrasmussen committed Aug 8, 2018
1 parent 00df2c7 commit 84dbf63
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 2 deletions.
5 changes: 4 additions & 1 deletion PyIlmBase/PyIex/iexmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions PyIlmBase/PyImath/PyImathBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,21 @@ static std::string Box2_repr(const Box<T> &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);

Expand All @@ -214,13 +222,21 @@ static std::string Box3_repr(const Box<T> &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);

Expand Down
9 changes: 9 additions & 0 deletions PyIlmBase/PyImath/PyImathFixedArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<PySliceObject *>(index);
#endif
Py_ssize_t s,e,sl;
if (PySlice_GetIndicesEx(slice,_length,&s,&e,&step,&sl) == -1) {
boost::python::throw_error_already_set();
Expand All @@ -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");
Expand Down
9 changes: 9 additions & 0 deletions PyIlmBase/PyImath/PyImathFixedArray2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<PySliceObject *>(index);
#endif
Py_ssize_t s, e, sl;
if (PySlice_GetIndicesEx(slice,length,&s,&e,&step,&sl) == -1) {
boost::python::throw_error_already_set();
Expand All @@ -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");
Expand Down
9 changes: 9 additions & 0 deletions PyIlmBase/PyImath/PyImathFixedMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<PySliceObject *>(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");
Expand Down
10 changes: 10 additions & 0 deletions PyIlmBase/PyImath/PyImathFixedVArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PySliceObject *>(index);
#endif
Py_ssize_t s, e, sl;
if (PySlice_GetIndicesEx(slice, totalLength, &s, &e, &step, &sl) == -1)
{
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions PyIlmBase/PyImath/PyImathLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,13 +427,21 @@ static std::string Line3_repr(const Line3<T> &v)

PyObject *v1Obj = V3<T>::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<T>::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);

Expand Down
12 changes: 12 additions & 0 deletions PyIlmBase/PyImath/PyImathPlane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ static std::string Plane3_str(const Plane3<T> &plane)

PyObject *normalObj = V3<T>::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);

Expand All @@ -217,7 +221,11 @@ std::string Plane3_repr(const Plane3<float> &plane)
{
PyObject *normalObj = V3<float>::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);

Expand All @@ -233,7 +241,11 @@ std::string Plane3_repr(const Plane3<double> &plane)
{
PyObject *normalObj = V3<double>::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);

Expand Down
4 changes: 4 additions & 0 deletions PyIlmBase/PyImath/PyImathUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
14 changes: 13 additions & 1 deletion PyIlmBase/PyImathNumpy/imathnumpymodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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";

Expand Down

0 comments on commit 84dbf63

Please sign in to comment.