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.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;
  15. int inspect = 0;
  16. int unbuffered = 0;
  17. char *oldloc;
  18. wchar_t **argv_copy = PyMem_Malloc(sizeof(wchar_t*)*argc);
  19. /* We need a second copies, as Python might modify the first one. */
  20. wchar_t **argv_copy2 = PyMem_Malloc(sizeof(wchar_t*)*argc);
  21. Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
  22. if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
  23. inspect = 1;
  24. if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
  25. unbuffered = 1;
  26. if (unbuffered) {
  27. setbuf(stdin, (char *)NULL);
  28. setbuf(stdout, (char *)NULL);
  29. setbuf(stderr, (char *)NULL);
  30. }
  31. if (!argv_copy) {
  32. fprintf(stderr, "out of memory\n");
  33. return 1;
  34. }
  35. oldloc = setlocale(LC_ALL, NULL);
  36. setlocale(LC_ALL, "");
  37. for (i = 0; i < argc; i++) {
  38. #ifdef HAVE_BROKEN_MBSTOWCS
  39. size_t argsize = strlen(argv[i]);
  40. #else
  41. size_t argsize = mbstowcs(NULL, argv[i], 0);
  42. #endif
  43. size_t count;
  44. if (argsize == (size_t)-1) {
  45. fprintf(stderr, "Could not convert argument %d to string\n", i);
  46. return 1;
  47. }
  48. argv_copy[i] = PyMem_Malloc((argsize+1)*sizeof(wchar_t));
  49. argv_copy2[i] = argv_copy[i];
  50. if (!argv_copy[i]) {
  51. fprintf(stderr, "out of memory\n");
  52. return 1;
  53. }
  54. count = mbstowcs(argv_copy[i], argv[i], argsize+1);
  55. if (count == (size_t)-1) {
  56. fprintf(stderr, "Could not convert argument %d to string\n", i);
  57. return 1;
  58. }
  59. }
  60. setlocale(LC_ALL, oldloc);
  61. #ifdef MS_WINDOWS
  62. PyInitFrozenExtensions();
  63. #endif /* MS_WINDOWS */
  64. Py_SetProgramName(argv_copy[0]);
  65. Py_Initialize();
  66. #ifdef MS_WINDOWS
  67. PyWinFreeze_ExeInit();
  68. #endif
  69. if (Py_VerboseFlag)
  70. fprintf(stderr, "Python %s\n%s\n",
  71. Py_GetVersion(), Py_GetCopyright());
  72. PySys_SetArgv(argc, argv_copy);
  73. n = PyImport_ImportFrozenModule("__main__");
  74. if (n == 0)
  75. Py_FatalError("__main__ not frozen");
  76. if (n < 0) {
  77. PyErr_Print();
  78. sts = 1;
  79. }
  80. else
  81. sts = 0;
  82. if (inspect && isatty((int)fileno(stdin)))
  83. sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
  84. #ifdef MS_WINDOWS
  85. PyWinFreeze_ExeTerm();
  86. #endif
  87. Py_Finalize();
  88. for (i = 0; i < argc; i++) {
  89. PyMem_Free(argv_copy2[i]);
  90. }
  91. PyMem_Free(argv_copy);
  92. PyMem_Free(argv_copy2);
  93. return sts;
  94. }