Skip to content

Commit 149ed7f

Browse files
larryhastingsmpage
authored andcommitted
pythongh-97943: PyFunction_GetAnnotations should return a borrowed reference. (python#97949)
1 parent af96109 commit 149ed7f

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Bugfix: :func:`PyFunction_GetAnnotations` should return a borrowed
2+
reference. It was returning a new reference.

Objects/funcobject.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ func_get_annotation_dict(PyFunctionObject *op)
340340
}
341341
Py_SETREF(op->func_annotations, ann_dict);
342342
}
343-
Py_INCREF(op->func_annotations);
344343
assert(PyDict_Check(op->func_annotations));
345344
return op->func_annotations;
346345
}
@@ -575,7 +574,11 @@ func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
575574
if (op->func_annotations == NULL)
576575
return NULL;
577576
}
578-
return func_get_annotation_dict(op);
577+
PyObject *d = func_get_annotation_dict(op);
578+
if (d) {
579+
Py_INCREF(d);
580+
}
581+
return d;
579582
}
580583

581584
static int

0 commit comments

Comments
 (0)