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.

110 lines
2.6 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. argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  22. argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  23. if (!argv_copy || !argv_copy2) {
  24. fprintf(stderr, "out of memory\n");
  25. goto error;
  26. }
  27. Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
  28. if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
  29. inspect = 1;
  30. if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
  31. unbuffered = 1;
  32. if (unbuffered) {
  33. setbuf(stdin, (char *)NULL);
  34. setbuf(stdout, (char *)NULL);
  35. setbuf(stderr, (char *)NULL);
  36. }
  37. oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
  38. if (!oldloc) {
  39. fprintf(stderr, "out of memory\n");
  40. goto error;
  41. }
  42. setlocale(LC_ALL, "");
  43. for (i = 0; i < argc; i++) {
  44. argv_copy[i] = _Py_char2wchar(argv[i], NULL);
  45. argv_copy2[i] = argv_copy[i];
  46. if (!argv_copy[i]) {
  47. fprintf(stderr, "Unable to decode the command line argument #%i\n",
  48. i + 1);
  49. argc = i;
  50. goto error;
  51. }
  52. }
  53. setlocale(LC_ALL, oldloc);
  54. PyMem_RawFree(oldloc);
  55. oldloc = NULL;
  56. #ifdef MS_WINDOWS
  57. PyInitFrozenExtensions();
  58. #endif /* MS_WINDOWS */
  59. Py_SetProgramName(argv_copy[0]);
  60. Py_Initialize();
  61. #ifdef MS_WINDOWS
  62. PyWinFreeze_ExeInit();
  63. #endif
  64. if (Py_VerboseFlag)
  65. fprintf(stderr, "Python %s\n%s\n",
  66. Py_GetVersion(), Py_GetCopyright());
  67. PySys_SetArgv(argc, argv_copy);
  68. n = PyImport_ImportFrozenModule("__main__");
  69. if (n == 0)
  70. Py_FatalError("__main__ not frozen");
  71. if (n < 0) {
  72. PyErr_Print();
  73. sts = 1;
  74. }
  75. else
  76. sts = 0;
  77. if (inspect && isatty((int)fileno(stdin)))
  78. sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
  79. #ifdef MS_WINDOWS
  80. PyWinFreeze_ExeTerm();
  81. #endif
  82. Py_Finalize();
  83. error:
  84. PyMem_RawFree(argv_copy);
  85. if (argv_copy2) {
  86. for (i = 0; i < argc; i++)
  87. PyMem_RawFree(argv_copy2[i]);
  88. PyMem_RawFree(argv_copy2);
  89. }
  90. PyMem_RawFree(oldloc);
  91. return sts;
  92. }