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)
18091815PyStatus
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
18191827void
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
0 commit comments