Skip to content
Snippets Groups Projects
Commit 9ce9b0b4 authored by Cédric Traizet's avatar Cédric Traizet
Browse files

BUG: reacquire the GIL before calling callbacks in PyCommand

parent 4d373203
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,27 @@
#include "itkPyCommand.h"
namespace
{
// Wrapper to automatics obtain and release GIL
// RAII idiom
class PyGILStateEnsure
{
public:
PyGILStateEnsure()
{
m_GIL = PyGILState_Ensure();
}
~PyGILStateEnsure()
{
PyGILState_Release(m_GIL);
}
private:
PyGILState_STATE m_GIL;
};
} // end anonymous namespace
namespace itk
{
......@@ -33,6 +54,7 @@ PyCommand::~PyCommand()
{
if (this->obj)
{
PyGILStateEnsure gil;
Py_DECREF(this->obj);
}
this->obj = nullptr;
......@@ -42,6 +64,7 @@ void PyCommand::SetCommandCallable(PyObject* theObj)
{
if (theObj != this->obj)
{
PyGILStateEnsure gil;
if (this->obj)
{
// get rid of our reference
......@@ -89,6 +112,7 @@ void PyCommand::PyExecute()
}
else
{
PyGILStateEnsure gil;
PyObject* result;
result = PyEval_CallObject(this->obj, (PyObject*)nullptr);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment