obs-scripting-python-import.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <util/dstr.h>
  15. #include <util/platform.h>
  16. #define NO_REDEFS
  17. #include "obs-scripting-python-import.h"
  18. #ifdef _MSC_VER
  19. #pragma warning(disable : 4152)
  20. #endif
  21. #ifdef _WIN32
  22. #include <windows.h>
  23. #include <io.h>
  24. #define F_OK 0
  25. #define access _access
  26. #define VERSION_PATTERN "%d%d"
  27. #define FILE_PATTERN "python%s.dll"
  28. #define PATH_MAX MAX_PATH
  29. #elif __APPLE__
  30. #define VERSION_PATTERN "%d.%d"
  31. #define FILE_PATTERN "Python.framework/Versions/Current/lib/libpython%s.dylib"
  32. #endif
  33. #define PY_MAJOR_VERSION_MAX 3
  34. #define PY_MINOR_VERSION_MAX 12
  35. bool import_python(const char *python_path, python_version_t *python_version)
  36. {
  37. struct dstr lib_path;
  38. bool success = false;
  39. void *lib = NULL;
  40. if (!python_path)
  41. python_path = "";
  42. if (!python_version) {
  43. blog(LOG_DEBUG,
  44. "[Python] Invalid python_version pointer provided.");
  45. goto fail;
  46. }
  47. dstr_init_copy(&lib_path, python_path);
  48. dstr_replace(&lib_path, "\\", "/");
  49. if (!dstr_is_empty(&lib_path)) {
  50. dstr_cat(&lib_path, "/");
  51. }
  52. struct dstr lib_candidate_path;
  53. dstr_init_copy(&lib_candidate_path, lib_path.array);
  54. char cur_version[5];
  55. char next_version[5];
  56. char temp[PATH_MAX];
  57. snprintf(cur_version, sizeof(cur_version), VERSION_PATTERN,
  58. PY_MAJOR_VERSION_MAX, PY_MINOR_VERSION_MAX);
  59. snprintf(temp, sizeof(temp), FILE_PATTERN, cur_version);
  60. dstr_cat(&lib_candidate_path, temp);
  61. int minor_version = PY_MINOR_VERSION_MAX;
  62. do {
  63. if (access(lib_candidate_path.array, F_OK) == 0) {
  64. lib = os_dlopen(lib_candidate_path.array);
  65. }
  66. if (lib) {
  67. break;
  68. }
  69. snprintf(cur_version, sizeof(cur_version), VERSION_PATTERN,
  70. PY_MAJOR_VERSION_MAX, minor_version);
  71. snprintf(next_version, sizeof(next_version), VERSION_PATTERN,
  72. PY_MAJOR_VERSION_MAX, --minor_version);
  73. dstr_replace(&lib_candidate_path, cur_version, next_version);
  74. } while (minor_version > 5);
  75. dstr_free(&lib_candidate_path);
  76. if (!lib) {
  77. blog(LOG_WARNING, "[Python] Could not load library: %s",
  78. lib_path.array);
  79. goto fail;
  80. }
  81. python_version->major = PY_MAJOR_VERSION_MAX;
  82. python_version->minor = minor_version;
  83. #define IMPORT_FUNC(x) \
  84. do { \
  85. Import_##x = os_dlsym(lib, #x); \
  86. if (!Import_##x) { \
  87. blog(LOG_WARNING, "[Python] Failed to import: %s", \
  88. #x); \
  89. goto fail; \
  90. } \
  91. } while (false)
  92. IMPORT_FUNC(PyType_Ready);
  93. IMPORT_FUNC(PyType_Modified);
  94. IMPORT_FUNC(PyObject_GenericGetAttr);
  95. IMPORT_FUNC(PyObject_IsTrue);
  96. IMPORT_FUNC(Py_DecRef);
  97. IMPORT_FUNC(PyObject_Malloc);
  98. IMPORT_FUNC(PyObject_Free);
  99. IMPORT_FUNC(PyObject_Init);
  100. IMPORT_FUNC(PyUnicode_FromFormat);
  101. IMPORT_FUNC(PyUnicode_Concat);
  102. IMPORT_FUNC(PyLong_FromVoidPtr);
  103. IMPORT_FUNC(PyLong_FromLong);
  104. IMPORT_FUNC(PyBool_FromLong);
  105. IMPORT_FUNC(PyGILState_Ensure);
  106. IMPORT_FUNC(PyGILState_GetThisThreadState);
  107. IMPORT_FUNC(PyErr_SetString);
  108. IMPORT_FUNC(PyErr_Occurred);
  109. IMPORT_FUNC(PyErr_Fetch);
  110. IMPORT_FUNC(PyErr_Restore);
  111. IMPORT_FUNC(PyErr_WriteUnraisable);
  112. IMPORT_FUNC(PyArg_UnpackTuple);
  113. IMPORT_FUNC(Py_BuildValue);
  114. IMPORT_FUNC(PyRun_SimpleStringFlags);
  115. IMPORT_FUNC(PyErr_Print);
  116. IMPORT_FUNC(Py_SetPythonHome);
  117. IMPORT_FUNC(Py_Initialize);
  118. IMPORT_FUNC(Py_Finalize);
  119. IMPORT_FUNC(Py_IsInitialized);
  120. if (python_version->major == 3 && python_version->minor < 7) {
  121. IMPORT_FUNC(PyEval_InitThreads);
  122. IMPORT_FUNC(PyEval_ThreadsInitialized);
  123. }
  124. IMPORT_FUNC(PyEval_ReleaseThread);
  125. IMPORT_FUNC(PySys_SetArgv);
  126. IMPORT_FUNC(PyImport_ImportModule);
  127. IMPORT_FUNC(PyObject_CallFunctionObjArgs);
  128. IMPORT_FUNC(_Py_NotImplementedStruct);
  129. IMPORT_FUNC(PyExc_TypeError);
  130. IMPORT_FUNC(PyExc_RuntimeError);
  131. IMPORT_FUNC(PyObject_GetAttr);
  132. IMPORT_FUNC(PyUnicode_FromString);
  133. IMPORT_FUNC(PyDict_New);
  134. IMPORT_FUNC(PyDict_GetItemString);
  135. IMPORT_FUNC(PyDict_SetItemString);
  136. IMPORT_FUNC(PyCFunction_NewEx);
  137. if (python_version->major == 3 && python_version->minor >= 9)
  138. IMPORT_FUNC(PyCMethod_New);
  139. IMPORT_FUNC(PyModule_GetDict);
  140. IMPORT_FUNC(PyModule_GetNameObject);
  141. IMPORT_FUNC(PyModule_AddObject);
  142. IMPORT_FUNC(PyModule_AddStringConstant);
  143. IMPORT_FUNC(PyImport_Import);
  144. IMPORT_FUNC(PyObject_CallObject);
  145. IMPORT_FUNC(_Py_FalseStruct);
  146. IMPORT_FUNC(_Py_TrueStruct);
  147. IMPORT_FUNC(PyGILState_Release);
  148. IMPORT_FUNC(PyList_Append);
  149. IMPORT_FUNC(PySys_GetObject);
  150. IMPORT_FUNC(PyImport_ReloadModule);
  151. IMPORT_FUNC(PyObject_GetAttrString);
  152. IMPORT_FUNC(PyCapsule_New);
  153. IMPORT_FUNC(PyCapsule_GetPointer);
  154. IMPORT_FUNC(PyArg_ParseTuple);
  155. IMPORT_FUNC(PyFunction_Type);
  156. IMPORT_FUNC(PyObject_SetAttr);
  157. IMPORT_FUNC(_PyObject_New);
  158. IMPORT_FUNC(PyCapsule_Import);
  159. IMPORT_FUNC(PyErr_Clear);
  160. IMPORT_FUNC(PyObject_Call);
  161. IMPORT_FUNC(PyList_New);
  162. IMPORT_FUNC(PyList_Size);
  163. IMPORT_FUNC(PyList_GetItem);
  164. IMPORT_FUNC(PyUnicode_AsUTF8String);
  165. IMPORT_FUNC(PyLong_FromUnsignedLongLong);
  166. IMPORT_FUNC(PyArg_VaParse);
  167. IMPORT_FUNC(_Py_NoneStruct);
  168. IMPORT_FUNC(PyTuple_New);
  169. if (python_version->major == 3 && python_version->minor >= 9) {
  170. IMPORT_FUNC(PyType_GetFlags);
  171. }
  172. #if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
  173. IMPORT_FUNC(_Py_Dealloc);
  174. #endif
  175. #undef IMPORT_FUNC
  176. success = true;
  177. fail:
  178. if (!success && lib)
  179. os_dlclose(lib);
  180. dstr_free(&lib_path);
  181. return success;
  182. }