From f7e5b56c37eb859e225e886c79c5d742c567ee95 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 15 Nov 2017 15:48:08 -0800 Subject: 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) --- Python/import.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'Python/import.c') diff --git a/Python/import.c b/Python/import.c index 950c872ccb..fe60844ef1 100644 --- a/Python/import.c +++ b/Python/import.c @@ -42,19 +42,23 @@ module _imp /* Initialize things */ -void +_PyInitError _PyImport_Init(void) { PyInterpreterState *interp = PyThreadState_Get()->interp; initstr = PyUnicode_InternFromString("__init__"); - if (initstr == NULL) - Py_FatalError("Can't initialize import variables"); + if (initstr == NULL) { + return _Py_INIT_ERR("Can't initialize import variables"); + } + interp->builtins_copy = PyDict_Copy(interp->builtins); - if (interp->builtins_copy == NULL) - Py_FatalError("Can't backup builtins dict"); + if (interp->builtins_copy == NULL) { + return _Py_INIT_ERR("Can't backup builtins dict"); + } + return _Py_INIT_OK(); } -void +_PyInitError _PyImportHooks_Init(void) { PyObject *v, *path_hooks = NULL; @@ -80,15 +84,18 @@ _PyImportHooks_Init(void) goto error; err = PySys_SetObject("path_hooks", path_hooks); if (err) { - error: - PyErr_Print(); - Py_FatalError("initializing sys.meta_path, sys.path_hooks, " - "or path_importer_cache failed"); + goto error; } Py_DECREF(path_hooks); + return _Py_INIT_OK(); + + error: + PyErr_Print(); + return _Py_INIT_ERR("initializing sys.meta_path, sys.path_hooks, " + "or path_importer_cache failed"); } -void +_PyInitError _PyImportZip_Init(void) { PyObject *path_hooks, *zimpimport; @@ -133,11 +140,11 @@ _PyImportZip_Init(void) } } - return; + return _Py_INIT_OK(); error: PyErr_Print(); - Py_FatalError("initializing zipimport failed"); + return _Py_INIT_ERR("initializing zipimport failed"); } /* Locking primitives to prevent parallel imports of the same module -- cgit v1.2.1