bpo-36763: _PyInitError always use int for exitcode (GH-13360)

We cannot use "unsigned int" for exitcode on Windows, since
Py_Main() and _Py_RunMain() always return an "int".

Changes:

* _PyPathConfig_ComputeSysPath0() now returns -1 if an exception is
  raised.
* pymain_run_python() no longer uses _PyInitError but display the
  exception and set exitcode to 1 in case of error.
* Fix _Py_RunMain(): return an exitcode rather than calling
  exit() on pymain_run_python() failure.
* _Py_ExitInitError() no longer uses ExitProcess() on Windows, use
  exit() on all platforms.
* _Py_ExitInitError() now fails with a fatal error if 'err' is not an
  error not an exit.
This commit is contained in:
Victor Stinner 2019-05-16 16:39:26 +02:00 committed by GitHub
parent 6e78900282
commit dbacfc2273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 44 deletions

View File

@ -15,11 +15,7 @@ typedef struct {
} _type; } _type;
const char *_func; const char *_func;
const char *err_msg; const char *err_msg;
#ifdef MS_WINDOWS
unsigned int exitcode;
#else
int exitcode; int exitcode;
#endif
} _PyInitError; } _PyInitError;
/* Almost all errors causing Python initialization to fail */ /* Almost all errors causing Python initialization to fail */

View File

@ -130,7 +130,7 @@ pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
if (sysdict != NULL) { if (sysdict != NULL) {
sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path); sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
if (sys_path == NULL && PyErr_Occurred()) { if (sys_path == NULL && PyErr_Occurred()) {
goto error; return -1;
} }
} }
else { else {
@ -138,17 +138,13 @@ pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
} }
if (sys_path == NULL) { if (sys_path == NULL) {
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path"); PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
goto error; return -1;
} }
if (PyList_Insert(sys_path, 0, path0)) { if (PyList_Insert(sys_path, 0, path0)) {
goto error; return -1;
} }
return 0; return 0;
error:
PyErr_Print();
return -1;
} }
@ -443,11 +439,9 @@ pymain_repl(_PyCoreConfig *config, PyCompilerFlags *cf, int *exitcode)
} }
static _PyInitError static void
pymain_run_python(int *exitcode) pymain_run_python(int *exitcode)
{ {
_PyInitError err;
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
/* pymain_run_stdin() modify the config */ /* pymain_run_stdin() modify the config */
_PyCoreConfig *config = &interp->core_config; _PyCoreConfig *config = &interp->core_config;
@ -464,22 +458,20 @@ pymain_run_python(int *exitcode)
if (main_importer_path != NULL) { if (main_importer_path != NULL) {
if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) { if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
err = _Py_INIT_EXIT(1); goto error;
goto done;
} }
} }
else if (!config->isolated) { else if (!config->isolated) {
PyObject *path0 = NULL; PyObject *path0 = NULL;
if (_PyPathConfig_ComputeSysPath0(&config->argv, &path0)) { int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0);
if (path0 == NULL) { if (res < 0) {
err = _Py_INIT_NO_MEMORY(); goto error;
goto done; }
}
if (res > 0) {
if (pymain_sys_path_add_path0(interp, path0) < 0) { if (pymain_sys_path_add_path0(interp, path0) < 0) {
Py_DECREF(path0); Py_DECREF(path0);
err = _Py_INIT_EXIT(1); goto error;
goto done;
} }
Py_DECREF(path0); Py_DECREF(path0);
} }
@ -508,11 +500,14 @@ pymain_run_python(int *exitcode)
} }
pymain_repl(config, &cf, exitcode); pymain_repl(config, &cf, exitcode);
err = _Py_INIT_OK(); goto done;
error:
PyErr_Print();
*exitcode = 1;
done: done:
Py_XDECREF(main_importer_path); Py_XDECREF(main_importer_path);
return err;
} }
@ -578,17 +573,14 @@ _Py_RunMain(void)
{ {
int exitcode = 0; int exitcode = 0;
_PyInitError err = pymain_run_python(&exitcode); pymain_run_python(&exitcode);
if (_Py_INIT_FAILED(err)) {
pymain_exit_error(err);
}
if (Py_FinalizeEx() < 0) { if (Py_FinalizeEx() < 0) {
/* Value unlikely to be confused with a non-error exit status or /* Value unlikely to be confused with a non-error exit status or
other special meaning */ other special meaning */
exitcode = 120; exitcode = 120;
} }
done:
pymain_free(); pymain_free();
if (_Py_UnhandledKeyboardInterrupt) { if (_Py_UnhandledKeyboardInterrupt) {
@ -603,6 +595,10 @@ static int
pymain_main(_PyArgv *args) pymain_main(_PyArgv *args)
{ {
_PyInitError err = pymain_init(args); _PyInitError err = pymain_init(args);
if (_Py_INIT_IS_EXIT(err)) {
pymain_free();
return err.exitcode;
}
if (_Py_INIT_FAILED(err)) { if (_Py_INIT_FAILED(err)) {
pymain_exit_error(err); pymain_exit_error(err);
} }

View File

@ -570,18 +570,17 @@ Py_GetProgramName(void)
directory ("-m module" case) which will be prepended to sys.argv: directory ("-m module" case) which will be prepended to sys.argv:
sys.path[0]. sys.path[0].
Return 1 if the path is correctly resolved, but *path0_p can be NULL Return 1 if the path is correctly resolved and written into *path0_p.
if the Unicode object fail to be created.
Return 0 if it fails to resolve the full path (and *path0_p will be NULL). Return 0 if it fails to resolve the full path. For example, return 0 if the
For example, return 0 if the current working directory has been removed current working directory has been removed (bpo-36236) or if argv is empty.
(bpo-36236) or if argv is empty.
Raise an exception and return -1 on error.
*/ */
int int
_PyPathConfig_ComputeSysPath0(const _PyWstrList *argv, PyObject **path0_p) _PyPathConfig_ComputeSysPath0(const _PyWstrList *argv, PyObject **path0_p)
{ {
assert(_PyWstrList_CheckConsistency(argv)); assert(_PyWstrList_CheckConsistency(argv));
assert(*path0_p == NULL);
if (argv->length == 0) { if (argv->length == 0) {
/* Leave sys.path unchanged if sys.argv is empty */ /* Leave sys.path unchanged if sys.argv is empty */
@ -697,7 +696,12 @@ _PyPathConfig_ComputeSysPath0(const _PyWstrList *argv, PyObject **path0_p)
} }
#endif /* All others */ #endif /* All others */
*path0_p = PyUnicode_FromWideChar(path0, n); PyObject *path0_obj = PyUnicode_FromWideChar(path0, n);
if (path0_obj == NULL) {
return -1;
}
*path0_p = path0_obj;
return 1; return 1;
} }

View File

@ -2124,17 +2124,14 @@ Py_FatalError(const char *msg)
void _Py_NO_RETURN void _Py_NO_RETURN
_Py_ExitInitError(_PyInitError err) _Py_ExitInitError(_PyInitError err)
{ {
assert(_Py_INIT_FAILED(err));
if (_Py_INIT_IS_EXIT(err)) { if (_Py_INIT_IS_EXIT(err)) {
#ifdef MS_WINDOWS
ExitProcess(err.exitcode);
#else
exit(err.exitcode); exit(err.exitcode);
#endif }
else if (_Py_INIT_IS_ERROR(err)) {
fatal_error(err._func, err.err_msg, 1);
} }
else { else {
assert(_Py_INIT_IS_ERROR(err)); Py_FatalError("_Py_ExitInitError() must not be called on success");
fatal_error(err._func, err.err_msg, 1);
} }
} }