Skip to content

Commit

Permalink
Refs #4298. Add some missing GIL locks.
Browse files Browse the repository at this point in the history
Stops crashes when opening a script while another is running.
  • Loading branch information
martyngigg committed Dec 9, 2011
1 parent 5a55872 commit 62a30d1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Code/Mantid/MantidPlot/src/PythonScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ bool PythonScript::exec()
if( env()->isRunning() ) return false;

// Must acquire the GIL just in case other Python is running, i.e asynchronous Python algorithm
GILHolder gil();
GILHolder gil;

env()->setIsRunning(true);

Expand Down Expand Up @@ -356,6 +356,7 @@ void PythonScript::updatePath(const QString & filename, bool append)
" sys.path.remove(r'%1')";
}
pyCode = pyCode.arg(scriptPath);
GILHolder gil;
PyRun_SimpleString(pyCode.toAscii());
}

Expand Down Expand Up @@ -545,6 +546,7 @@ QString PythonScript::constructErrorMsg()

bool PythonScript::setQObject(QObject *val, const char *name)
{
GILHolder gil; // Aqcuire the GIL
if (localDict) // Avoid segfault for un-initialized object
{
if (!PyDict_Contains(localDict, PyString_FromString(name)))
Expand All @@ -557,13 +559,15 @@ bool PythonScript::setQObject(QObject *val, const char *name)

bool PythonScript::setInt(int val, const char *name)
{
GILHolder gil; // Aqcuire the GIL
if (!PyDict_Contains(localDict, PyString_FromString(name)))
compiled = notCompiled;
return env()->setInt(val, name, localDict);
}

bool PythonScript::setDouble(double val, const char *name)
{
GILHolder gil; // Aqcuire the GIL
if (!PyDict_Contains(localDict, PyString_FromString(name)))
compiled = notCompiled;
return env()->setDouble(val, name, localDict);
Expand Down Expand Up @@ -619,6 +623,7 @@ PythonScripting * PythonScript::env() const
*/
void PythonScript::beginStdoutRedirect()
{
GILHolder gil; // Aqcuire the GIL
stdoutSave = PyDict_GetItemString(env()->sysDict(), "stdout");
Py_XINCREF(stdoutSave);
stderrSave = PyDict_GetItemString(env()->sysDict(), "stderr");
Expand All @@ -633,6 +638,7 @@ void PythonScript::beginStdoutRedirect()
*/
void PythonScript::endStdoutRedirect()
{
GILHolder gil; // Aqcuire the GIL
PyDict_SetItemString(env()->sysDict(), "stdout", stdoutSave);
Py_XDECREF(stdoutSave);
PyDict_SetItemString(env()->sysDict(), "stderr", stderrSave);
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/MantidPlot/src/PythonScripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ bool PythonScripting::start()
*/
void PythonScripting::shutdown()
{
GILHolder gil();
GILHolder gil;
Py_XDECREF(m_math);
Py_XDECREF(m_locals);
Py_Finalize();
Expand Down

0 comments on commit 62a30d1

Please sign in to comment.