@@ -93,38 +93,6 @@ API.. The module does not create any objects that are shared globally.
93
93
PyMem_RawFree(VAR)
94
94
95
95
96
- struct xid_class_registry {
97
- size_t count ;
98
- #define MAX_XID_CLASSES 5
99
- struct {
100
- PyTypeObject * cls ;
101
- } added [MAX_XID_CLASSES ];
102
- };
103
-
104
- static int
105
- register_xid_class (PyTypeObject * cls , crossinterpdatafunc shared ,
106
- struct xid_class_registry * classes )
107
- {
108
- int res = ensure_xid_class (cls , shared );
109
- if (res == 0 ) {
110
- assert (classes -> count < MAX_XID_CLASSES );
111
- // The class has refs elsewhere, so we need to incref here.
112
- classes -> added [classes -> count ].cls = cls ;
113
- classes -> count += 1 ;
114
- }
115
- return res ;
116
- }
117
-
118
- static void
119
- clear_xid_class_registry (struct xid_class_registry * classes )
120
- {
121
- while (classes -> count > 0 ) {
122
- classes -> count -= 1 ;
123
- PyTypeObject * cls = classes -> added [classes -> count ].cls ;
124
- _PyCrossInterpreterData_UnregisterClass (cls );
125
- }
126
- }
127
-
128
96
#define XID_IGNORE_EXC 1
129
97
#define XID_FREE 2
130
98
@@ -223,28 +191,6 @@ add_new_exception(PyObject *mod, const char *name, PyObject *base)
223
191
#define ADD_NEW_EXCEPTION (MOD , NAME , BASE ) \
224
192
add_new_exception(MOD, MODULE_NAME_STR "." Py_STRINGIFY(NAME), BASE)
225
193
226
- static PyTypeObject *
227
- add_new_type (PyObject * mod , PyType_Spec * spec , crossinterpdatafunc shared ,
228
- struct xid_class_registry * classes )
229
- {
230
- PyTypeObject * cls = (PyTypeObject * )PyType_FromModuleAndSpec (
231
- mod , spec , NULL );
232
- if (cls == NULL ) {
233
- return NULL ;
234
- }
235
- if (PyModule_AddType (mod , cls ) < 0 ) {
236
- Py_DECREF (cls );
237
- return NULL ;
238
- }
239
- if (shared != NULL ) {
240
- if (register_xid_class (cls , shared , classes )) {
241
- Py_DECREF (cls );
242
- return NULL ;
243
- }
244
- }
245
- return cls ;
246
- }
247
-
248
194
static int
249
195
wait_for_lock (PyThread_type_lock mutex , PY_TIMEOUT_T timeout )
250
196
{
@@ -269,8 +215,6 @@ wait_for_lock(PyThread_type_lock mutex, PY_TIMEOUT_T timeout)
269
215
/* module state *************************************************************/
270
216
271
217
typedef struct {
272
- struct xid_class_registry xid_classes ;
273
-
274
218
/* Added at runtime by interpreters module. */
275
219
PyTypeObject * send_channel_type ;
276
220
PyTypeObject * recv_channel_type ;
@@ -332,19 +276,33 @@ traverse_module_state(module_state *state, visitproc visit, void *arg)
332
276
return 0 ;
333
277
}
334
278
335
- static int
336
- clear_module_state (module_state * state )
279
+ static void
280
+ clear_xid_types (module_state * state )
337
281
{
338
282
/* external types */
339
- Py_CLEAR (state -> send_channel_type );
340
- Py_CLEAR (state -> recv_channel_type );
283
+ if (state -> send_channel_type != NULL ) {
284
+ (void )_PyCrossInterpreterData_UnregisterClass (state -> send_channel_type );
285
+ Py_CLEAR (state -> send_channel_type );
286
+ }
287
+ if (state -> recv_channel_type != NULL ) {
288
+ (void )_PyCrossInterpreterData_UnregisterClass (state -> recv_channel_type );
289
+ Py_CLEAR (state -> recv_channel_type );
290
+ }
341
291
342
292
/* heap types */
343
- Py_CLEAR (state -> ChannelInfoType );
344
293
if (state -> ChannelIDType != NULL ) {
345
294
(void )_PyCrossInterpreterData_UnregisterClass (state -> ChannelIDType );
295
+ Py_CLEAR (state -> ChannelIDType );
346
296
}
347
- Py_CLEAR (state -> ChannelIDType );
297
+ }
298
+
299
+ static int
300
+ clear_module_state (module_state * state )
301
+ {
302
+ clear_xid_types (state );
303
+
304
+ /* heap types */
305
+ Py_CLEAR (state -> ChannelInfoType );
348
306
349
307
/* exceptions */
350
308
Py_CLEAR (state -> ChannelError );
@@ -2614,6 +2572,25 @@ static PyType_Spec channelid_typespec = {
2614
2572
.slots = channelid_typeslots ,
2615
2573
};
2616
2574
2575
+ static PyTypeObject *
2576
+ add_channelid_type (PyObject * mod )
2577
+ {
2578
+ PyTypeObject * cls = (PyTypeObject * )PyType_FromModuleAndSpec (
2579
+ mod , & channelid_typespec , NULL );
2580
+ if (cls == NULL ) {
2581
+ return NULL ;
2582
+ }
2583
+ if (PyModule_AddType (mod , cls ) < 0 ) {
2584
+ Py_DECREF (cls );
2585
+ return NULL ;
2586
+ }
2587
+ if (ensure_xid_class (cls , _channelid_shared ) < 0 ) {
2588
+ Py_DECREF (cls );
2589
+ return NULL ;
2590
+ }
2591
+ return cls ;
2592
+ }
2593
+
2617
2594
2618
2595
/* SendChannel and RecvChannel classes */
2619
2596
@@ -2697,7 +2674,6 @@ set_channelend_types(PyObject *mod, PyTypeObject *send, PyTypeObject *recv)
2697
2674
if (state == NULL ) {
2698
2675
return -1 ;
2699
2676
}
2700
- struct xid_class_registry * xid_classes = & state -> xid_classes ;
2701
2677
2702
2678
if (state -> send_channel_type != NULL
2703
2679
|| state -> recv_channel_type != NULL )
@@ -2707,11 +2683,15 @@ set_channelend_types(PyObject *mod, PyTypeObject *send, PyTypeObject *recv)
2707
2683
}
2708
2684
state -> send_channel_type = (PyTypeObject * )Py_NewRef (send );
2709
2685
state -> recv_channel_type = (PyTypeObject * )Py_NewRef (recv );
2710
-
2711
- if (register_xid_class (send , _channelend_shared , xid_classes )) {
2686
+ if (ensure_xid_class (send , _channelend_shared ) < 0 ) {
2687
+ Py_CLEAR (state -> send_channel_type );
2688
+ Py_CLEAR (state -> recv_channel_type );
2712
2689
return -1 ;
2713
2690
}
2714
- if (register_xid_class (recv , _channelend_shared , xid_classes )) {
2691
+ if (ensure_xid_class (recv , _channelend_shared ) < 0 ) {
2692
+ (void )_PyCrossInterpreterData_UnregisterClass (state -> send_channel_type );
2693
+ Py_CLEAR (state -> send_channel_type );
2694
+ Py_CLEAR (state -> recv_channel_type );
2715
2695
return -1 ;
2716
2696
}
2717
2697
@@ -3294,13 +3274,11 @@ module_exec(PyObject *mod)
3294
3274
if (_globals_init () != 0 ) {
3295
3275
return -1 ;
3296
3276
}
3297
- struct xid_class_registry * xid_classes = NULL ;
3298
3277
3299
3278
module_state * state = get_module_state (mod );
3300
3279
if (state == NULL ) {
3301
3280
goto error ;
3302
3281
}
3303
- xid_classes = & state -> xid_classes ;
3304
3282
3305
3283
/* Add exception types */
3306
3284
if (exceptions_init (mod ) != 0 ) {
@@ -3319,8 +3297,7 @@ module_exec(PyObject *mod)
3319
3297
}
3320
3298
3321
3299
// ChannelID
3322
- state -> ChannelIDType = add_new_type (
3323
- mod , & channelid_typespec , _channelid_shared , xid_classes );
3300
+ state -> ChannelIDType = add_channelid_type (mod );
3324
3301
if (state -> ChannelIDType == NULL ) {
3325
3302
goto error ;
3326
3303
}
@@ -3332,8 +3309,8 @@ module_exec(PyObject *mod)
3332
3309
return 0 ;
3333
3310
3334
3311
error :
3335
- if (xid_classes != NULL ) {
3336
- clear_xid_class_registry ( xid_classes );
3312
+ if (state != NULL ) {
3313
+ clear_xid_types ( state );
3337
3314
}
3338
3315
_globals_fini ();
3339
3316
return -1 ;
@@ -3360,9 +3337,6 @@ module_clear(PyObject *mod)
3360
3337
module_state * state = get_module_state (mod );
3361
3338
assert (state != NULL );
3362
3339
3363
- // Before clearing anything, we unregister the various XID types. */
3364
- clear_xid_class_registry (& state -> xid_classes );
3365
-
3366
3340
// Now we clear the module state.
3367
3341
clear_module_state (state );
3368
3342
return 0 ;
@@ -3374,9 +3348,6 @@ module_free(void *mod)
3374
3348
module_state * state = get_module_state (mod );
3375
3349
assert (state != NULL );
3376
3350
3377
- // Before clearing anything, we unregister the various XID types. */
3378
- clear_xid_class_registry (& state -> xid_classes );
3379
-
3380
3351
// Now we clear the module state.
3381
3352
clear_module_state (state );
3382
3353
0 commit comments