Skip to content

Commit

Permalink
Introduce PyCapsule
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamdi Sahloul committed Jul 30, 2021
1 parent 06f9ded commit 24e6b63
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set( CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE )

# Define here the needed parameters
set (OPENRAVE_VERSION_MAJOR 0)
set (OPENRAVE_VERSION_MINOR 83)
set (OPENRAVE_VERSION_PATCH 1)
set (OPENRAVE_VERSION_MINOR 84)
set (OPENRAVE_VERSION_PATCH 0)
set (OPENRAVE_VERSION ${OPENRAVE_VERSION_MAJOR}.${OPENRAVE_VERSION_MINOR}.${OPENRAVE_VERSION_PATCH})
set (OPENRAVE_SOVERSION ${OPENRAVE_VERSION_MAJOR}.${OPENRAVE_VERSION_MINOR})
message(STATUS "Compiling OpenRAVE Version ${OPENRAVE_VERSION}, soversion=${OPENRAVE_SOVERSION}")
Expand Down
58 changes: 57 additions & 1 deletion python/bindings/include/openravepy/openravepy_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ class OPENRAVEPY_API PythonThreadSaver
PyThreadState *_save;
};

typedef OPENRAVE_SHARED_PTR<PythonThreadSaver> PythonThreadSaverPtr;

/// \brief release and restore the python GIL... no thread state saved..?
class OPENRAVEPY_API PythonGILSaver
{
Expand All @@ -215,6 +217,7 @@ class OPENRAVEPY_API PythonGILSaver
}
};

// TODO: Remove AutoPyArrayObjectDereferencer in favor of PyCapsule
class OPENRAVEPY_API AutoPyArrayObjectDereferencer
{
public:
Expand All @@ -228,7 +231,60 @@ class OPENRAVEPY_API AutoPyArrayObjectDereferencer
PyArrayObject* _pyarrobj;
};

typedef OPENRAVE_SHARED_PTR<PythonThreadSaver> PythonThreadSaverPtr;
template<typename T>
class OPENRAVEPY_API PyCapsule
{
public:
// \brief disables copy constructor to avoid calling Py_DECREF more than once
PyCapsule(const PyCapsule<T>&) = delete;

// \brief the only entrypoint that allows a nullptr. Expects a `reset` call to follow
PyCapsule()
: _ptr(nullptr)
{
}

// \param ptr a not-null pointer
PyCapsule(T* ptr)
: _ptr(_PreventNullPtr(ptr))
{
}

// \param ptr a not-null pointer
void reset(T* ptr)
{
_ptr = _PreventNullPtr(ptr);
}

T* get() const
{
return _ptr;
}

operator T*() const
{
return _ptr;
}

~PyCapsule() {
if (_ptr != nullptr) {
Py_DECREF(_ptr);
}
}

private:
T* _ptr;

static T* _PreventNullPtr(T* ptr)
{
if (ptr == nullptr) {
throw OPENRAVE_EXCEPTION_FORMAT0(_("Invalid Numpy-array pointer. Failed to get contiguous array?"), ORE_InvalidArguments);
}
return ptr;
}
};

typedef PyCapsule<PyArrayObject> PyArrayCapsule;

inline RaveVector<float> ExtractFloat3(const py::object& o)
{
Expand Down
6 changes: 2 additions & 4 deletions python/bindings/openravepy_global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,7 @@ class PyTriMesh
if (!PyArray_ISFLOAT(pPyVertices)) {
throw openrave_exception(_("vertices must be in float"), ORE_InvalidArguments);
}
PyArrayObject* pPyVerticesContiguous = PyArray_GETCONTIGUOUS(reinterpret_cast<PyArrayObject*>(pPyVertices));
AutoPyArrayObjectDereferencer pydecref(pPyVerticesContiguous);
PyArrayCapsule pPyVerticesContiguous(PyArray_GETCONTIGUOUS(reinterpret_cast<PyArrayObject*>(pPyVertices)));

const size_t typeSize = PyArray_ITEMSIZE(pPyVerticesContiguous);
const size_t n = PyArray_DIM(pPyVerticesContiguous, 0);
Expand Down Expand Up @@ -393,8 +392,7 @@ class PyTriMesh
if (PyArray_NDIM(pPyIndices) != 2 || PyArray_DIM(pPyIndices, 1) != 3 || !PyArray_ISINTEGER(pPyIndices)) {
throw openrave_exception(_("indices must be a Nx3 int array"), ORE_InvalidArguments);
}
PyArrayObject* pPyIndiciesContiguous = PyArray_GETCONTIGUOUS(reinterpret_cast<PyArrayObject*>(pPyIndices));
AutoPyArrayObjectDereferencer pydecref(pPyIndiciesContiguous);
PyArrayCapsule pPyIndiciesContiguous(PyArray_GETCONTIGUOUS(reinterpret_cast<PyArrayObject*>(pPyIndices)));

const size_t typeSize = PyArray_ITEMSIZE(pPyIndiciesContiguous);
const bool signedInt = PyArray_ISSIGNED(pPyIndiciesContiguous);
Expand Down
6 changes: 2 additions & 4 deletions python/bindings/openravepy_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ void toRapidJSONValue(const object &obj, rapidjson::Value &value, rapidjson::Doc
#endif // USE_PYBIND11_PYTHON_BINDINGS
}
else if (PyArray_Check(obj.ptr()) ) {
PyArrayObject* pyarrayvalues = PyArray_GETCONTIGUOUS(reinterpret_cast<PyArrayObject*>(obj.ptr()));
AutoPyArrayObjectDereferencer pydecref(pyarrayvalues);
PyArrayCapsule pyarrayvalues(PyArray_GETCONTIGUOUS(reinterpret_cast<PyArrayObject*>(obj.ptr())));

int ndims = PyArray_NDIM(pyarrayvalues);
npy_intp* dims = PyArray_DIMS(pyarrayvalues);
Expand Down Expand Up @@ -1586,8 +1585,7 @@ object PyEnvironmentBase::CheckCollisionRays(py::numeric::array rays, PyKinBodyP
CollisionReport report;
CollisionReportPtr preport(&report,null_deleter());

PyArrayObject *pPyRays = PyArray_GETCONTIGUOUS(reinterpret_cast<PyArrayObject*>(rays.ptr()));
AutoPyArrayObjectDereferencer pyderef(pPyRays);
PyArrayCapsule pPyRays(PyArray_GETCONTIGUOUS(reinterpret_cast<PyArrayObject*>(rays.ptr())));

if( !PyArray_ISFLOAT(pPyRays) ) {
throw OpenRAVEException(_("rays has to be a float array\n"));
Expand Down

0 comments on commit 24e6b63

Please sign in to comment.