main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. static const char* env_entry_point = "Y_PYTHON_ENTRY_POINT";
  9. static const char* env_bytes_warning = "Y_PYTHON_BYTES_WARNING";
  10. #ifdef _MSC_VER
  11. extern char** environ;
  12. void unsetenv(const char* name) {
  13. const int n = strlen(name);
  14. char** dst = environ;
  15. for (char** src = environ; *src; src++)
  16. if (strncmp(*src, name, n) || (*src)[n] != '=')
  17. *dst++ = *src;
  18. *dst = NULL;
  19. }
  20. #endif
  21. static int RunModule(const char *modname)
  22. {
  23. PyObject *module, *runpy, *runmodule, *runargs, *result;
  24. runpy = PyImport_ImportModule("runpy");
  25. if (runpy == NULL) {
  26. fprintf(stderr, "Could not import runpy module\n");
  27. PyErr_Print();
  28. return -1;
  29. }
  30. runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
  31. if (runmodule == NULL) {
  32. fprintf(stderr, "Could not access runpy._run_module_as_main\n");
  33. PyErr_Print();
  34. Py_DECREF(runpy);
  35. return -1;
  36. }
  37. module = PyUnicode_FromString(modname);
  38. if (module == NULL) {
  39. fprintf(stderr, "Could not convert module name to unicode\n");
  40. PyErr_Print();
  41. Py_DECREF(runpy);
  42. Py_DECREF(runmodule);
  43. return -1;
  44. }
  45. runargs = Py_BuildValue("(Oi)", module, 0);
  46. if (runargs == NULL) {
  47. fprintf(stderr,
  48. "Could not create arguments for runpy._run_module_as_main\n");
  49. PyErr_Print();
  50. Py_DECREF(runpy);
  51. Py_DECREF(runmodule);
  52. Py_DECREF(module);
  53. return -1;
  54. }
  55. result = PyObject_Call(runmodule, runargs, NULL);
  56. if (result == NULL) {
  57. PyErr_Print();
  58. }
  59. Py_DECREF(runpy);
  60. Py_DECREF(runmodule);
  61. Py_DECREF(module);
  62. Py_DECREF(runargs);
  63. if (result == NULL) {
  64. return -1;
  65. }
  66. Py_DECREF(result);
  67. return 0;
  68. }
  69. static int pymain(int argc, char** argv) {
  70. PyStatus status = _PyRuntime_Initialize();
  71. if (PyStatus_Exception(status)) {
  72. Py_ExitStatusException(status);
  73. }
  74. int i, sts = 1;
  75. char* oldloc = NULL;
  76. wchar_t** argv_copy = NULL;
  77. /* We need a second copies, as Python might modify the first one. */
  78. wchar_t** argv_copy2 = NULL;
  79. char* entry_point_copy = NULL;
  80. if (argc > 0) {
  81. argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  82. argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  83. if (!argv_copy || !argv_copy2) {
  84. fprintf(stderr, "out of memory\n");
  85. goto error;
  86. }
  87. }
  88. PyConfig config;
  89. PyConfig_InitPythonConfig(&config);
  90. config.pathconfig_warnings = 0; /* Suppress errors from getpath.c */
  91. const char* bytes_warning = getenv(env_bytes_warning);
  92. if (bytes_warning) {
  93. config.bytes_warning = atoi(bytes_warning);
  94. }
  95. oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
  96. if (!oldloc) {
  97. fprintf(stderr, "out of memory\n");
  98. goto error;
  99. }
  100. setlocale(LC_ALL, "");
  101. for (i = 0; i < argc; i++) {
  102. argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
  103. argv_copy2[i] = argv_copy[i];
  104. if (!argv_copy[i]) {
  105. fprintf(stderr, "Unable to decode the command line argument #%i\n",
  106. i + 1);
  107. argc = i;
  108. goto error;
  109. }
  110. }
  111. setlocale(LC_ALL, oldloc);
  112. PyMem_RawFree(oldloc);
  113. oldloc = NULL;
  114. if (argc >= 1)
  115. Py_SetProgramName(argv_copy[0]);
  116. status = Py_InitializeFromConfig(&config);
  117. PyConfig_Clear(&config);
  118. if (PyStatus_Exception(status)) {
  119. Py_ExitStatusException(status);
  120. }
  121. const char* entry_point = getenv(env_entry_point);
  122. if (entry_point) {
  123. entry_point_copy = strdup(entry_point);
  124. if (!entry_point_copy) {
  125. fprintf(stderr, "out of memory\n");
  126. goto error;
  127. }
  128. } else {
  129. entry_point_copy = GetPyMain();
  130. }
  131. if (entry_point_copy == NULL) {
  132. fprintf(stderr, "No entry point, did you forget PY_MAIN?\n");
  133. goto error;
  134. }
  135. if (entry_point_copy && !strcmp(entry_point_copy, ":main")) {
  136. unsetenv(env_entry_point);
  137. sts = Py_Main(argc, argv_copy);
  138. free(entry_point_copy);
  139. return sts;
  140. }
  141. Py_InitArgcArgv(argc, argv_copy);
  142. PySys_SetArgv(argc, argv_copy);
  143. {
  144. PyObject* module = PyImport_ImportModule("library.python.runtime_py3.entry_points");
  145. if (module == NULL) {
  146. PyErr_Print();
  147. } else {
  148. PyObject* res = PyObject_CallMethod(module, "run_constructors", NULL);
  149. if (res == NULL) {
  150. PyErr_Print();
  151. } else {
  152. Py_DECREF(res);
  153. }
  154. Py_DECREF(module);
  155. }
  156. }
  157. const char* module_name = entry_point_copy;
  158. const char* func_name = NULL;
  159. char *colon = strchr(entry_point_copy, ':');
  160. if (colon != NULL) {
  161. colon[0] = '\0';
  162. func_name = colon + 1;
  163. }
  164. if (module_name[0] == '\0') {
  165. module_name = "library.python.runtime_py3.entry_points";
  166. }
  167. if (!func_name) {
  168. sts = RunModule(module_name);
  169. } else {
  170. PyObject* module = PyImport_ImportModule(module_name);
  171. if (module == NULL) {
  172. PyErr_Print();
  173. } else {
  174. PyObject* value = PyObject_CallMethod(module, func_name, NULL);
  175. if (value == NULL) {
  176. PyErr_Print();
  177. } else {
  178. Py_DECREF(value);
  179. sts = 0;
  180. }
  181. Py_DECREF(module);
  182. }
  183. }
  184. if (Py_FinalizeEx() < 0) {
  185. sts = 120;
  186. }
  187. error:
  188. free(entry_point_copy);
  189. PyMem_RawFree(argv_copy);
  190. if (argv_copy2) {
  191. for (i = 0; i < argc; i++)
  192. PyMem_RawFree(argv_copy2[i]);
  193. PyMem_RawFree(argv_copy2);
  194. }
  195. PyMem_RawFree(oldloc);
  196. return sts;
  197. }
  198. int (*mainptr)(int argc, char** argv) = pymain;
  199. int main(int argc, char** argv) {
  200. return mainptr(argc, argv);
  201. }