main.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <Python.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void Py_InitArgcArgv(int argc, char** argv);
  5. static const char* env_entry_point = "Y_PYTHON_ENTRY_POINT";
  6. #ifdef _MSC_VER
  7. extern char** environ;
  8. void unsetenv(const char* name) {
  9. const int n = strlen(name);
  10. char** dst = environ;
  11. for (char** src = environ; *src; src++)
  12. if (strncmp(*src, name, n) || (*src)[n] != '=')
  13. *dst++ = *src;
  14. *dst = NULL;
  15. }
  16. #endif
  17. static int pymain(int argc, char** argv) {
  18. const char* entry_point = getenv(env_entry_point);
  19. if (entry_point && !strcmp(entry_point, ":main")) {
  20. unsetenv(env_entry_point);
  21. return Py_Main(argc, argv);
  22. }
  23. Py_InitArgcArgv(argc, argv);
  24. Py_SetProgramName(argv[0]);
  25. Py_Initialize();
  26. PySys_SetArgv(argc, argv);
  27. int rc = PyRun_SimpleString(
  28. "from library.python.runtime import entry_points\n"
  29. "entry_points.run_constructors()\n"
  30. "import __res\n"
  31. "__res.importer.run_main()\n");
  32. Py_Finalize();
  33. return rc == 0 ? 0 : 1;
  34. }
  35. int (*mainptr)(int argc, char** argv) = pymain;
  36. int main(int argc, char** argv) {
  37. return mainptr(argc, argv);
  38. }