[Python-checkins] [3.10] gh-97943: PyFunction_GetAnnotations should return a borrowed reference. (GH-97949) (GH-97989)

ambv webhook-mailer at python.org
Thu Oct 6 20:59:15 EDT 2022


https://github.com/python/cpython/commit/e0e303abe426d554b9e4713722b0cc20818d5bfb
commit: e0e303abe426d554b9e4713722b0cc20818d5bfb
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2022-10-06T17:59:09-07:00
summary:

[3.10] gh-97943: PyFunction_GetAnnotations should return a borrowed reference. (GH-97949) (GH-97989)

gh-97943: PyFunction_GetAnnotations should return a borrowed reference. (GH-97949)
(cherry picked from commit 6bfb0be80486c614cd60dce44c9fe7b3e6c76e3b)

Co-authored-by: larryhastings <larry at hastings.org>

files:
A Misc/NEWS.d/next/Core and Builtins/2022-10-05-17-02-22.gh-issue-97943.LYAWlE.rst
M Objects/funcobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-10-05-17-02-22.gh-issue-97943.LYAWlE.rst b/Misc/NEWS.d/next/Core and Builtins/2022-10-05-17-02-22.gh-issue-97943.LYAWlE.rst
new file mode 100644
index 000000000000..9b4a421a9d47
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-10-05-17-02-22.gh-issue-97943.LYAWlE.rst	
@@ -0,0 +1,2 @@
+Bugfix: :func:`PyFunction_GetAnnotations` should return a borrowed
+reference. It was returning a new reference.
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 801478ade22f..eaf733392466 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -247,7 +247,6 @@ func_get_annotation_dict(PyFunctionObject *op)
         }
         Py_SETREF(op->func_annotations, ann_dict);
     }
-    Py_INCREF(op->func_annotations);
     assert(PyDict_Check(op->func_annotations));
     return op->func_annotations;
 }
@@ -474,7 +473,11 @@ func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
         if (op->func_annotations == NULL)
             return NULL;
     }
-    return func_get_annotation_dict(op);
+    PyObject *d = func_get_annotation_dict(op);
+    if (d) {
+        Py_INCREF(d);
+    }
+    return d;
 }
 
 static int



More information about the Python-checkins mailing list