@@ -221,14 +221,45 @@ PyFunction_SetClosure(PyObject *op, PyObject *closure)
221
221
return 0 ;
222
222
}
223
223
224
+ static PyObject *
225
+ func_get_annotation_dict (PyFunctionObject * op )
226
+ {
227
+ if (op -> func_annotations == NULL ) {
228
+ return NULL ;
229
+ }
230
+ if (PyTuple_CheckExact (op -> func_annotations )) {
231
+ PyObject * ann_tuple = op -> func_annotations ;
232
+ PyObject * ann_dict = PyDict_New ();
233
+ if (ann_dict == NULL ) {
234
+ return NULL ;
235
+ }
236
+
237
+ assert (PyTuple_GET_SIZE (ann_tuple ) % 2 == 0 );
238
+
239
+ for (Py_ssize_t i = 0 ; i < PyTuple_GET_SIZE (ann_tuple ); i += 2 ) {
240
+ int err = PyDict_SetItem (ann_dict ,
241
+ PyTuple_GET_ITEM (ann_tuple , i ),
242
+ PyTuple_GET_ITEM (ann_tuple , i + 1 ));
243
+
244
+ if (err < 0 ) {
245
+ return NULL ;
246
+ }
247
+ }
248
+ Py_SETREF (op -> func_annotations , ann_dict );
249
+ }
250
+ Py_INCREF (op -> func_annotations );
251
+ assert (PyDict_Check (op -> func_annotations ));
252
+ return op -> func_annotations ;
253
+ }
254
+
224
255
PyObject *
225
256
PyFunction_GetAnnotations (PyObject * op )
226
257
{
227
258
if (!PyFunction_Check (op )) {
228
259
PyErr_BadInternalCall ();
229
260
return NULL ;
230
261
}
231
- return ((PyFunctionObject * ) op ) -> func_annotations ;
262
+ return func_get_annotation_dict ((PyFunctionObject * )op );
232
263
}
233
264
234
265
int
@@ -443,27 +474,7 @@ func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
443
474
if (op -> func_annotations == NULL )
444
475
return NULL ;
445
476
}
446
- if (PyTuple_CheckExact (op -> func_annotations )) {
447
- PyObject * ann_tuple = op -> func_annotations ;
448
- PyObject * ann_dict = PyDict_New ();
449
- if (ann_dict == NULL ) {
450
- return NULL ;
451
- }
452
-
453
- assert (PyTuple_GET_SIZE (ann_tuple ) % 2 == 0 );
454
-
455
- for (Py_ssize_t i = 0 ; i < PyTuple_GET_SIZE (ann_tuple ); i += 2 ) {
456
- int err = PyDict_SetItem (ann_dict ,
457
- PyTuple_GET_ITEM (ann_tuple , i ),
458
- PyTuple_GET_ITEM (ann_tuple , i + 1 ));
459
-
460
- if (err < 0 )
461
- return NULL ;
462
- }
463
- Py_SETREF (op -> func_annotations , ann_dict );
464
- }
465
- Py_INCREF (op -> func_annotations );
466
- return op -> func_annotations ;
477
+ return func_get_annotation_dict (op );
467
478
}
468
479
469
480
static int
0 commit comments