diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2024-04-03 16:58:39 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-03 16:58:39 (GMT) |
commit | 976bcb2379709da57073a9e07b518ff51daa617a (patch) | |
tree | 66fca9084cbae6c0b503491f627829d703df19bc /Python | |
parent | 7ecd55d604a8fa287c1d131cac14d10260be826b (diff) | |
download | cpython-976bcb2379709da57073a9e07b518ff51daa617a.zip cpython-976bcb2379709da57073a9e07b518ff51daa617a.tar.gz cpython-976bcb2379709da57073a9e07b518ff51daa617a.tar.bz2 |
gh-76785: Raise InterpreterError, Not RuntimeError (gh-117489)
I had meant to switch everything to InterpreterError when I added it a while back. At the time I missed a few key spots.
As part of this, I've added print-the-exception to _PyXI_InitTypes() and fixed an error case in `_PyStaticType_InitBuiltin().
Diffstat (limited to 'Python')
-rw-r--r-- | Python/crossinterp.c | 9 | ||||
-rw-r--r-- | Python/crossinterp_exceptions.h | 17 | ||||
-rw-r--r-- | Python/pystate.c | 2 |
3 files changed, 22 insertions, 6 deletions
diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 18dec4d..16efe9c 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -845,7 +845,7 @@ _PyXI_ApplyErrorCode(_PyXI_errcode code, PyInterpreterState *interp) return 0; case _PyXI_ERR_OTHER: // XXX msg? - PyErr_SetNone(PyExc_RuntimeError); + PyErr_SetNone(PyExc_InterpreterError); break; case _PyXI_ERR_NO_MEMORY: PyErr_NoMemory(); @@ -856,11 +856,11 @@ _PyXI_ApplyErrorCode(_PyXI_errcode code, PyInterpreterState *interp) _PyInterpreterState_FailIfRunningMain(interp); break; case _PyXI_ERR_MAIN_NS_FAILURE: - PyErr_SetString(PyExc_RuntimeError, + PyErr_SetString(PyExc_InterpreterError, "failed to get __main__ namespace"); break; case _PyXI_ERR_APPLY_NS_FAILURE: - PyErr_SetString(PyExc_RuntimeError, + PyErr_SetString(PyExc_InterpreterError, "failed to apply namespace to __main__"); break; case _PyXI_ERR_NOT_SHAREABLE: @@ -935,7 +935,7 @@ _PyXI_ApplyError(_PyXI_error *error) if (error->uncaught.type.name != NULL || error->uncaught.msg != NULL) { // __context__ will be set to a proxy of the propagated exception. PyObject *exc = PyErr_GetRaisedException(); - _PyXI_excinfo_Apply(&error->uncaught, PyExc_RuntimeError); + _PyXI_excinfo_Apply(&error->uncaught, PyExc_InterpreterError); PyObject *exc2 = PyErr_GetRaisedException(); PyException_SetContext(exc, exc2); PyErr_SetRaisedException(exc); @@ -1671,6 +1671,7 @@ PyStatus _PyXI_InitTypes(PyInterpreterState *interp) { if (init_exceptions(interp) < 0) { + PyErr_PrintEx(0); return _PyStatus_ERR("failed to initialize an exception type"); } return _PyStatus_OK(); diff --git a/Python/crossinterp_exceptions.h b/Python/crossinterp_exceptions.h index e418cf9..0f324ba 100644 --- a/Python/crossinterp_exceptions.h +++ b/Python/crossinterp_exceptions.h @@ -5,6 +5,9 @@ static PyTypeObject _PyExc_InterpreterError = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "interpreters.InterpreterError", .tp_doc = PyDoc_STR("A cross-interpreter operation failed"), + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, + //.tp_traverse = ((PyTypeObject *)PyExc_BaseException)->tp_traverse, + //.tp_clear = ((PyTypeObject *)PyExc_BaseException)->tp_clear, //.tp_base = (PyTypeObject *)PyExc_BaseException, }; PyObject *PyExc_InterpreterError = (PyObject *)&_PyExc_InterpreterError; @@ -15,6 +18,9 @@ static PyTypeObject _PyExc_InterpreterNotFoundError = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "interpreters.InterpreterNotFoundError", .tp_doc = PyDoc_STR("An interpreter was not found"), + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, + //.tp_traverse = ((PyTypeObject *)PyExc_BaseException)->tp_traverse, + //.tp_clear = ((PyTypeObject *)PyExc_BaseException)->tp_clear, .tp_base = &_PyExc_InterpreterError, }; PyObject *PyExc_InterpreterNotFoundError = (PyObject *)&_PyExc_InterpreterNotFoundError; @@ -55,16 +61,25 @@ _get_not_shareable_error_type(PyInterpreterState *interp) static int init_exceptions(PyInterpreterState *interp) { + PyTypeObject *base = (PyTypeObject *)PyExc_BaseException; + // builtin static types - _PyExc_InterpreterError.tp_base = (PyTypeObject *)PyExc_BaseException; + + _PyExc_InterpreterError.tp_base = base; + _PyExc_InterpreterError.tp_traverse = base->tp_traverse; + _PyExc_InterpreterError.tp_clear = base->tp_clear; if (_PyStaticType_InitBuiltin(interp, &_PyExc_InterpreterError) < 0) { return -1; } + + _PyExc_InterpreterNotFoundError.tp_traverse = base->tp_traverse; + _PyExc_InterpreterNotFoundError.tp_clear = base->tp_clear; if (_PyStaticType_InitBuiltin(interp, &_PyExc_InterpreterNotFoundError) < 0) { return -1; } // heap types + // We would call _init_not_shareable_error_type() here too, // but that leads to ref leaks diff --git a/Python/pystate.c b/Python/pystate.c index 925d1cf..892e740 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1073,7 +1073,7 @@ int _PyInterpreterState_FailIfRunningMain(PyInterpreterState *interp) { if (interp->threads.main != NULL) { - PyErr_SetString(PyExc_RuntimeError, + PyErr_SetString(PyExc_InterpreterError, "interpreter already running"); return -1; } |