@@ -322,17 +322,29 @@ static PyMemberDef union_members[] = {
322
322
{0 }
323
323
};
324
324
325
- static PyObject *
326
- union_getitem (PyObject * self , PyObject * item )
325
+ // Populate __parameters__ if needed.
326
+ static int
327
+ union_init_parameters (unionobject * alias )
327
328
{
328
- unionobject * alias = ( unionobject * ) self ;
329
- // Populate __parameters__ if needed.
329
+ int result = 0 ;
330
+ Py_BEGIN_CRITICAL_SECTION ( alias );
330
331
if (alias -> parameters == NULL ) {
331
332
alias -> parameters = _Py_make_parameters (alias -> args );
332
333
if (alias -> parameters == NULL ) {
333
- return NULL ;
334
+ result = -1 ;
334
335
}
335
336
}
337
+ Py_END_CRITICAL_SECTION ();
338
+ return result ;
339
+ }
340
+
341
+ static PyObject *
342
+ union_getitem (PyObject * self , PyObject * item )
343
+ {
344
+ unionobject * alias = (unionobject * )self ;
345
+ if (union_init_parameters (alias ) < 0 ) {
346
+ return NULL ;
347
+ }
336
348
337
349
PyObject * newargs = _Py_subs_parameters (self , alias -> args , alias -> parameters , item );
338
350
if (newargs == NULL ) {
@@ -352,11 +364,8 @@ static PyObject *
352
364
union_parameters (PyObject * self , void * Py_UNUSED (unused ))
353
365
{
354
366
unionobject * alias = (unionobject * )self ;
355
- if (alias -> parameters == NULL ) {
356
- alias -> parameters = _Py_make_parameters (alias -> args );
357
- if (alias -> parameters == NULL ) {
358
- return NULL ;
359
- }
367
+ if (union_init_parameters (alias ) < 0 ) {
368
+ return NULL ;
360
369
}
361
370
return Py_NewRef (alias -> parameters );
362
371
}
0 commit comments