Skip to content

Commit 0bb21cb

Browse files
committed
obs-scripting: Replace PySys_SetArgv with recent Python method
1 parent 84b8d16 commit 0bb21cb

3 files changed

Lines changed: 91 additions & 23 deletions

File tree

shared/obs-scripting/obs-scripting-python-import.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ bool import_python(const char *python_path, python_version_t *python_version)
197197
#if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
198198
IMPORT_FUNC(_Py_Dealloc);
199199
#endif
200+
#if PY_VERSION_HEX >= 0x03080000
201+
if (python_version->major == 3 && python_version->minor >= 8) {
202+
IMPORT_FUNC(PyWideStringList_Append);
203+
IMPORT_FUNC(PyConfig_InitPythonConfig);
204+
IMPORT_FUNC(PyConfig_Clear);
205+
IMPORT_FUNC(Py_InitializeFromConfig);
206+
IMPORT_FUNC(PyStatus_Exception);
207+
}
208+
#endif
200209
#undef IMPORT_FUNC
201210
success = true;
202211

shared/obs-scripting/obs-scripting-python-import.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ PY_EXTERN int (*Import_PyType_GetFlags)(PyTypeObject *o);
134134
#if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
135135
PY_EXTERN void (*Import__Py_Dealloc)(PyObject *obj);
136136
#endif
137+
#if PY_VERSION_HEX >= 0x03080000
138+
PY_EXTERN PyStatus (*Import_PyWideStringList_Append)(PyWideStringList *list, const wchar_t *item);
139+
PY_EXTERN void (*Import_PyConfig_InitPythonConfig)(PyConfig *config);
140+
PY_EXTERN void (*Import_PyConfig_Clear)(PyConfig *config);
141+
PY_EXTERN PyStatus (*Import_Py_InitializeFromConfig)(const PyConfig *config);
142+
PY_EXTERN int (*Import_PyStatus_Exception)(PyStatus err);
143+
#endif
137144

138145
PY_EXTERN PyTypeObject PyCFunction_Type;
139146

@@ -222,6 +229,13 @@ extern bool import_python(const char *python_path, python_version_t *python_vers
222229
#if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
223230
#define _Py_Dealloc Import__Py_Dealloc
224231
#endif
232+
#if PY_VERSION_HEX >= 0x03080000
233+
#define PyWideStringList_Append Import_PyWideStringList_Append
234+
#define PyConfig_InitPythonConfig Import_PyConfig_InitPythonConfig
235+
#define PyConfig_Clear Import_PyConfig_Clear
236+
#define Py_InitializeFromConfig Import_Py_InitializeFromConfig
237+
#define PyStatus_Exception Import_PyStatus_Exception
238+
#endif
225239

226240
#if PY_VERSION_HEX >= 0x030800f0
227241
static inline void Import__Py_DECREF(const char *filename, int lineno, PyObject *op)

shared/obs-scripting/obs-scripting-python.c

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,65 @@ extern void add_python_frontend_funcs(PyObject *module);
15941594

15951595
static 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 >= 0x03080000
1636+
static bool python_init()
1637+
{
1638+
PyConfig config;
1639+
PyConfig_InitPythonConfig(&config);
1640+
1641+
/* ---------------------------------------------- */
1642+
/* Must set arguments for guis to work */
1643+
config.parse_argv = 1;
1644+
PyWideStringList_Append(&config.argv, L"");
1645+
1646+
PyStatus status = Py_InitializeFromConfig(&config);
1647+
PyConfig_Clear(&config);
1648+
1649+
if (PyStatus_Exception(status))
1650+
return false;
1651+
1652+
return true;
1653+
}
1654+
#endif
1655+
15971656
bool obs_scripting_load_python(const char *python_path)
15981657
{
15991658
if (python_loaded)
@@ -1621,33 +1680,19 @@ bool obs_scripting_load_python(const char *python_path)
16211680
UNUSED_PARAMETER(python_path);
16221681
#endif
16231682

1624-
Py_Initialize();
1625-
if (!Py_IsInitialized())
1626-
return false;
1627-
16281683
#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())
1684+
if (python_version.minor < 8 && !python_pre38_init())
1685+
return false;
1686+
if (python_version.minor > 7 && !python_init())
1687+
return false;
1688+
#elif PY_VERSION_HEX < 0x03080000
1689+
if (!python_pre38_init())
1690+
return false;
1691+
#else
1692+
if (!python_init())
16371693
return false;
16381694
#endif
16391695

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-
16511696
#ifdef __APPLE__
16521697
PyRun_SimpleString("import sys");
16531698
PyRun_SimpleString("sys.dont_write_bytecode = True");

0 commit comments

Comments
 (0)