Skip to content

Commit

Permalink
Fix obsolete python threads code
Browse files Browse the repository at this point in the history
  • Loading branch information
matevz committed Apr 27, 2020
1 parent d929778 commit a63451b
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 246 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ ENDIF("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
FIND_PACKAGE(SWIG 3.0.8 QUIET)
IF(SWIG_FOUND AND NOT NO_SWIG)
SET(USE_SWIG True)
cmake_policy(SET CMP0078 NEW)
cmake_policy(SET CMP0086 NEW)
MESSAGE("SWIG version ${SWIG_VERSION} found, using scripting extensions.")
ELSE(SWIG_FOUND AND NOT NO_SWIG)
MESSAGE("SWIG version >= 3.0.8 *not* found, version is ${SWIG_VERSION}, disabling SWIG!")
Expand Down Expand Up @@ -638,6 +640,7 @@ ENDIF(USE_RUBY)
IF(USE_PYTHON)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIRS})
SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/scripting/canoruspython.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/scripting/canoruspython.i PROPERTIES SWIG_FLAGS "-threads")

SET(CANORUS_PYTHON_WRAP_CXX ${CMAKE_CURRENT_BINARY_DIR}/canoruspythonPYTHON_wrap.cxx)
SET_SOURCE_FILES_PROPERTIES(${CANORUS_PYTHON_WRAP_CXX} PROPERTIES GENERATED TRUE)
Expand Down
23 changes: 20 additions & 3 deletions src/interface/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ bool CAPlugin::callAction(CAPluginAction* action, CAMainWin* mainWin, CADocument
#endif
#ifdef USE_PYTHON
if (action->lang() == "python") {
PyEval_RestoreThread(CASwigPython::mainThreadState);
pythonArgs << CASwigPython::toPythonObject(document, CASwigPython::Document);
PyEval_ReleaseThread(CASwigPython::mainThreadState);
}
#endif
} else
Expand All @@ -132,9 +134,11 @@ bool CAPlugin::callAction(CAPluginAction* action, CAMainWin* mainWin, CADocument
#endif
#ifdef USE_PYTHON
if (action->lang() == "python") {
if (mainWin->currentSheet())
if (mainWin->currentSheet()) {
PyEval_RestoreThread(CASwigPython::mainThreadState);
pythonArgs << CASwigPython::toPythonObject(mainWin->currentSheet(), CASwigPython::Sheet);
else {
PyEval_ReleaseThread(CASwigPython::mainThreadState);
} else {
error = true;
break;
}
Expand Down Expand Up @@ -170,7 +174,9 @@ bool CAPlugin::callAction(CAPluginAction* action, CAMainWin* mainWin, CADocument
error = true;
break;
}
PyEval_RestoreThread(CASwigPython::mainThreadState);
pythonArgs << CASwigPython::toPythonObject(v->selection().front()->musElement(), CASwigPython::MusElement);
PyEval_ReleaseThread(CASwigPython::mainThreadState);
} else {
error = true;
break;
Expand All @@ -190,9 +196,11 @@ bool CAPlugin::callAction(CAPluginAction* action, CAMainWin* mainWin, CADocument
if (mainWin->currentScoreView()) {
QList<CAMusElement*> musElements = mainWin->currentScoreView()->musElementSelection();
PyObject* list = PyList_New(0);
PyEval_RestoreThread(CASwigPython::mainThreadState);
for (int i = 0; i < musElements.size(); i++) {
PyList_Append(list, CASwigPython::toPythonObject(musElements[i], CASwigPython::MusElement));
}
PyEval_ReleaseThread(CASwigPython::mainThreadState);

pythonArgs << list;
} else {
Expand All @@ -213,7 +221,9 @@ bool CAPlugin::callAction(CAPluginAction* action, CAMainWin* mainWin, CADocument
#endif
#ifdef USE_PYTHON
if (action->lang() == "python") {
PyEval_RestoreThread(CASwigPython::mainThreadState);
pythonArgs << CASwigPython::toPythonObject(&_dirName, CASwigPython::String);
PyEval_ReleaseThread(CASwigPython::mainThreadState);
}
#endif
} else
Expand All @@ -227,15 +237,20 @@ bool CAPlugin::callAction(CAPluginAction* action, CAMainWin* mainWin, CADocument
#endif
#ifdef USE_PYTHON
if (action->lang() == "python") {
PyEval_RestoreThread(CASwigPython::mainThreadState);
pythonArgs << CASwigPython::toPythonObject(&filename, CASwigPython::String);
PyEval_ReleaseThread(CASwigPython::mainThreadState);
}
#endif
}
}
#ifdef USE_PYTHON
if (_name == "pyCLI") {
if (mainWin->pyConsoleIface)
if (mainWin->pyConsoleIface) {
PyEval_RestoreThread(CASwigPython::mainThreadState);
pythonArgs << CASwigPython::toPythonObject(mainWin->pyConsoleIface, CASwigPython::PyConsoleInterface);
PyEval_ReleaseThread(CASwigPython::mainThreadState);
}
}
#endif

Expand All @@ -248,7 +263,9 @@ bool CAPlugin::callAction(CAPluginAction* action, CAMainWin* mainWin, CADocument
#endif
#ifdef USE_PYTHON
if (action->lang() == "python") {
PyEval_RestoreThread(CASwigPython::mainThreadState);
PyRun_SimpleString((QString("sys.path.append('") + dirName() + "')").toStdString().c_str());
PyEval_ReleaseThread(CASwigPython::mainThreadState);
}
#endif
}
Expand Down
Loading

0 comments on commit a63451b

Please sign in to comment.