You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.7 KiB

31 years ago
  1. /* Python interpreter main program for frozen scripts */
  2. #include "Python.h"
  3. #include <locale.h>
  4. #ifdef MS_WINDOWS
  5. extern void PyWinFreeze_ExeInit(void);
  6. extern void PyWinFreeze_ExeTerm(void);
  7. extern int PyInitFrozenExtensions(void);
  8. #endif
  9. /* Main program */
  10. int
  11. Py_FrozenMain(int argc, char **argv)
  12. {
  13. char *p;
  14. int i, n, sts = 1;
  15. int inspect = 0;
  16. int unbuffered = 0;
  17. char *oldloc = NULL;
  18. wchar_t **argv_copy = NULL;
  19. /* We need a second copies, as Python might modify the first one. */
  20. wchar_t **argv_copy2 = NULL;
  21. if (argc > 0) {
  22. argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  23. argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  24. if (!argv_copy || !argv_copy2) {
  25. fprintf(stderr, "out of memory\n");
  26. goto error;
  27. }
  28. }
  29. Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
  30. if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
  31. inspect = 1;
  32. if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
  33. unbuffered = 1;
  34. if (unbuffered) {
  35. setbuf(stdin, (char *)NULL);
  36. setbuf(stdout, (char *)NULL);
  37. setbuf(stderr, (char *)NULL);
  38. }
  39. oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
  40. if (!oldloc) {
  41. fprintf(stderr, "out of memory\n");
  42. goto error;
  43. }
  44. setlocale(LC_ALL, "");
  45. for (i = 0; i < argc; i++) {
  46. argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
  47. argv_copy2[i] = argv_copy[i];
  48. if (!argv_copy[i]) {
  49. fprintf(stderr, "Unable to decode the command line argument #%i\n",
  50. i + 1);
  51. argc = i;
  52. goto error;
  53. }
  54. }
  55. setlocale(LC_ALL, oldloc);
  56. PyMem_RawFree(oldloc);
  57. oldloc = NULL;
  58. #ifdef MS_WINDOWS
  59. PyInitFrozenExtensions();
  60. #endif /* MS_WINDOWS */
  61. if (argc >= 1)
  62. Py_SetProgramName(argv_copy[0]);
  63. Py_Initialize();
  64. #ifdef MS_WINDOWS
  65. PyWinFreeze_ExeInit();
  66. #endif
  67. if (Py_VerboseFlag)
  68. fprintf(stderr, "Python %s\n%s\n",
  69. Py_GetVersion(), Py_GetCopyright());
  70. PySys_SetArgv(argc, argv_copy);
  71. n = PyImport_ImportFrozenModule("__main__");
  72. if (n == 0)
  73. Py_FatalError("__main__ not frozen");
  74. if (n < 0) {
  75. PyErr_Print();
  76. sts = 1;
  77. }
  78. else
  79. sts = 0;
  80. if (inspect && isatty((int)fileno(stdin)))
  81. sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
  82. #ifdef MS_WINDOWS
  83. PyWinFreeze_ExeTerm();
  84. #endif
  85. if (Py_FinalizeEx() < 0) {
  86. sts = 120;
  87. }
  88. error:
  89. PyMem_RawFree(argv_copy);
  90. if (argv_copy2) {
  91. for (i = 0; i < argc; i++)
  92. PyMem_RawFree(argv_copy2[i]);
  93. PyMem_RawFree(argv_copy2);
  94. }
  95. PyMem_RawFree(oldloc);
  96. return sts;
  97. }