main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include <Python.h>
  2. #include <contrib/tools/python3/Include/internal/pycore_runtime.h> // _PyRuntime_Initialize()
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <locale.h>
  6. char* GetPyMain();
  7. int IsYaIdeVenv();
  8. static const char* env_entry_point = "Y_PYTHON_ENTRY_POINT";
  9. static const char* main_entry_point = ":main";
  10. static const char* env_bytes_warning = "Y_PYTHON_BYTES_WARNING";
  11. #ifdef _MSC_VER
  12. extern char** environ;
  13. void unsetenv(const char* name) {
  14. const int n = strlen(name);
  15. char** dst = environ;
  16. for (char** src = environ; *src; src++)
  17. if (strncmp(*src, name, n) || (*src)[n] != '=')
  18. *dst++ = *src;
  19. *dst = NULL;
  20. }
  21. #endif
  22. static int RunModule(const char* modname)
  23. {
  24. PyObject *module, *runpy, *runmodule, *runargs, *result;
  25. runpy = PyImport_ImportModule("runpy");
  26. if (runpy == NULL) {
  27. fprintf(stderr, "Could not import runpy module\n");
  28. PyErr_Print();
  29. return -1;
  30. }
  31. runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
  32. if (runmodule == NULL) {
  33. fprintf(stderr, "Could not access runpy._run_module_as_main\n");
  34. PyErr_Print();
  35. Py_DECREF(runpy);
  36. return -1;
  37. }
  38. module = PyUnicode_FromString(modname);
  39. if (module == NULL) {
  40. fprintf(stderr, "Could not convert module name to unicode\n");
  41. PyErr_Print();
  42. Py_DECREF(runpy);
  43. Py_DECREF(runmodule);
  44. return -1;
  45. }
  46. runargs = Py_BuildValue("(Oi)", module, 0);
  47. if (runargs == NULL) {
  48. fprintf(stderr,
  49. "Could not create arguments for runpy._run_module_as_main\n");
  50. PyErr_Print();
  51. Py_DECREF(runpy);
  52. Py_DECREF(runmodule);
  53. Py_DECREF(module);
  54. return -1;
  55. }
  56. result = PyObject_Call(runmodule, runargs, NULL);
  57. if (result == NULL) {
  58. PyErr_Print();
  59. }
  60. Py_DECREF(runpy);
  61. Py_DECREF(runmodule);
  62. Py_DECREF(module);
  63. Py_DECREF(runargs);
  64. if (result == NULL) {
  65. return -1;
  66. }
  67. Py_DECREF(result);
  68. return 0;
  69. }
  70. #ifdef MS_WINDOWS
  71. static int pymain(int argc, wchar_t** argv)
  72. #else
  73. static int pymain(int argc, char** argv)
  74. #endif
  75. {
  76. PyStatus status;
  77. if (IsYaIdeVenv()) {
  78. #ifdef MS_WINDOWS
  79. return Py_Main(argc, argv);
  80. #else
  81. return Py_BytesMain(argc, argv);
  82. #endif
  83. }
  84. status = _PyRuntime_Initialize();
  85. if (PyStatus_Exception(status)) {
  86. Py_ExitStatusException(status);
  87. }
  88. PyPreConfig preconfig;
  89. PyPreConfig_InitPythonConfig(&preconfig);
  90. // Enable UTF-8 mode for all (DEVTOOLSSUPPORT-46624)
  91. preconfig.utf8_mode = 1;
  92. #ifdef MS_WINDOWS
  93. preconfig.legacy_windows_fs_encoding = 0;
  94. #endif
  95. status = Py_PreInitialize(&preconfig);
  96. if (PyStatus_Exception(status)) {
  97. Py_ExitStatusException(status);
  98. }
  99. int sts = 1;
  100. char* entry_point_copy = NULL;
  101. PyConfig config;
  102. PyConfig_InitPythonConfig(&config);
  103. // Suppress errors from getpath.c
  104. config.pathconfig_warnings = 0;
  105. // Disable parsing command line arguments
  106. config.parse_argv = 0;
  107. const char* bytes_warning = getenv(env_bytes_warning);
  108. if (bytes_warning) {
  109. config.bytes_warning = atoi(bytes_warning);
  110. }
  111. if (argc > 0 && argv) {
  112. #ifdef MS_WINDOWS
  113. status = PyConfig_SetString(&config, &config.program_name, argv[0]);
  114. #else
  115. status = PyConfig_SetBytesString(&config, &config.program_name, argv[0]);
  116. #endif
  117. if (PyStatus_Exception(status)) {
  118. goto error;
  119. }
  120. #ifdef MS_WINDOWS
  121. status = PyConfig_SetArgv(&config, argc, argv);
  122. #else
  123. status = PyConfig_SetBytesArgv(&config, argc, argv);
  124. #endif
  125. if (PyStatus_Exception(status)) {
  126. goto error;
  127. }
  128. }
  129. const char* entry_point = getenv(env_entry_point);
  130. if (entry_point) {
  131. entry_point_copy = strdup(entry_point);
  132. if (!entry_point_copy) {
  133. fprintf(stderr, "out of memory\n");
  134. goto error;
  135. }
  136. } else {
  137. entry_point_copy = GetPyMain();
  138. }
  139. if (entry_point_copy == NULL) {
  140. fprintf(stderr, "No entry point, did you forget PY_MAIN?\n");
  141. goto error;
  142. }
  143. if (entry_point_copy && !strcmp(entry_point_copy, main_entry_point)) {
  144. unsetenv(env_entry_point);
  145. // Py_InitializeFromConfig freeze environ, so we need to finish all manipulations with environ before
  146. }
  147. status = Py_InitializeFromConfig(&config);
  148. PyConfig_Clear(&config);
  149. if (PyStatus_Exception(status)) {
  150. Py_ExitStatusException(status);
  151. }
  152. if (entry_point_copy && !strcmp(entry_point_copy, main_entry_point)) {
  153. #ifdef MS_WINDOWS
  154. sts = Py_Main(argc, argv);
  155. #else
  156. sts = Py_BytesMain(argc, argv);
  157. #endif
  158. free(entry_point_copy);
  159. return sts;
  160. }
  161. {
  162. PyObject* module = PyImport_ImportModule("library.python.runtime_py3.entry_points");
  163. if (module == NULL) {
  164. PyErr_Print();
  165. } else {
  166. PyObject* res = PyObject_CallMethod(module, "run_constructors", NULL);
  167. if (res == NULL) {
  168. PyErr_Print();
  169. } else {
  170. Py_DECREF(res);
  171. }
  172. Py_DECREF(module);
  173. }
  174. }
  175. const char* module_name = entry_point_copy;
  176. const char* func_name = NULL;
  177. char* colon = strchr(entry_point_copy, ':');
  178. if (colon != NULL) {
  179. colon[0] = '\0';
  180. func_name = colon + 1;
  181. }
  182. if (module_name[0] == '\0') {
  183. module_name = "library.python.runtime_py3.entry_points";
  184. }
  185. if (!func_name) {
  186. sts = RunModule(module_name);
  187. } else {
  188. PyObject* module = PyImport_ImportModule(module_name);
  189. if (module == NULL) {
  190. PyErr_Print();
  191. } else {
  192. PyObject* value = PyObject_CallMethod(module, func_name, NULL);
  193. if (value == NULL) {
  194. PyErr_Print();
  195. } else {
  196. Py_DECREF(value);
  197. sts = 0;
  198. }
  199. Py_DECREF(module);
  200. }
  201. }
  202. if (Py_FinalizeEx() < 0) {
  203. sts = 120;
  204. }
  205. error:
  206. free(entry_point_copy);
  207. return sts;
  208. }
  209. #ifdef MS_WINDOWS
  210. int (*mainptr)(int argc, wchar_t** argv) = pymain;
  211. int wmain(int argc, wchar_t** argv) {
  212. return mainptr(argc, argv);
  213. }
  214. #else
  215. int (*mainptr)(int argc, char** argv) = pymain;
  216. int main(int argc, char** argv) {
  217. return mainptr(argc, argv);
  218. }
  219. #endif