Skip to content

Commit

Permalink
AppManager: fix Python interpreter
Browse files Browse the repository at this point in the history
- GIL must be acquired before calling Py_Main
- Py_Main releases it for us
- Py_Main calls Py_Finalize for us
  • Loading branch information
devernay committed Jul 6, 2021
1 parent 66c12a0 commit e2557c8
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Engine/AppManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3411,6 +3411,10 @@ AppManager::tearDownPython()

return;
#endif
if ( !Py_IsInitialized() ) {
return;
}

///See https://web.archive.org/web/20150918224620/http://wiki.blender.org/index.php/Dev:2.4/Source/Python/API/Threads
#if !defined(NDEBUG)
QThread* curThread = QThread::currentThread();
Expand Down Expand Up @@ -3499,14 +3503,19 @@ AppManager::launchPythonInterpreter()
throw std::runtime_error("AppInstance::launchPythonInterpreter(): interpretPythonScript(" + s + " failed!");
}

PythonGILLocker pgl;
PyGILState_Ensure(); // Py_Main does PyGILState_Release() for us.

assert(PyThreadState_Get());
#if PY_VERSION_HEX >= 0x030400F0
assert(PyGILState_Check()); // Not available prior to Python 3.4
#endif

#if PY_MAJOR_VERSION >= 3
// Python 3
Py_Main(1, &_imp->commandLineArgsWide[0]);
#else
Py_Main(1, &_imp->commandLineArgsUtf8[0]);
#endif

}

int
Expand Down

0 comments on commit e2557c8

Please sign in to comment.