@@ -1594,6 +1594,67 @@ extern void add_python_frontend_funcs(PyObject *module);
15941594
15951595static bool python_loaded_at_all = false;
15961596
1597+ #if RUNTIME_LINK || PY_VERSION_HEX < 0x03070000
1598+ static bool python_pre37_init_threads ()
1599+ {
1600+ PyEval_InitThreads ();
1601+ if (!PyEval_ThreadsInitialized ())
1602+ return false;
1603+
1604+ return true;
1605+ }
1606+ #endif
1607+
1608+ #if RUNTIME_LINK || PY_VERSION_HEX < 0x03080000
1609+ static bool python_pre38_init ()
1610+ {
1611+ Py_Initialize ();
1612+ if (!Py_IsInitialized ())
1613+ return false;
1614+
1615+ #if RUNTIME_LINK
1616+ if (python_version .minor < 7 && !python_pre37_init_threads ())
1617+ return false;
1618+ #elif PY_VERSION_HEX < 0x03070000
1619+ if (!python_pre37_init_threads ())
1620+ return false;
1621+ #endif
1622+
1623+ /* ---------------------------------------------- */
1624+ /* Must set arguments for guis to work */
1625+
1626+ wchar_t * argv [] = {L"" , NULL };
1627+ int argc = sizeof (argv ) / sizeof (wchar_t * ) - 1 ;
1628+
1629+ PySys_SetArgv (argc , argv );
1630+
1631+ return true;
1632+ }
1633+ #endif
1634+
1635+ #if RUNTIME_LINK || PY_VERSION_HEX > 0x03070000
1636+ static bool python_post37_init ()
1637+ {
1638+ PyConfig config ;
1639+ PyConfig_InitPythonConfig (& config );
1640+
1641+ config .parse_argv = 0 ;
1642+ config .safe_path = 1 ;
1643+
1644+ /* ---------------------------------------------- */
1645+ /* Must set arguments for guis to work */
1646+ PyWideStringList_Append (& config .argv , L"" );
1647+
1648+ PyStatus status = Py_InitializeFromConfig (& config );
1649+ PyConfig_Clear (& config );
1650+
1651+ if (PyStatus_Exception (status ))
1652+ return false;
1653+
1654+ return true;
1655+ }
1656+ #endif
1657+
15971658bool obs_scripting_load_python (const char * python_path )
15981659{
15991660 if (python_loaded )
@@ -1621,33 +1682,19 @@ bool obs_scripting_load_python(const char *python_path)
16211682 UNUSED_PARAMETER (python_path );
16221683#endif
16231684
1624- Py_Initialize ();
1625- if (!Py_IsInitialized ())
1626- return false;
1627-
16281685#if RUNTIME_LINK
1629- if (python_version .major == 3 && python_version .minor < 7 ) {
1630- PyEval_InitThreads ();
1631- if (!PyEval_ThreadsInitialized ())
1632- return false;
1633- }
1634- #elif PY_VERSION_HEX < 0x03070000
1635- PyEval_InitThreads ();
1636- if (!PyEval_ThreadsInitialized ())
1686+ if (python_version .minor < 8 && !python_pre38_init ())
1687+ return false;
1688+ if (python_version .minor > 7 && !python_post37_init ())
1689+ return false;
1690+ #elif PY_VERSION_HEX < 0x03080000
1691+ if (!python_pre38_init ())
1692+ return false;
1693+ #else
1694+ if (!python_post37_init ())
16371695 return false;
16381696#endif
16391697
1640- /* ---------------------------------------------- */
1641- /* Must set arguments for guis to work */
1642-
1643- wchar_t * argv [] = {L"" , NULL };
1644- int argc = sizeof (argv ) / sizeof (wchar_t * ) - 1 ;
1645-
1646- PRAGMA_WARN_PUSH
1647- PRAGMA_WARN_DEPRECATION
1648- PySys_SetArgv (argc , argv );
1649- PRAGMA_WARN_POP
1650-
16511698#ifdef __APPLE__
16521699 PyRun_SimpleString ("import sys" );
16531700 PyRun_SimpleString ("sys.dont_write_bytecode = True" );
0 commit comments