obs-scripting-python-import.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/%s/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, "[Python] Invalid python_version pointer provided.");
  44. goto fail;
  45. }
  46. dstr_init_copy(&lib_path, python_path);
  47. dstr_replace(&lib_path, "\\", "/");
  48. if (!dstr_is_empty(&lib_path)) {
  49. dstr_cat(&lib_path, "/");
  50. }
  51. struct dstr lib_candidate_path;
  52. dstr_init(&lib_candidate_path);
  53. char cur_version[5];
  54. char next_version[5];
  55. snprintf(cur_version, sizeof(cur_version), VERSION_PATTERN, PY_MAJOR_VERSION_MAX, PY_MINOR_VERSION_MAX);
  56. struct dstr temp;
  57. dstr_init(&temp);
  58. dstr_printf(&temp, FILE_PATTERN, cur_version, cur_version);
  59. int minor_version = PY_MINOR_VERSION_MAX;
  60. do {
  61. dstr_printf(&lib_candidate_path, "%s%s", lib_path.array, temp.array);
  62. if (access(lib_candidate_path.array, F_OK) == 0) {
  63. lib = os_dlopen(lib_candidate_path.array);
  64. }
  65. if (lib) {
  66. break;
  67. }
  68. snprintf(cur_version, sizeof(cur_version), VERSION_PATTERN, PY_MAJOR_VERSION_MAX, minor_version);
  69. snprintf(next_version, sizeof(next_version), VERSION_PATTERN, PY_MAJOR_VERSION_MAX, --minor_version);
  70. dstr_replace(&temp, cur_version, next_version);
  71. } while (minor_version > 5);
  72. dstr_free(&temp);
  73. dstr_free(&lib_candidate_path);
  74. if (!lib) {
  75. blog(LOG_WARNING, "[Python] Could not load library: %s", lib_path.array);
  76. goto fail;
  77. }
  78. python_version->major = PY_MAJOR_VERSION_MAX;
  79. python_version->minor = minor_version;
  80. #define IMPORT_FUNC(x) \
  81. do { \
  82. Import_##x = os_dlsym(lib, #x); \
  83. if (!Import_##x) { \
  84. blog(LOG_WARNING, "[Python] Failed to import: %s", #x); \
  85. goto fail; \
  86. } \
  87. } while (false)
  88. IMPORT_FUNC(PyType_Ready);
  89. IMPORT_FUNC(PyType_Modified);
  90. IMPORT_FUNC(PyType_IsSubtype);
  91. IMPORT_FUNC(PyObject_GenericGetAttr);
  92. IMPORT_FUNC(PyObject_IsTrue);
  93. IMPORT_FUNC(Py_DecRef);
  94. IMPORT_FUNC(PyObject_Malloc);
  95. IMPORT_FUNC(PyObject_Free);
  96. IMPORT_FUNC(PyObject_Init);
  97. IMPORT_FUNC(PyUnicode_FromFormat);
  98. IMPORT_FUNC(PyUnicode_Concat);
  99. IMPORT_FUNC(PyLong_FromVoidPtr);
  100. IMPORT_FUNC(PyLong_FromLong);
  101. IMPORT_FUNC(PyBool_FromLong);
  102. IMPORT_FUNC(PyGILState_Ensure);
  103. IMPORT_FUNC(PyGILState_GetThisThreadState);
  104. IMPORT_FUNC(PyErr_SetString);
  105. IMPORT_FUNC(PyErr_Occurred);
  106. IMPORT_FUNC(PyErr_Fetch);
  107. IMPORT_FUNC(PyErr_Restore);
  108. IMPORT_FUNC(PyErr_WriteUnraisable);
  109. IMPORT_FUNC(PyArg_UnpackTuple);
  110. IMPORT_FUNC(Py_BuildValue);
  111. IMPORT_FUNC(PyRun_SimpleStringFlags);
  112. IMPORT_FUNC(PyErr_Print);
  113. IMPORT_FUNC(Py_SetPythonHome);
  114. IMPORT_FUNC(Py_Initialize);
  115. IMPORT_FUNC(Py_Finalize);
  116. IMPORT_FUNC(Py_IsInitialized);
  117. if (python_version->major == 3 && python_version->minor < 7) {
  118. IMPORT_FUNC(PyEval_InitThreads);
  119. IMPORT_FUNC(PyEval_ThreadsInitialized);
  120. }
  121. IMPORT_FUNC(PyEval_ReleaseThread);
  122. IMPORT_FUNC(PySys_SetArgv);
  123. IMPORT_FUNC(PyImport_ImportModule);
  124. IMPORT_FUNC(PyObject_CallFunctionObjArgs);
  125. IMPORT_FUNC(_Py_NotImplementedStruct);
  126. IMPORT_FUNC(PyExc_TypeError);
  127. IMPORT_FUNC(PyExc_RuntimeError);
  128. IMPORT_FUNC(PyObject_GetAttr);
  129. IMPORT_FUNC(PyUnicode_FromString);
  130. IMPORT_FUNC(PyDict_New);
  131. IMPORT_FUNC(PyDict_GetItemString);
  132. IMPORT_FUNC(PyDict_SetItemString);
  133. IMPORT_FUNC(PyCFunction_NewEx);
  134. if (python_version->major == 3 && python_version->minor >= 9)
  135. IMPORT_FUNC(PyCMethod_New);
  136. IMPORT_FUNC(PyModule_GetDict);
  137. IMPORT_FUNC(PyModule_GetNameObject);
  138. IMPORT_FUNC(PyModule_AddObject);
  139. IMPORT_FUNC(PyModule_AddStringConstant);
  140. IMPORT_FUNC(PyImport_Import);
  141. IMPORT_FUNC(PyObject_CallObject);
  142. IMPORT_FUNC(_Py_FalseStruct);
  143. IMPORT_FUNC(_Py_TrueStruct);
  144. IMPORT_FUNC(PyGILState_Release);
  145. IMPORT_FUNC(PyList_Append);
  146. IMPORT_FUNC(PySys_GetObject);
  147. IMPORT_FUNC(PyImport_ReloadModule);
  148. IMPORT_FUNC(PyObject_GetAttrString);
  149. IMPORT_FUNC(PyCapsule_New);
  150. IMPORT_FUNC(PyCapsule_GetPointer);
  151. IMPORT_FUNC(PyArg_ParseTuple);
  152. IMPORT_FUNC(PyFunction_Type);
  153. IMPORT_FUNC(PyObject_SetAttr);
  154. IMPORT_FUNC(_PyObject_New);
  155. IMPORT_FUNC(PyCapsule_Import);
  156. IMPORT_FUNC(PyErr_Clear);
  157. IMPORT_FUNC(PyObject_Call);
  158. IMPORT_FUNC(PyList_New);
  159. IMPORT_FUNC(PyList_Size);
  160. IMPORT_FUNC(PyList_GetItem);
  161. IMPORT_FUNC(PyUnicode_AsUTF8String);
  162. IMPORT_FUNC(PyLong_FromUnsignedLongLong);
  163. IMPORT_FUNC(PyArg_VaParse);
  164. IMPORT_FUNC(_Py_NoneStruct);
  165. IMPORT_FUNC(PyTuple_New);
  166. if (python_version->major == 3 && python_version->minor >= 9) {
  167. IMPORT_FUNC(PyType_GetFlags);
  168. }
  169. #if defined(Py_DEBUG) || PY_VERSION_HEX >= 0x030900b0
  170. IMPORT_FUNC(_Py_Dealloc);
  171. #endif
  172. #undef IMPORT_FUNC
  173. success = true;
  174. fail:
  175. if (!success && lib)
  176. os_dlclose(lib);
  177. dstr_free(&lib_path);
  178. return success;
  179. }