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.

123 lines
2.9 KiB

31 years ago
bpo-32030: Split Py_Main() into subfunctions (#4399) * Don't use "Python runtime" anymore to parse command line options or to get environment variables: pymain_init() is now a strict separation. * Use an error message rather than "crashing" directly with Py_FatalError(). Limit the number of calls to Py_FatalError(). It prepares the code to handle errors more nicely later. * Warnings options (-W, PYTHONWARNINGS) and "XOptions" (-X) are now only added to the sys module once Python core is properly initialized. * _PyMain is now the well identified owner of some important strings like: warnings options, XOptions, and the "program name". The program name string is now properly freed at exit. pymain_free() is now responsible to free the "command" string. * Rename most methods in Modules/main.c to use a "pymain_" prefix to avoid conflits and ease debug. * Replace _Py_CommandLineDetails_INIT with memset(0) * Reorder a lot of code to fix the initialization ordering. For example, initializing standard streams now comes before parsing PYTHONWARNINGS. * Py_Main() now handles errors when adding warnings options and XOptions. * Add _PyMem_GetDefaultRawAllocator() private function. * Cleanup _PyMem_Initialize(): remove useless global constants: move them into _PyMem_Initialize(). * Call _PyRuntime_Initialize() as soon as possible: _PyRuntime_Initialize() now returns an error message on failure. * Add _PyInitError structure and following macros: * _Py_INIT_OK() * _Py_INIT_ERR(msg) * _Py_INIT_USER_ERR(msg): "user" error, don't abort() in that case * _Py_INIT_FAILED(err)
8 years ago
bpo-32030: Split Py_Main() into subfunctions (#4399) * Don't use "Python runtime" anymore to parse command line options or to get environment variables: pymain_init() is now a strict separation. * Use an error message rather than "crashing" directly with Py_FatalError(). Limit the number of calls to Py_FatalError(). It prepares the code to handle errors more nicely later. * Warnings options (-W, PYTHONWARNINGS) and "XOptions" (-X) are now only added to the sys module once Python core is properly initialized. * _PyMain is now the well identified owner of some important strings like: warnings options, XOptions, and the "program name". The program name string is now properly freed at exit. pymain_free() is now responsible to free the "command" string. * Rename most methods in Modules/main.c to use a "pymain_" prefix to avoid conflits and ease debug. * Replace _Py_CommandLineDetails_INIT with memset(0) * Reorder a lot of code to fix the initialization ordering. For example, initializing standard streams now comes before parsing PYTHONWARNINGS. * Py_Main() now handles errors when adding warnings options and XOptions. * Add _PyMem_GetDefaultRawAllocator() private function. * Cleanup _PyMem_Initialize(): remove useless global constants: move them into _PyMem_Initialize(). * Call _PyRuntime_Initialize() as soon as possible: _PyRuntime_Initialize() now returns an error message on failure. * Add _PyInitError structure and following macros: * _Py_INIT_OK() * _Py_INIT_ERR(msg) * _Py_INIT_USER_ERR(msg): "user" error, don't abort() in that case * _Py_INIT_FAILED(err)
8 years ago
  1. /* Python interpreter main program for frozen scripts */
  2. #include "Python.h"
  3. #include "internal/pystate.h"
  4. #include <locale.h>
  5. #ifdef MS_WINDOWS
  6. extern void PyWinFreeze_ExeInit(void);
  7. extern void PyWinFreeze_ExeTerm(void);
  8. extern int PyInitFrozenExtensions(void);
  9. #endif
  10. /* Main program */
  11. int
  12. Py_FrozenMain(int argc, char **argv)
  13. {
  14. _PyInitError err = _PyRuntime_Initialize();
  15. if (_Py_INIT_FAILED(err)) {
  16. fprintf(stderr, "Fatal Python error: %s\n", err.msg);
  17. fflush(stderr);
  18. exit(1);
  19. }
  20. const char *p;
  21. int i, n, sts = 1;
  22. int inspect = 0;
  23. int unbuffered = 0;
  24. char *oldloc = NULL;
  25. wchar_t **argv_copy = NULL;
  26. /* We need a second copies, as Python might modify the first one. */
  27. wchar_t **argv_copy2 = NULL;
  28. if (argc > 0) {
  29. argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  30. argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  31. if (!argv_copy || !argv_copy2) {
  32. fprintf(stderr, "out of memory\n");
  33. goto error;
  34. }
  35. }
  36. Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
  37. if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
  38. inspect = 1;
  39. if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
  40. unbuffered = 1;
  41. if (unbuffered) {
  42. setbuf(stdin, (char *)NULL);
  43. setbuf(stdout, (char *)NULL);
  44. setbuf(stderr, (char *)NULL);
  45. }
  46. oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
  47. if (!oldloc) {
  48. fprintf(stderr, "out of memory\n");
  49. goto error;
  50. }
  51. setlocale(LC_ALL, "");
  52. for (i = 0; i < argc; i++) {
  53. argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
  54. argv_copy2[i] = argv_copy[i];
  55. if (!argv_copy[i]) {
  56. fprintf(stderr, "Unable to decode the command line argument #%i\n",
  57. i + 1);
  58. argc = i;
  59. goto error;
  60. }
  61. }
  62. setlocale(LC_ALL, oldloc);
  63. PyMem_RawFree(oldloc);
  64. oldloc = NULL;
  65. #ifdef MS_WINDOWS
  66. PyInitFrozenExtensions();
  67. #endif /* MS_WINDOWS */
  68. if (argc >= 1)
  69. Py_SetProgramName(argv_copy[0]);
  70. Py_Initialize();
  71. #ifdef MS_WINDOWS
  72. PyWinFreeze_ExeInit();
  73. #endif
  74. if (Py_VerboseFlag)
  75. fprintf(stderr, "Python %s\n%s\n",
  76. Py_GetVersion(), Py_GetCopyright());
  77. PySys_SetArgv(argc, argv_copy);
  78. n = PyImport_ImportFrozenModule("__main__");
  79. if (n == 0)
  80. Py_FatalError("__main__ not frozen");
  81. if (n < 0) {
  82. PyErr_Print();
  83. sts = 1;
  84. }
  85. else
  86. sts = 0;
  87. if (inspect && isatty((int)fileno(stdin)))
  88. sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
  89. #ifdef MS_WINDOWS
  90. PyWinFreeze_ExeTerm();
  91. #endif
  92. if (Py_FinalizeEx() < 0) {
  93. sts = 120;
  94. }
  95. error:
  96. PyMem_RawFree(argv_copy);
  97. if (argv_copy2) {
  98. for (i = 0; i < argc; i++)
  99. PyMem_RawFree(argv_copy2[i]);
  100. PyMem_RawFree(argv_copy2);
  101. }
  102. PyMem_RawFree(oldloc);
  103. return sts;
  104. }