Skip to content

Commit 4546130

Browse files
committed
guard boost::python::object
1 parent 5285567 commit 4546130

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/Core/Python/PythonDatatypeConverter.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct PythonObjectVisitor : boost::static_visitor<py::object>
7777
struct ValueVisitor : boost::static_visitor<Variable::Value>
7878
{
7979
private:
80-
const py::object& object_;
80+
py::object object_;
8181
public:
8282
explicit ValueVisitor(const py::object& object) : object_(object) {}
8383

@@ -132,10 +132,12 @@ struct ValueVisitor : boost::static_visitor<Variable::Value>
132132
const auto firstVal = v[0];
133133
const py::extract<py::list> e(object_);
134134
auto pyList = e();
135+
auto gil = PyGILState_Ensure();
135136
Variable::List newList(py::len(pyList));
136137
for (auto i = 0; i < py::len(pyList); ++i)
137138
newList[i] = Variable(firstVal.name(),
138139
boost::apply_visitor(ValueVisitor(pyList[i]), firstVal.value()));
140+
PyGILState_Release(gil);
139141
return newList;
140142
}
141143
else
@@ -626,7 +628,10 @@ Variable SCIRun::Core::Python::convertPythonObjectToVariable(const py::object& o
626628
Variable SCIRun::Core::Python::convertPythonObjectToVariableWithTypeInference(
627629
const py::object& object, const Variable& var)
628630
{
629-
return Variable(var.name(), boost::apply_visitor(ValueVisitor(object), var.value()));
631+
auto gil = PyGILState_Ensure();
632+
Variable result(var.name(), boost::apply_visitor(ValueVisitor(object), var.value()));
633+
PyGILState_Release(gil);
634+
return result;
630635
}
631636

632637
py::object SCIRun::Core::Python::convertVariableToPythonObject(const Variable& var)

src/Core/Python/PythonDatatypeConverter.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,23 @@ namespace SCIRun
105105
class SCISHARE DatatypePythonExtractor
106106
{
107107
public:
108-
virtual ~DatatypePythonExtractor() {}
108+
virtual ~DatatypePythonExtractor()
109+
{
110+
PyGILState_STATE gil = PyGILState_Ensure();
111+
try
112+
{
113+
object_ = boost::python::object(); // release
114+
}
115+
catch (...)
116+
{}
117+
PyGILState_Release(gil);
118+
}
109119
explicit DatatypePythonExtractor(const boost::python::object& object) : object_(object) {}
110120
virtual bool check() const = 0;
111121
virtual Datatypes::DatatypeHandle operator()() const = 0;
112122
virtual std::string label() const = 0;
113123
protected:
114-
const boost::python::object& object_;
124+
boost::python::object object_;
115125
};
116126

117127
class SCISHARE DenseMatrixExtractor : public DatatypePythonExtractor

src/Core/Python/PythonInterpreter.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ PythonInterpreter::PythonInterpreter() : private_(new PythonInterpreterPrivate)
232232
PythonInterpreter::~PythonInterpreter()
233233
{
234234
// NOTE: Boost.Python requires that we don't call Py_Finalize
235-
// Py_Finalize();
235+
Py_Finalize();
236236
}
237237

238238
// #define PRINT_PY_INIT_DEBUG(n) std::cout << "ev " << (n) << std::endl;

src/Dataflow/Engine/Controller/PythonImpl.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ namespace
7070
class PyDatatypeString : public PyDatatype
7171
{
7272
public:
73+
~PyDatatypeString()
74+
{
75+
if (Py_IsInitialized())
76+
{
77+
PyGILState_STATE gil = PyGILState_Ensure();
78+
str_ = py::object(); // release under GIL
79+
PyGILState_Release(gil);
80+
}
81+
}
7382
explicit PyDatatypeString(StringHandle underlying) : underlying_(underlying), str_(convertStringToPython(underlying))
7483
{
7584
}

0 commit comments

Comments
 (0)