Skip to content

Commit b697d8c

Browse files
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.
1 parent 3c6d2d1 commit b697d8c

File tree

4 files changed

+81
-58
lines changed

4 files changed

+81
-58
lines changed

Include/internal/pycore_crossinterp.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern "C" {
1111
#include "pycore_lock.h" // PyMutex
1212
#include "pycore_pyerrors.h"
1313

14+
1415
/**************/
1516
/* exceptions */
1617
/**************/
@@ -163,8 +164,13 @@ struct _xi_state {
163164
// heap types
164165
_PyXIData_lookup_t data_lookup;
165166

166-
// heap types
167-
PyObject *PyExc_NotShareableError;
167+
struct xi_exceptions {
168+
// static types
169+
PyObject *PyExc_InterpreterError;
170+
PyObject *PyExc_InterpreterNotFoundError;
171+
// heap types
172+
PyObject *PyExc_NotShareableError;
173+
} exceptions;
168174
};
169175

170176
extern PyStatus _PyXI_Init(PyInterpreterState *interp);

Modules/_interpretersmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ module_exec(PyObject *mod)
15071507
goto error;
15081508
}
15091509
PyObject *PyExc_NotShareableError = \
1510-
_PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError;
1510+
_PyInterpreterState_GetXIState(interp)->exceptions.PyExc_NotShareableError;
15111511
if (PyModule_AddType(mod, (PyTypeObject *)PyExc_NotShareableError) < 0) {
15121512
goto error;
15131513
}

Python/crossinterp.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
/* exceptions */
1818
/**************/
1919

20-
static int init_exceptions(PyInterpreterState *);
21-
static void fini_exceptions(PyInterpreterState *);
22-
static int _init_not_shareable_error_type(PyInterpreterState *);
23-
static void _fini_not_shareable_error_type(PyInterpreterState *);
24-
static PyObject * _get_not_shareable_error_type(PyInterpreterState *);
20+
typedef struct xi_exceptions exceptions_t;
21+
static int init_static_exctypes(exceptions_t *, PyInterpreterState *);
22+
static void fini_static_exctypes(exceptions_t *, PyInterpreterState *);
23+
static int init_heap_exctypes(exceptions_t *);
24+
static void fini_heap_exctypes(exceptions_t *);
2525
#include "crossinterp_exceptions.h"
2626

2727

@@ -205,7 +205,8 @@ static inline void
205205
_set_xid_lookup_failure(PyInterpreterState *interp,
206206
PyObject *obj, const char *msg)
207207
{
208-
PyObject *exctype = _get_not_shareable_error_type(interp);
208+
exceptions_t *state = &_PyInterpreterState_GetXIState(interp)->exceptions;
209+
PyObject *exctype = state->PyExc_NotShareableError;
209210
assert(exctype != NULL);
210211
if (msg != NULL) {
211212
assert(obj == NULL);
@@ -1605,7 +1606,9 @@ _propagate_not_shareable_error(_PyXI_session *session)
16051606
return;
16061607
}
16071608
PyInterpreterState *interp = PyInterpreterState_Get();
1608-
if (PyErr_ExceptionMatches(_get_not_shareable_error_type(interp))) {
1609+
exceptions_t *state = &_PyInterpreterState_GetXIState(interp)->exceptions;
1610+
assert(state->PyExc_NotShareableError != NULL);
1611+
if (PyErr_ExceptionMatches(state->PyExc_NotShareableError)) {
16091612
// We want to propagate the exception directly.
16101613
session->_error_override = _PyXI_ERR_NOT_SHAREABLE;
16111614
session->error_override = &session->_error_override;
@@ -1782,9 +1785,11 @@ _PyXI_Init(PyInterpreterState *interp)
17821785
}
17831786
xid_lookup_init(&_PyXI_GET_STATE(interp)->data_lookup);
17841787

1785-
// Initialize exceptions (heap types).
1786-
if (_init_not_shareable_error_type(interp) < 0) {
1787-
return _PyStatus_ERR("failed to initialize NotShareableError");
1788+
// Initialize exceptions.(heap types).
1789+
// See _PyXI_InitTypes() for the static types.
1790+
if (init_heap_exctypes(&_PyXI_GET_STATE(interp)->exceptions) < 0) {
1791+
PyErr_PrintEx(0);
1792+
return _PyStatus_ERR("failed to initialize exceptions");
17881793
}
17891794

17901795
return _PyStatus_OK();
@@ -1797,7 +1802,8 @@ void
17971802
_PyXI_Fini(PyInterpreterState *interp)
17981803
{
17991804
// Finalize exceptions (heap types).
1800-
_fini_not_shareable_error_type(interp);
1805+
// See _PyXI_FiniTypes() for the static types.
1806+
fini_heap_exctypes(&_PyXI_GET_STATE(interp)->exceptions);
18011807

18021808
// Finalize the XID lookup state (e.g. registry).
18031809
xid_lookup_fini(&_PyXI_GET_STATE(interp)->data_lookup);
@@ -1809,17 +1815,21 @@ _PyXI_Fini(PyInterpreterState *interp)
18091815
PyStatus
18101816
_PyXI_InitTypes(PyInterpreterState *interp)
18111817
{
1812-
if (init_exceptions(interp) < 0) {
1818+
if (init_static_exctypes(&_PyXI_GET_STATE(interp)->exceptions, interp) < 0) {
18131819
PyErr_PrintEx(0);
18141820
return _PyStatus_ERR("failed to initialize an exception type");
18151821
}
1822+
// We would initialize heap types here too but that leads to ref leaks.
1823+
// Instead, we intialize them in _PyXI_Init().
18161824
return _PyStatus_OK();
18171825
}
18181826

18191827
void
18201828
_PyXI_FiniTypes(PyInterpreterState *interp)
18211829
{
1822-
fini_exceptions(interp);
1830+
// We would finalize heap types here too but that leads to ref leaks.
1831+
// Instead, we finalize them in _PyXI_Fini().
1832+
fini_static_exctypes(&_PyXI_GET_STATE(interp)->exceptions, interp);
18231833
}
18241834

18251835

Python/crossinterp_exceptions.h

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,71 +25,78 @@ static PyTypeObject _PyExc_InterpreterNotFoundError = {
2525
};
2626
PyObject *PyExc_InterpreterNotFoundError = (PyObject *)&_PyExc_InterpreterNotFoundError;
2727

28-
/* NotShareableError extends ValueError */
29-
30-
static int
31-
_init_not_shareable_error_type(PyInterpreterState *interp)
32-
{
33-
const char *name = "interpreters.NotShareableError";
34-
PyObject *base = PyExc_ValueError;
35-
PyObject *ns = NULL;
36-
PyObject *exctype = PyErr_NewException(name, base, ns);
37-
if (exctype == NULL) {
38-
return -1;
39-
}
40-
41-
_PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError = exctype;
42-
return 0;
43-
}
44-
45-
static void
46-
_fini_not_shareable_error_type(PyInterpreterState *interp)
47-
{
48-
Py_CLEAR(_PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError);
49-
}
50-
51-
static PyObject *
52-
_get_not_shareable_error_type(PyInterpreterState *interp)
53-
{
54-
assert(_PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError != NULL);
55-
return _PyInterpreterState_GetXIState(interp)->PyExc_NotShareableError;
56-
}
57-
5828

5929
/* lifecycle */
6030

6131
static int
62-
init_exceptions(PyInterpreterState *interp)
32+
init_static_exctypes(exceptions_t *state, PyInterpreterState *interp)
6333
{
34+
assert(state == &_PyXI_GET_STATE(interp)->exceptions);
6435
PyTypeObject *base = (PyTypeObject *)PyExc_Exception;
6536

66-
// builtin static types
67-
37+
// PyExc_InterpreterError
6838
_PyExc_InterpreterError.tp_base = base;
6939
_PyExc_InterpreterError.tp_traverse = base->tp_traverse;
7040
_PyExc_InterpreterError.tp_clear = base->tp_clear;
7141
if (_PyStaticType_InitBuiltin(interp, &_PyExc_InterpreterError) < 0) {
72-
return -1;
42+
goto error;
7343
}
44+
state->PyExc_InterpreterError = (PyObject *)&_PyExc_InterpreterError;
7445

46+
// PyExc_InterpreterNotFoundError
7547
_PyExc_InterpreterNotFoundError.tp_traverse = base->tp_traverse;
7648
_PyExc_InterpreterNotFoundError.tp_clear = base->tp_clear;
7749
if (_PyStaticType_InitBuiltin(interp, &_PyExc_InterpreterNotFoundError) < 0) {
78-
return -1;
50+
goto error;
7951
}
52+
state->PyExc_InterpreterNotFoundError =
53+
(PyObject *)&_PyExc_InterpreterNotFoundError;
8054

81-
// heap types
55+
return 0;
8256

83-
// We would call _init_not_shareable_error_type() here too,
84-
// but that leads to ref leaks
57+
error:
58+
fini_static_exctypes(state, interp);
59+
return -1;
60+
}
61+
62+
static void
63+
fini_static_exctypes(exceptions_t *state, PyInterpreterState *interp)
64+
{
65+
assert(state == &_PyXI_GET_STATE(interp)->exceptions);
66+
if (state->PyExc_InterpreterNotFoundError != NULL) {
67+
state->PyExc_InterpreterNotFoundError = NULL;
68+
_PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterNotFoundError);
69+
}
70+
if (state->PyExc_InterpreterError != NULL) {
71+
state->PyExc_InterpreterError = NULL;
72+
_PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterError);
73+
}
74+
}
75+
76+
static int
77+
init_heap_exctypes(exceptions_t *state)
78+
{
79+
PyObject *exctype;
80+
81+
/* NotShareableError extends ValueError */
82+
const char *name = "interpreters.NotShareableError";
83+
PyObject *base = PyExc_ValueError;
84+
PyObject *ns = NULL;
85+
exctype = PyErr_NewException(name, base, ns);
86+
if (exctype == NULL) {
87+
goto error;
88+
}
89+
state->PyExc_NotShareableError = exctype;
8590

8691
return 0;
92+
93+
error:
94+
fini_heap_exctypes(state);
95+
return -1;
8796
}
8897

8998
static void
90-
fini_exceptions(PyInterpreterState *interp)
99+
fini_heap_exctypes(exceptions_t *state)
91100
{
92-
// Likewise with _fini_not_shareable_error_type().
93-
_PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterNotFoundError);
94-
_PyStaticType_FiniBuiltin(interp, &_PyExc_InterpreterError);
101+
Py_CLEAR(state->PyExc_NotShareableError);
95102
}

0 commit comments

Comments
 (0)