| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6e78900 commit dbacfc2
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,11 +15,7 @@ typedef struct { | |||
| 15 | 15 | } _type; | |
| 16 | 16 | const char *_func; | |
| 17 | 17 | const char *err_msg; | |
| 18 | - #ifdef MS_WINDOWS | ||
| 19 | - unsigned int exitcode; | ||
| 20 | - #else | ||
| 21 | 18 | int exitcode; | |
| 22 | - #endif | ||
| 23 | 19 | } _PyInitError; | |
| 24 | 20 | ||
| 25 | 21 | /* Almost all errors causing Python initialization to fail */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -130,25 +130,21 @@ pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0) | |||
| 130 | 130 | if (sysdict != NULL) { | |
| 131 | 131 | sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path); | |
| 132 | 132 | if (sys_path == NULL && PyErr_Occurred()) { | |
| 133 | - goto error; | ||
| 133 | + return -1; | ||
| 134 | 134 | } | |
| 135 | 135 | } | |
| 136 | 136 | else { | |
| 137 | 137 | sys_path = NULL; | |
| 138 | 138 | } | |
| 139 | 139 | if (sys_path == NULL) { | |
| 140 | 140 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path"); | |
| 141 | - goto error; | ||
| 141 | + return -1; | ||
| 142 | 142 | } | |
| 143 | 143 | ||
| 144 | 144 | if (PyList_Insert(sys_path, 0, path0)) { | |
| 145 | - goto error; | ||
| 145 | + return -1; | ||
| 146 | 146 | } | |
| 147 | 147 | return 0; | |
| 148 | - | ||
| 149 | - error: | ||
| 150 | - PyErr_Print(); | ||
| 151 | - return -1; | ||
| 152 | 148 | } | |
| 153 | 149 | ||
| 154 | 150 | ||
@@ -443,11 +439,9 @@ pymain_repl(_PyCoreConfig *config, PyCompilerFlags *cf, int *exitcode) | |||
| 443 | 439 | } | |
| 444 | 440 | ||
| 445 | 441 | ||
| 446 | - static _PyInitError | ||
| 442 | + static void | ||
| 447 | 443 | pymain_run_python(int *exitcode) | |
| 448 | 444 | { | |
| 449 | - _PyInitError err; | ||
| 450 | - | ||
| 451 | 445 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); | |
| 452 | 446 | /* pymain_run_stdin() modify the config */ | |
| 453 | 447 | _PyCoreConfig *config = &interp->core_config; | |
@@ -464,22 +458,20 @@ pymain_run_python(int *exitcode) | |||
| 464 | 458 | ||
| 465 | 459 | if (main_importer_path != NULL) { | |
| 466 | 460 | if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) { | |
| 467 | - err = _Py_INIT_EXIT(1); | ||
| 468 | - goto done; | ||
| 461 | + goto error; | ||
| 469 | 462 | } | |
| 470 | 463 | } | |
| 471 | 464 | else if (!config->isolated) { | |
| 472 | 465 | PyObject *path0 = NULL; | |
| 473 | - if (_PyPathConfig_ComputeSysPath0(&config->argv, &path0)) { | ||
| 474 | - if (path0 == NULL) { | ||
| 475 | - err = _Py_INIT_NO_MEMORY(); | ||
| 476 | - goto done; | ||
| 477 | - } | ||
| 466 | + int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0); | ||
| 467 | + if (res < 0) { | ||
| 468 | + goto error; | ||
| 469 | + } | ||
| 478 | 470 | ||
| 471 | + if (res > 0) { | ||
| 479 | 472 | if (pymain_sys_path_add_path0(interp, path0) < 0) { | |
| 480 | 473 | Py_DECREF(path0); | |
| 481 | - err = _Py_INIT_EXIT(1); | ||
| 482 | - goto done; | ||
| 474 | + goto error; | ||
| 483 | 475 | } | |
| 484 | 476 | Py_DECREF(path0); | |
| 485 | 477 | } | |
@@ -508,11 +500,14 @@ pymain_run_python(int *exitcode) | |||
| 508 | 500 | } | |
| 509 | 501 | ||
| 510 | 502 | pymain_repl(config, &cf, exitcode); | |
| 511 | - err = _Py_INIT_OK(); | ||
| 503 | + goto done; | ||
| 504 | + | ||
| 505 | + error: | ||
| 506 | + PyErr_Print(); | ||
| 507 | + *exitcode = 1; | ||
| 512 | 508 | ||
| 513 | 509 | done: | |
| 514 | 510 | Py_XDECREF(main_importer_path); | |
| 515 | - return err; | ||
| 516 | 511 | } | |
| 517 | 512 | ||
| 518 | 513 | ||
@@ -578,17 +573,14 @@ _Py_RunMain(void) | |||
| 578 | 573 | { | |
| 579 | 574 | int exitcode = 0; | |
| 580 | 575 | ||
| 581 | - _PyInitError err = pymain_run_python(&exitcode); | ||
| 582 | - if (_Py_INIT_FAILED(err)) { | ||
| 583 | - pymain_exit_error(err); | ||
| 584 | - } | ||
| 585 | - | ||
| 576 | + pymain_run_python(&exitcode); | ||
| 586 | 577 | if (Py_FinalizeEx() < 0) { | |
| 587 | 578 | /* Value unlikely to be confused with a non-error exit status or | |
| 588 | 579 | other special meaning */ | |
| 589 | 580 | exitcode = 120; | |
| 590 | 581 | } | |
| 591 | 582 | ||
| 583 | + done: | ||
| 592 | 584 | pymain_free(); | |
| 593 | 585 | ||
| 594 | 586 | if (_Py_UnhandledKeyboardInterrupt) { | |
@@ -603,6 +595,10 @@ static int | |||
| 603 | 595 | pymain_main(_PyArgv *args) | |
| 604 | 596 | { | |
| 605 | 597 | _PyInitError err = pymain_init(args); | |
| 598 | + if (_Py_INIT_IS_EXIT(err)) { | ||
| 599 | + pymain_free(); | ||
| 600 | + return err.exitcode; | ||
| 601 | + } | ||
| 606 | 602 | if (_Py_INIT_FAILED(err)) { | |
| 607 | 603 | pymain_exit_error(err); | |
| 608 | 604 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -570,18 +570,17 @@ Py_GetProgramName(void) | |||
| 570 | 570 | directory ("-m module" case) which will be prepended to sys.argv: | |
| 571 | 571 | sys.path[0]. | |
| 572 | 572 | ||
| 573 | - Return 1 if the path is correctly resolved, but *path0_p can be NULL | ||
| 574 | - if the Unicode object fail to be created. | ||
| 573 | + Return 1 if the path is correctly resolved and written into *path0_p. | ||
| 575 | 574 | ||
| 576 | - Return 0 if it fails to resolve the full path (and *path0_p will be NULL). | ||
| 577 | - For example, return 0 if the current working directory has been removed | ||
| 578 | - (bpo-36236) or if argv is empty. | ||
| 575 | + Return 0 if it fails to resolve the full path. For example, return 0 if the | ||
| 576 | + current working directory has been removed (bpo-36236) or if argv is empty. | ||
| 577 | + | ||
| 578 | + Raise an exception and return -1 on error. | ||
| 579 | 579 | */ | |
| 580 | 580 | int | |
| 581 | 581 | _PyPathConfig_ComputeSysPath0(const _PyWstrList *argv, PyObject **path0_p) | |
| 582 | 582 | { | |
| 583 | 583 | assert(_PyWstrList_CheckConsistency(argv)); | |
| 584 | - assert(*path0_p == NULL); | ||
| 585 | 584 | ||
| 586 | 585 | if (argv->length == 0) { | |
| 587 | 586 | /* Leave sys.path unchanged if sys.argv is empty */ | |
@@ -697,7 +696,12 @@ _PyPathConfig_ComputeSysPath0(const _PyWstrList *argv, PyObject **path0_p) | |||
| 697 | 696 | } | |
| 698 | 697 | #endif /* All others */ | |
| 699 | 698 | ||
| 700 | - *path0_p = PyUnicode_FromWideChar(path0, n); | ||
| 699 | + PyObject *path0_obj = PyUnicode_FromWideChar(path0, n); | ||
| 700 | + if (path0_obj == NULL) { | ||
| 701 | + return -1; | ||
| 702 | + } | ||
| 703 | + | ||
| 704 | + *path0_p = path0_obj; | ||
| 701 | 705 | return 1; | |
| 702 | 706 | } | |
| 703 | 707 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2124,18 +2124,15 @@ Py_FatalError(const char *msg) | |||
| 2124 | 2124 | void _Py_NO_RETURN | |
| 2125 | 2125 | _Py_ExitInitError(_PyInitError err) | |
| 2126 | 2126 | { | |
| 2127 | - assert(_Py_INIT_FAILED(err)); | ||
| 2128 | 2127 | if (_Py_INIT_IS_EXIT(err)) { | |
| 2129 | - #ifdef MS_WINDOWS | ||
| 2130 | - ExitProcess(err.exitcode); | ||
| 2131 | - #else | ||
| 2132 | 2128 | exit(err.exitcode); | |
| 2133 | - #endif | ||
| 2134 | 2129 | } | |
| 2135 | - else { | ||
| 2136 | - assert(_Py_INIT_IS_ERROR(err)); | ||
| 2130 | + else if (_Py_INIT_IS_ERROR(err)) { | ||
| 2137 | 2131 | fatal_error(err._func, err.err_msg, 1); | |
| 2138 | 2132 | } | |
| 2133 | + else { | ||
| 2134 | + Py_FatalError("_Py_ExitInitError() must not be called on success"); | ||
| 2135 | + } | ||
| 2139 | 2136 | } | |
| 2140 | 2137 | ||
| 2141 | 2138 | /* Clean up and exit */ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments