summaryrefslogtreecommitdiffstats
path: root/Python/crossinterp_exceptions.h
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2024-11-11 21:49:41 (GMT)
committerGitHub <noreply@github.com>2024-11-11 21:49:41 (GMT)
commitb697d8c48e5e47c37fdd5dd74de40dfb4d6c0d01 (patch)
tree5a28ddf2d9e6e5c0bac0d18389308e7d37997583 /Python/crossinterp_exceptions.h
parent3c6d2d123004d8e37a2111083e22e2a2c96dffd0 (diff)
downloadcpython-b697d8c48e5e47c37fdd5dd74de40dfb4d6c0d01.zip
cpython-b697d8c48e5e47c37fdd5dd74de40dfb4d6c0d01.tar.gz
cpython-b697d8c48e5e47c37fdd5dd74de40dfb4d6c0d01.tar.bz2
gh-76785: Minor Cleanup of Exception-related Cross-interpreter State (gh-126602)
This change makes it easier to backport the _interpreters, _interpqueues, and _interpchannels modules to Python 3.12.
Diffstat (limited to 'Python/crossinterp_exceptions.h')
-rw-r--r--Python/crossinterp_exceptions.h91
1 files changed, 49 insertions, 42 deletions
diff --git a/Python/crossinterp_exceptions.h b/Python/crossinterp_exceptions.h
index 278511d..3cb45d2 100644
--- a/Python/crossinterp_exceptions.h
+++ b/Python/crossinterp_exceptions.h
@@ -25,71 +25,78 @@ static PyTypeObject _PyExc_InterpreterNotFoundError = {
};
PyObject *PyExc_InterpreterNotFoundError = (PyObject *)&_PyExc_InterpreterNotFoundError;
-/* NotShareableError extends ValueError */
-
-static int
-_init_not_shareable_error_type(PyInterpreterState *interp)
-{
- const char *name = "interpreters.NotShareableError";
- PyObject *base = PyExc_ValueError;
- PyObject *ns = NULL;
- PyObject *exctype = PyErr_NewException(name, base, ns);
- if (exctype == NULL) {
- return -1;
- }
-
- _PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError = exctype;
- return 0;
-}
-
-static void
-_fini_not_shareable_error_type(PyInterpreterState *interp)
-{
- Py_CLEAR(_PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError);
-}
-
-static PyObject *
-_get_not_shareable_error_type(PyInterpreterState *interp)
-{
- assert(_PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError != NULL);
- return _PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError;
-}
-
/* lifecycle */
static int
-init_exceptions(PyInterpreterState *interp)
+init_static_exctypes(exceptions_t *state, PyInterpreterState *interp)
{
+ assert(state == &_PyXI_GET_STATE(interp)->exceptions);
PyTypeObject *base = (PyTypeObject *)PyExc_Exception;
- // builtin static types
-
+ // PyExc_InterpreterError
_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;
+ goto error;
}
+ state->PyExc_InterpreterError = (PyObject *)&_PyExc_InterpreterError;
+ // PyExc_InterpreterNotFoundError
_PyExc_InterpreterNotFoundError.tp_traverse = base->tp_traverse;
_PyExc_InterpreterNotFoundError.tp_clear = base->tp_clear;
if (_PyStaticType_InitBuiltin(interp, &_PyExc_InterpreterNotFoundError) < 0) {
- return -1;
+ goto error;
}
+ state->PyExc_InterpreterNotFoundError =
+ (PyObject *)&_PyExc_InterpreterNotFoundError;
- // heap types
+ return 0;
- // We would call _init_not_shareable_error_type() here too,
- // but that leads to ref leaks
+error:
+ fini_static_exctypes(state, interp);
+ return -1;
+}
+
+static void
+fini_static_exctypes(exceptions_t *state, PyInterpreterState *interp)
+{
+ assert(state == &_PyXI_GET_STATE(interp)->exceptions);
+ if (state->PyExc_InterpreterNotFoundError != NULL) {
+ state->PyExc_InterpreterNotFoundError = NULL;
+ _PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterNotFoundError);
+ }
+ if (state->PyExc_InterpreterError != NULL) {
+ state->PyExc_InterpreterError = NULL;
+ _PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterError);
+ }
+}
+
+static int
+init_heap_exctypes(exceptions_t *state)
+{
+ PyObject *exctype;
+
+ /* NotShareableError extends ValueError */
+ const char *name = "interpreters.NotShareableError";
+ PyObject *base = PyExc_ValueError;
+ PyObject *ns = NULL;
+ exctype = PyErr_NewException(name, base, ns);
+ if (exctype == NULL) {
+ goto error;
+ }
+ state->PyExc_NotShareableError = exctype;
return 0;
+
+error:
+ fini_heap_exctypes(state);
+ return -1;
}
static void
-fini_exceptions(PyInterpreterState *interp)
+fini_heap_exctypes(exceptions_t *state)
{
- // Likewise with _fini_not_shareable_error_type().
- _PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterNotFoundError);
- _PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterError);
+ Py_CLEAR(state->PyExc_NotShareableError);
}