main.c 5.4 KB

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