]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-97943: PyFunction_GetAnnotations should return a borrowed reference. (#97949)
authorlarryhastings <larry@hastings.org>
Thu, 6 Oct 2022 19:23:20 +0000 (12:23 -0700)
committerGitHub <noreply@github.com>
Thu, 6 Oct 2022 19:23:20 +0000 (12:23 -0700)
Misc/NEWS.d/next/Core and Builtins/2022-10-05-17-02-22.gh-issue-97943.LYAWlE.rst [new file with mode: 0644]
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 (file)
index 0000000..9b4a421
--- /dev/null
@@ -0,0 +1,2 @@
+Bugfix: :func:`PyFunction_GetAnnotations` should return a borrowed
+reference. It was returning a new reference.
index 7f257a9986987b00fd1768666cced47bb03afd4d..ccc6d0b52eab68885cfd729aeb497314e69b6b61 100644 (file)
@@ -311,7 +311,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;
 }
@@ -543,7 +542,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