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.

133 lines
3.2 KiB

31 years ago
bpo-36763: Implement the PEP 587 (GH-13592) * Add a whole new documentation page: "Python Initialization Configuration" * PyWideStringList_Append() return type is now PyStatus, instead of int * PyInterpreterState_New() now calls PyConfig_Clear() if PyConfig_InitPythonConfig() fails. * Rename files: * Python/coreconfig.c => Python/initconfig.c * Include/cpython/coreconfig.h => Include/cpython/initconfig.h * Include/internal/: pycore_coreconfig.h => pycore_initconfig.h * Rename structures * _PyCoreConfig => PyConfig * _PyPreConfig => PyPreConfig * _PyInitError => PyStatus * _PyWstrList => PyWideStringList * Rename PyConfig fields: * use_module_search_paths => module_search_paths_set * module_search_path_env => pythonpath_env * Rename PyStatus field: _func => func * PyInterpreterState: rename core_config field to config * Rename macros and functions: * _PyCoreConfig_SetArgv() => PyConfig_SetBytesArgv() * _PyCoreConfig_SetWideArgv() => PyConfig_SetArgv() * _PyCoreConfig_DecodeLocale() => PyConfig_SetBytesString() * _PyInitError_Failed() => PyStatus_Exception() * _Py_INIT_ERROR_TYPE_xxx enums => _PyStatus_TYPE_xxx * _Py_UnixMain() => Py_BytesMain() * _Py_ExitInitError() => Py_ExitStatusException() * _Py_PreInitializeFromArgs() => Py_PreInitializeFromBytesArgs() * _Py_PreInitializeFromWideArgs() => Py_PreInitializeFromArgs() * _Py_PreInitialize() => Py_PreInitialize() * _Py_RunMain() => Py_RunMain() * _Py_InitializeFromConfig() => Py_InitializeFromConfig() * _Py_INIT_XXX() => _PyStatus_XXX() * _Py_INIT_FAILED() => _PyStatus_EXCEPTION() * Rename 'err' PyStatus variables to 'status' * Convert RUN_CODE() macro to config_run_code() static inline function * Remove functions: * _Py_InitializeFromArgs() * _Py_InitializeFromWideArgs() * _PyInterpreterState_GetCoreConfig()
7 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-36763: Implement the PEP 587 (GH-13592) * Add a whole new documentation page: "Python Initialization Configuration" * PyWideStringList_Append() return type is now PyStatus, instead of int * PyInterpreterState_New() now calls PyConfig_Clear() if PyConfig_InitPythonConfig() fails. * Rename files: * Python/coreconfig.c => Python/initconfig.c * Include/cpython/coreconfig.h => Include/cpython/initconfig.h * Include/internal/: pycore_coreconfig.h => pycore_initconfig.h * Rename structures * _PyCoreConfig => PyConfig * _PyPreConfig => PyPreConfig * _PyInitError => PyStatus * _PyWstrList => PyWideStringList * Rename PyConfig fields: * use_module_search_paths => module_search_paths_set * module_search_path_env => pythonpath_env * Rename PyStatus field: _func => func * PyInterpreterState: rename core_config field to config * Rename macros and functions: * _PyCoreConfig_SetArgv() => PyConfig_SetBytesArgv() * _PyCoreConfig_SetWideArgv() => PyConfig_SetArgv() * _PyCoreConfig_DecodeLocale() => PyConfig_SetBytesString() * _PyInitError_Failed() => PyStatus_Exception() * _Py_INIT_ERROR_TYPE_xxx enums => _PyStatus_TYPE_xxx * _Py_UnixMain() => Py_BytesMain() * _Py_ExitInitError() => Py_ExitStatusException() * _Py_PreInitializeFromArgs() => Py_PreInitializeFromBytesArgs() * _Py_PreInitializeFromWideArgs() => Py_PreInitializeFromArgs() * _Py_PreInitialize() => Py_PreInitialize() * _Py_RunMain() => Py_RunMain() * _Py_InitializeFromConfig() => Py_InitializeFromConfig() * _Py_INIT_XXX() => _PyStatus_XXX() * _Py_INIT_FAILED() => _PyStatus_EXCEPTION() * Rename 'err' PyStatus variables to 'status' * Convert RUN_CODE() macro to config_run_code() static inline function * Remove functions: * _Py_InitializeFromArgs() * _Py_InitializeFromWideArgs() * _PyInterpreterState_GetCoreConfig()
7 years ago
bpo-36763: Implement the PEP 587 (GH-13592) * Add a whole new documentation page: "Python Initialization Configuration" * PyWideStringList_Append() return type is now PyStatus, instead of int * PyInterpreterState_New() now calls PyConfig_Clear() if PyConfig_InitPythonConfig() fails. * Rename files: * Python/coreconfig.c => Python/initconfig.c * Include/cpython/coreconfig.h => Include/cpython/initconfig.h * Include/internal/: pycore_coreconfig.h => pycore_initconfig.h * Rename structures * _PyCoreConfig => PyConfig * _PyPreConfig => PyPreConfig * _PyInitError => PyStatus * _PyWstrList => PyWideStringList * Rename PyConfig fields: * use_module_search_paths => module_search_paths_set * module_search_path_env => pythonpath_env * Rename PyStatus field: _func => func * PyInterpreterState: rename core_config field to config * Rename macros and functions: * _PyCoreConfig_SetArgv() => PyConfig_SetBytesArgv() * _PyCoreConfig_SetWideArgv() => PyConfig_SetArgv() * _PyCoreConfig_DecodeLocale() => PyConfig_SetBytesString() * _PyInitError_Failed() => PyStatus_Exception() * _Py_INIT_ERROR_TYPE_xxx enums => _PyStatus_TYPE_xxx * _Py_UnixMain() => Py_BytesMain() * _Py_ExitInitError() => Py_ExitStatusException() * _Py_PreInitializeFromArgs() => Py_PreInitializeFromBytesArgs() * _Py_PreInitializeFromWideArgs() => Py_PreInitializeFromArgs() * _Py_PreInitialize() => Py_PreInitialize() * _Py_RunMain() => Py_RunMain() * _Py_InitializeFromConfig() => Py_InitializeFromConfig() * _Py_INIT_XXX() => _PyStatus_XXX() * _Py_INIT_FAILED() => _PyStatus_EXCEPTION() * Rename 'err' PyStatus variables to 'status' * Convert RUN_CODE() macro to config_run_code() static inline function * Remove functions: * _Py_InitializeFromArgs() * _Py_InitializeFromWideArgs() * _PyInterpreterState_GetCoreConfig()
7 years ago
  1. /* Python interpreter main program for frozen scripts */
  2. #include "Python.h"
  3. #include "pycore_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. PyStatus status = _PyRuntime_Initialize();
  15. if (PyStatus_Exception(status)) {
  16. Py_ExitStatusException(status);
  17. }
  18. const char *p;
  19. int i, n, sts = 1;
  20. int inspect = 0;
  21. int unbuffered = 0;
  22. char *oldloc = NULL;
  23. wchar_t **argv_copy = NULL;
  24. /* We need a second copies, as Python might modify the first one. */
  25. wchar_t **argv_copy2 = NULL;
  26. if (argc > 0) {
  27. argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  28. argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
  29. if (!argv_copy || !argv_copy2) {
  30. fprintf(stderr, "out of memory\n");
  31. goto error;
  32. }
  33. }
  34. PyConfig config;
  35. status = PyConfig_InitPythonConfig(&config);
  36. if (PyStatus_Exception(status)) {
  37. PyConfig_Clear(&config);
  38. Py_ExitStatusException(status);
  39. }
  40. config.pathconfig_warnings = 0; /* Suppress errors from getpath.c */
  41. if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
  42. inspect = 1;
  43. if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
  44. unbuffered = 1;
  45. if (unbuffered) {
  46. setbuf(stdin, (char *)NULL);
  47. setbuf(stdout, (char *)NULL);
  48. setbuf(stderr, (char *)NULL);
  49. }
  50. oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
  51. if (!oldloc) {
  52. fprintf(stderr, "out of memory\n");
  53. goto error;
  54. }
  55. setlocale(LC_ALL, "");
  56. for (i = 0; i < argc; i++) {
  57. argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
  58. argv_copy2[i] = argv_copy[i];
  59. if (!argv_copy[i]) {
  60. fprintf(stderr, "Unable to decode the command line argument #%i\n",
  61. i + 1);
  62. argc = i;
  63. goto error;
  64. }
  65. }
  66. setlocale(LC_ALL, oldloc);
  67. PyMem_RawFree(oldloc);
  68. oldloc = NULL;
  69. #ifdef MS_WINDOWS
  70. PyInitFrozenExtensions();
  71. #endif /* MS_WINDOWS */
  72. if (argc >= 1)
  73. Py_SetProgramName(argv_copy[0]);
  74. status = Py_InitializeFromConfig(&config);
  75. PyConfig_Clear(&config);
  76. if (PyStatus_Exception(status)) {
  77. Py_ExitStatusException(status);
  78. }
  79. #ifdef MS_WINDOWS
  80. PyWinFreeze_ExeInit();
  81. #endif
  82. if (Py_VerboseFlag)
  83. fprintf(stderr, "Python %s\n%s\n",
  84. Py_GetVersion(), Py_GetCopyright());
  85. PySys_SetArgv(argc, argv_copy);
  86. n = PyImport_ImportFrozenModule("__main__");
  87. if (n == 0)
  88. Py_FatalError("__main__ not frozen");
  89. if (n < 0) {
  90. PyErr_Print();
  91. sts = 1;
  92. }
  93. else
  94. sts = 0;
  95. if (inspect && isatty((int)fileno(stdin)))
  96. sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
  97. #ifdef MS_WINDOWS
  98. PyWinFreeze_ExeTerm();
  99. #endif
  100. if (Py_FinalizeEx() < 0) {
  101. sts = 120;
  102. }
  103. error:
  104. PyMem_RawFree(argv_copy);
  105. if (argv_copy2) {
  106. for (i = 0; i < argc; i++)
  107. PyMem_RawFree(argv_copy2[i]);
  108. PyMem_RawFree(argv_copy2);
  109. }
  110. PyMem_RawFree(oldloc);
  111. return sts;
  112. }