summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2019-05-16 14:39:26 (GMT)
committerGitHub <noreply@github.com>2019-05-16 14:39:26 (GMT)
commitdbacfc227381fbc7b3c886ea0bd7806ab3dc62c2 (patch)
tree7669139306b262719531d501001f417fb113f17b /Modules
parent6e7890028213b30939327e7cf885bf097fc14472 (diff)
downloadcpython-dbacfc227381fbc7b3c886ea0bd7806ab3dc62c2.zip
cpython-dbacfc227381fbc7b3c886ea0bd7806ab3dc62c2.tar.gz
cpython-dbacfc227381fbc7b3c886ea0bd7806ab3dc62c2.tar.bz2
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.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/main.c48
1 files changed, 22 insertions, 26 deletions
diff --git a/Modules/main.c b/Modules/main.c
index 0f99e2a..47d0574 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -130,7 +130,7 @@ pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
if (sysdict != NULL) {
sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
if (sys_path == NULL && PyErr_Occurred()) {
- goto error;
+ return -1;
}
}
else {
@@ -138,17 +138,13 @@ pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
}
if (sys_path == NULL) {
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
- goto error;
+ return -1;
}
if (PyList_Insert(sys_path, 0, path0)) {
- goto error;
+ return -1;
}
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)
{
- _PyInitError err;
-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
/* pymain_run_stdin() modify the config */
_PyCoreConfig *config = &interp->core_config;
@@ -464,22 +458,20 @@ pymain_run_python(int *exitcode)
if (main_importer_path != NULL) {
if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
- err = _Py_INIT_EXIT(1);
- goto done;
+ goto error;
}
}
else if (!config->isolated) {
PyObject *path0 = NULL;
- if (_PyPathConfig_ComputeSysPath0(&config->argv, &path0)) {
- if (path0 == NULL) {
- err = _Py_INIT_NO_MEMORY();
- goto done;
- }
+ int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0);
+ if (res < 0) {
+ goto error;
+ }
+ if (res > 0) {
if (pymain_sys_path_add_path0(interp, path0) < 0) {
Py_DECREF(path0);
- err = _Py_INIT_EXIT(1);
- goto done;
+ goto error;
}
Py_DECREF(path0);
}
@@ -508,11 +500,14 @@ pymain_run_python(int *exitcode)
}
pymain_repl(config, &cf, exitcode);
- err = _Py_INIT_OK();
+ goto done;
+
+error:
+ PyErr_Print();
+ *exitcode = 1;
done:
Py_XDECREF(main_importer_path);
- return err;
}
@@ -578,17 +573,14 @@ _Py_RunMain(void)
{
int exitcode = 0;
- _PyInitError err = pymain_run_python(&exitcode);
- if (_Py_INIT_FAILED(err)) {
- pymain_exit_error(err);
- }
-
+ pymain_run_python(&exitcode);
if (Py_FinalizeEx() < 0) {
/* Value unlikely to be confused with a non-error exit status or
other special meaning */
exitcode = 120;
}
+done:
pymain_free();
if (_Py_UnhandledKeyboardInterrupt) {
@@ -603,6 +595,10 @@ static int
pymain_main(_PyArgv *args)
{
_PyInitError err = pymain_init(args);
+ if (_Py_INIT_IS_EXIT(err)) {
+ pymain_free();
+ return err.exitcode;
+ }
if (_Py_INIT_FAILED(err)) {
pymain_exit_error(err);
}