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.

129 lines
3.1 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_runtime.h" // _PyRuntime_Initialize()
  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. PyConfig_InitPythonConfig(&config);
  36. config.pathconfig_warnings = 0; /* 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. status = Py_InitializeFromConfig(&config);
  71. PyConfig_Clear(&config);
  72. if (PyStatus_Exception(status)) {
  73. Py_ExitStatusException(status);
  74. }
  75. #ifdef MS_WINDOWS
  76. PyWinFreeze_ExeInit();
  77. #endif
  78. if (Py_VerboseFlag)
  79. fprintf(stderr, "Python %s\n%s\n",
  80. Py_GetVersion(), Py_GetCopyright());
  81. PySys_SetArgv(argc, argv_copy);
  82. n = PyImport_ImportFrozenModule("__main__");
  83. if (n == 0)
  84. Py_FatalError("the __main__ module is not frozen");
  85. if (n < 0) {
  86. PyErr_Print();
  87. sts = 1;
  88. }
  89. else
  90. sts = 0;
  91. if (inspect && isatty((int)fileno(stdin)))
  92. sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
  93. #ifdef MS_WINDOWS
  94. PyWinFreeze_ExeTerm();
  95. #endif
  96. if (Py_FinalizeEx() < 0) {
  97. sts = 120;
  98. }
  99. error:
  100. PyMem_RawFree(argv_copy);
  101. if (argv_copy2) {
  102. for (i = 0; i < argc; i++)
  103. PyMem_RawFree(argv_copy2[i]);
  104. PyMem_RawFree(argv_copy2);
  105. }
  106. PyMem_RawFree(oldloc);
  107. return sts;
  108. }