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.

131 lines
3.2 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. _PyCoreConfig config = _PyCoreConfig_INIT;
  37. config._frozen = 1; /* Suppress errors from getpath.c */
  38. if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
  39. inspect = 1;
  40. if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
  41. unbuffered = 1;
  42. if (unbuffered) {
  43. setbuf(stdin, (char *)NULL);
  44. setbuf(stdout, (char *)NULL);
  45. setbuf(stderr, (char *)NULL);
  46. }
  47. oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
  48. if (!oldloc) {
  49. fprintf(stderr, "out of memory\n");
  50. goto error;
  51. }
  52. setlocale(LC_ALL, "");
  53. for (i = 0; i < argc; i++) {
  54. argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
  55. argv_copy2[i] = argv_copy[i];
  56. if (!argv_copy[i]) {
  57. fprintf(stderr, "Unable to decode the command line argument #%i\n",
  58. i + 1);
  59. argc = i;
  60. goto error;
  61. }
  62. }
  63. setlocale(LC_ALL, oldloc);
  64. PyMem_RawFree(oldloc);
  65. oldloc = NULL;
  66. #ifdef MS_WINDOWS
  67. PyInitFrozenExtensions();
  68. #endif /* MS_WINDOWS */
  69. if (argc >= 1)
  70. Py_SetProgramName(argv_copy[0]);
  71. err = _Py_InitializeFromConfig(&config);
  72. /* No need to call _PyCoreConfig_Clear() since we didn't allocate any
  73. memory: program_name is a constant string. */
  74. if (_Py_INIT_FAILED(err)) {
  75. _Py_FatalInitError(err);
  76. }
  77. #ifdef MS_WINDOWS
  78. PyWinFreeze_ExeInit();
  79. #endif
  80. if (Py_VerboseFlag)
  81. fprintf(stderr, "Python %s\n%s\n",
  82. Py_GetVersion(), Py_GetCopyright());
  83. PySys_SetArgv(argc, argv_copy);
  84. n = PyImport_ImportFrozenModule("__main__");
  85. if (n == 0)
  86. Py_FatalError("__main__ not frozen");
  87. if (n < 0) {
  88. PyErr_Print();
  89. sts = 1;
  90. }
  91. else
  92. sts = 0;
  93. if (inspect && isatty((int)fileno(stdin)))
  94. sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
  95. #ifdef MS_WINDOWS
  96. PyWinFreeze_ExeTerm();
  97. #endif
  98. if (Py_FinalizeEx() < 0) {
  99. sts = 120;
  100. }
  101. error:
  102. PyMem_RawFree(argv_copy);
  103. if (argv_copy2) {
  104. for (i = 0; i < argc; i++)
  105. PyMem_RawFree(argv_copy2[i]);
  106. PyMem_RawFree(argv_copy2);
  107. }
  108. PyMem_RawFree(oldloc);
  109. return sts;
  110. }