Skip to content

Commit d46f37e

Browse files
committed
gh-97943: PyFunction_GetAnnotations should return a borrowed reference.
It was returning a new reference, which isn't how it used to work, and isn't how it's documented.
1 parent 9fbfa42 commit d46f37e

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
@@ -311,7 +311,6 @@ func_get_annotation_dict(PyFunctionObject *op)
311311
}
312312
Py_SETREF(op->func_annotations, ann_dict);
313313
}
314-
Py_INCREF(op->func_annotations);
315314
assert(PyDict_Check(op->func_annotations));
316315
return op->func_annotations;
317316
}
@@ -543,7 +542,11 @@ func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
543542
if (op->func_annotations == NULL)
544543
return NULL;
545544
}
546-
return func_get_annotation_dict(op);
545+
PyObject *d = func_get_annotation_dict(op);
546+
if (d) {
547+
Py_INCREF(d);
548+
}
549+
return d;
547550
}
548551

549552
static int

0 commit comments

Comments
 (0)