obs-scripting: Fix issues between runtime and compile-time versions

Calling `PyEval_InitThreads` has been deprecated in Python 3.7 and the
function itself will be removed in Python 3.11. The current check
guards this function behind a version check that only happens at compile
time.

This in turn leads to crashes when run on Python 3.6, as the necessary
initialization for `PyEval_ReleaseThread` did not take place.

This commit ensures the manual initialization takes place based on the
runtime version of Python and avoids loading the associated symbols
on Python 3.9 or later.
This commit is contained in:
PatTheMav 2022-08-02 17:31:33 +02:00 • committed by Ryan Foster
commit 24a123119a
3 changed files with 45 additions and 50 deletions

View file

@ -49,6 +49,7 @@ sys.stderr = stderr_logger()\n";
#if RUNTIME_LINK
static wchar_t home_path[1024] = {0};
static python_version_t python_version = {0};
#endif
DARRAY(char *) python_paths;
@ -1615,12 +1616,7 @@ bool obs_scripting_load_python(const char *python_path)
/* Use external python on windows and mac */
#if RUNTIME_LINK
#if 0
struct dstr old_path = {0};
struct dstr new_path = {0};
#endif
if (!import_python(python_path))
if (!import_python(python_path, &python_version))
return false;
if (python_path && *python_path) {
@ -1634,11 +1630,6 @@ bool obs_scripting_load_python(const char *python_path)
os_utf8_to_wcs(python_path, 0, home_path, 1024);
Py_SetPythonHome(home_path);
#endif
#if 0
dstr_copy(&old_path, getenv("PATH"));
_putenv("PYTHONPATH=");
_putenv("PATH=");
#endif
}
#else
@ -1649,24 +1640,17 @@ bool obs_scripting_load_python(const char *python_path)
if (!Py_IsInitialized())
return false;
#if 0
#ifdef _DEBUG
if (pythondir && *pythondir) {
dstr_printf(&new_path, "PATH=%s", old_path.array);
_putenv(new_path.array);
#if RUNTIME_LINK
if (python_version.major == 3 && python_version.minor < 7) {
#elif PY_VERSION_HEX < 0x030700b0
if (true) {
#else
if (false) {
#endif
PyEval_InitThreads();
if (!PyEval_ThreadsInitialized())
return false;
}
#endif
bfree(pythondir);
dstr_free(&new_path);
dstr_free(&old_path);
#endif
#if PY_VERSION_HEX < 0x03070000
PyEval_InitThreads();
if (!PyEval_ThreadsInitialized())
return false;
#endif
/* ---------------------------------------------- */
/* Must set arguments for guis to work */