return 0;
}
+static PyObject *
+func_get_annotation_dict(PyFunctionObject *op)
+{
+ if (op->func_annotations == NULL) {
+ return NULL;
+ }
+ if (PyTuple_CheckExact(op->func_annotations)) {
+ PyObject *ann_tuple = op->func_annotations;
+ PyObject *ann_dict = PyDict_New();
+ if (ann_dict == NULL) {
+ return NULL;
+ }
+
+ assert(PyTuple_GET_SIZE(ann_tuple) % 2 == 0);
+
+ for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(ann_tuple); i += 2) {
+ int err = PyDict_SetItem(ann_dict,
+ PyTuple_GET_ITEM(ann_tuple, i),
+ PyTuple_GET_ITEM(ann_tuple, i + 1));
+
+ if (err < 0) {
+ return NULL;
+ }
+ }
+ Py_SETREF(op->func_annotations, ann_dict);
+ }
+ Py_INCREF(op->func_annotations);
+ assert(PyDict_Check(op->func_annotations));
+ return op->func_annotations;
+}
+
PyObject *
PyFunction_GetAnnotations(PyObject *op)
{
PyErr_BadInternalCall();
return NULL;
}
- return ((PyFunctionObject *) op) -> func_annotations;
+ return func_get_annotation_dict((PyFunctionObject *)op);
}
int
if (op->func_annotations == NULL)
return NULL;
}
- if (PyTuple_CheckExact(op->func_annotations)) {
- PyObject *ann_tuple = op->func_annotations;
- PyObject *ann_dict = PyDict_New();
- if (ann_dict == NULL) {
- return NULL;
- }
-
- assert(PyTuple_GET_SIZE(ann_tuple) % 2 == 0);
-
- for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(ann_tuple); i += 2) {
- int err = PyDict_SetItem(ann_dict,
- PyTuple_GET_ITEM(ann_tuple, i),
- PyTuple_GET_ITEM(ann_tuple, i + 1));
-
- if (err < 0)
- return NULL;
- }
- Py_SETREF(op->func_annotations, ann_dict);
- }
- Py_INCREF(op->func_annotations);
- return op->func_annotations;
+ return func_get_annotation_dict(op);
}
static int