main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. void Py_InitArgcArgv(int argc, wchar_t **argv);
  7. char* GetPyMain();
  8. int IsYaIdeVenv();
  9. static const char* env_entry_point = "Y_PYTHON_ENTRY_POINT";
  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 i, sts = 1;
  79. char* oldloc = NULL;
  80. wchar_t** argv_copy = NULL;
  81. /* We need a second copies, as Python might modify the first one. */
  82. wchar_t** argv_copy2 = NULL;
  83. char* entry_point_copy = NULL;
  84. if (argc > 0) {
  85. argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  86. argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  87. if (!argv_copy || !argv_copy2) {
  88. fprintf(stderr, "out of memory\n");
  89. goto error;
  90. }
  91. }
  92. PyConfig config;
  93. PyConfig_InitPythonConfig(&config);
  94. config.pathconfig_warnings = 0; /* Suppress errors from getpath.c */
  95. const char* bytes_warning = getenv(env_bytes_warning);
  96. if (bytes_warning) {
  97. config.bytes_warning = atoi(bytes_warning);
  98. }
  99. oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
  100. if (!oldloc) {
  101. fprintf(stderr, "out of memory\n");
  102. goto error;
  103. }
  104. setlocale(LC_ALL, "");
  105. for (i = 0; i < argc; i++) {
  106. argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
  107. argv_copy2[i] = argv_copy[i];
  108. if (!argv_copy[i]) {
  109. fprintf(stderr, "Unable to decode the command line argument #%i\n",
  110. i + 1);
  111. argc = i;
  112. goto error;
  113. }
  114. }
  115. setlocale(LC_ALL, oldloc);
  116. PyMem_RawFree(oldloc);
  117. oldloc = NULL;
  118. if (argc >= 1)
  119. Py_SetProgramName(argv_copy[0]);
  120. status = Py_InitializeFromConfig(&config);
  121. PyConfig_Clear(&config);
  122. if (PyStatus_Exception(status)) {
  123. Py_ExitStatusException(status);
  124. }
  125. const char* entry_point = getenv(env_entry_point);
  126. if (entry_point) {
  127. entry_point_copy = strdup(entry_point);
  128. if (!entry_point_copy) {
  129. fprintf(stderr, "out of memory\n");
  130. goto error;
  131. }
  132. } else {
  133. entry_point_copy = GetPyMain();
  134. }
  135. if (entry_point_copy == NULL) {
  136. fprintf(stderr, "No entry point, did you forget PY_MAIN?\n");
  137. goto error;
  138. }
  139. if (entry_point_copy && !strcmp(entry_point_copy, ":main")) {
  140. unsetenv(env_entry_point);
  141. sts = Py_Main(argc, argv_copy);
  142. free(entry_point_copy);
  143. return sts;
  144. }
  145. Py_InitArgcArgv(argc, argv_copy);
  146. PySys_SetArgv(argc, argv_copy);
  147. {
  148. PyObject* module = PyImport_ImportModule("library.python.runtime_py3.entry_points");
  149. if (module == NULL) {
  150. PyErr_Print();
  151. } else {
  152. PyObject* res = PyObject_CallMethod(module, "run_constructors", NULL);
  153. if (res == NULL) {
  154. PyErr_Print();
  155. } else {
  156. Py_DECREF(res);
  157. }
  158. Py_DECREF(module);
  159. }
  160. }
  161. const char* module_name = entry_point_copy;
  162. const char* func_name = NULL;
  163. char *colon = strchr(entry_point_copy, ':');
  164. if (colon != NULL) {
  165. colon[0] = '\0';
  166. func_name = colon + 1;
  167. }
  168. if (module_name[0] == '\0') {
  169. module_name = "library.python.runtime_py3.entry_points";
  170. }
  171. if (!func_name) {
  172. sts = RunModule(module_name);
  173. } else {
  174. PyObject* module = PyImport_ImportModule(module_name);
  175. if (module == NULL) {
  176. PyErr_Print();
  177. } else {
  178. PyObject* value = PyObject_CallMethod(module, func_name, NULL);
  179. if (value == NULL) {
  180. PyErr_Print();
  181. } else {
  182. Py_DECREF(value);
  183. sts = 0;
  184. }
  185. Py_DECREF(module);
  186. }
  187. }
  188. if (Py_FinalizeEx() < 0) {
  189. sts = 120;
  190. }
  191. error:
  192. free(entry_point_copy);
  193. PyMem_RawFree(argv_copy);
  194. if (argv_copy2) {
  195. for (i = 0; i < argc; i++)
  196. PyMem_RawFree(argv_copy2[i]);
  197. PyMem_RawFree(argv_copy2);
  198. }
  199. PyMem_RawFree(oldloc);
  200. return sts;
  201. }
  202. int (*mainptr)(int argc, char** argv) = pymain;
  203. int main(int argc, char** argv) {
  204. return mainptr(argc, argv);
  205. }