main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. static int pymain(int argc, char** argv) {
  71. PyStatus status;
  72. if (IsYaIdeVenv()) {
  73. return Py_BytesMain(argc, argv);
  74. }
  75. status = _PyRuntime_Initialize();
  76. if (PyStatus_Exception(status)) {
  77. Py_ExitStatusException(status);
  78. }
  79. PyPreConfig preconfig;
  80. PyPreConfig_InitPythonConfig(&preconfig);
  81. // Enable UTF-8 mode for all (DEVTOOLSSUPPORT-46624)
  82. preconfig.utf8_mode = 1;
  83. #ifdef MS_WINDOWS
  84. preconfig.legacy_windows_fs_encoding = 0;
  85. #endif
  86. status = Py_PreInitialize(&preconfig);
  87. if (PyStatus_Exception(status)) {
  88. Py_ExitStatusException(status);
  89. }
  90. int sts = 1;
  91. char* entry_point_copy = NULL;
  92. PyConfig config;
  93. PyConfig_InitPythonConfig(&config);
  94. // Suppress errors from getpath.c
  95. config.pathconfig_warnings = 0;
  96. // Disable parsing command line arguments
  97. config.parse_argv = 0;
  98. const char* bytes_warning = getenv(env_bytes_warning);
  99. if (bytes_warning) {
  100. config.bytes_warning = atoi(bytes_warning);
  101. }
  102. if (argc > 0 && argv) {
  103. status = PyConfig_SetBytesString(&config, &config.program_name, argv[0]);
  104. if (PyStatus_Exception(status)) {
  105. goto error;
  106. }
  107. status = PyConfig_SetBytesArgv(&config, argc, argv);
  108. if (PyStatus_Exception(status)) {
  109. goto error;
  110. }
  111. }
  112. const char* entry_point = getenv(env_entry_point);
  113. if (entry_point) {
  114. entry_point_copy = strdup(entry_point);
  115. if (!entry_point_copy) {
  116. fprintf(stderr, "out of memory\n");
  117. goto error;
  118. }
  119. } else {
  120. entry_point_copy = GetPyMain();
  121. }
  122. if (entry_point_copy == NULL) {
  123. fprintf(stderr, "No entry point, did you forget PY_MAIN?\n");
  124. goto error;
  125. }
  126. if (entry_point_copy && !strcmp(entry_point_copy, main_entry_point)) {
  127. unsetenv(env_entry_point);
  128. // Py_InitializeFromConfig freeze environ, so we need to finish all manipulations with environ before
  129. }
  130. status = Py_InitializeFromConfig(&config);
  131. PyConfig_Clear(&config);
  132. if (PyStatus_Exception(status)) {
  133. Py_ExitStatusException(status);
  134. }
  135. if (entry_point_copy && !strcmp(entry_point_copy, main_entry_point)) {
  136. sts = Py_BytesMain(argc, argv);
  137. free(entry_point_copy);
  138. return sts;
  139. }
  140. {
  141. PyObject* module = PyImport_ImportModule("library.python.runtime_py3.entry_points");
  142. if (module == NULL) {
  143. PyErr_Print();
  144. } else {
  145. PyObject* res = PyObject_CallMethod(module, "run_constructors", NULL);
  146. if (res == NULL) {
  147. PyErr_Print();
  148. } else {
  149. Py_DECREF(res);
  150. }
  151. Py_DECREF(module);
  152. }
  153. }
  154. const char* module_name = entry_point_copy;
  155. const char* func_name = NULL;
  156. char* colon = strchr(entry_point_copy, ':');
  157. if (colon != NULL) {
  158. colon[0] = '\0';
  159. func_name = colon + 1;
  160. }
  161. if (module_name[0] == '\0') {
  162. module_name = "library.python.runtime_py3.entry_points";
  163. }
  164. if (!func_name) {
  165. sts = RunModule(module_name);
  166. } else {
  167. PyObject* module = PyImport_ImportModule(module_name);
  168. if (module == NULL) {
  169. PyErr_Print();
  170. } else {
  171. PyObject* value = PyObject_CallMethod(module, func_name, NULL);
  172. if (value == NULL) {
  173. PyErr_Print();
  174. } else {
  175. Py_DECREF(value);
  176. sts = 0;
  177. }
  178. Py_DECREF(module);
  179. }
  180. }
  181. if (Py_FinalizeEx() < 0) {
  182. sts = 120;
  183. }
  184. error:
  185. free(entry_point_copy);
  186. return sts;
  187. }
  188. int (*mainptr)(int argc, char** argv) = pymain;
  189. int main(int argc, char** argv) {
  190. return mainptr(argc, argv);
  191. }