GeezerVison is showing github.com — PyFunction_GetAnnotations is probably causing ref leaks · Issue #97943 · python/cpython
PyFunction_GetAnnotations is probably causing ref leaks #97943DescriptionIn issue #30409 PyFunction_GetAnnotations was fixed so it wouldn't return a tuple. That's good.
But this change changed the return value. PyFunction_GetAnnotations now returns a new reference to the annotations dict. But according to the documentation, it should return a borrowed reference.
https://docs.python.org/3/c-api/function.html#c.PyFunction_GetAnnotations
If users only read the documentation, and assume that it's correct, they'll leak a reference to the annotation dict.
We could fix this by changing the documentation. However, all the other PyFunction_Get* functions also return borrowed references. So I think the correct fix is to remove the incref. Activitylarryhastings commented on Oct 5, 2022 ContributorAuthor More actions changed the title [-]PyFunction_GetAnnotations should be causing ref leaks[/-] [+]PyFunction_GetAnnotations is probably causing ref leaks[/+] on Oct 5, 2022larryhastings commented on Oct 5, 2022 ContributorAuthor More actions Also, I consider this enough of a bug fix that it should be backported to versions still getting bug fixes. ericvsmith commented on Oct 6, 2022 I agree that changing back to a borrowed reference to the current version and backporting that behavior it is correct. Many more people would read the documentation and assume it's correct than would read the source code and know that it's a new reference that they need to manage. larryhastings commented on Oct 6, 2022 ContributorAuthor More actions I didn't do a full forensic analysis, but I think what happened here is:
- func_get_annotations (the getter/setter) returns a fresh reference.
- func_get_annotations was changed to call this code that says "oh! if it's a tuple, turn it into a dict." this function returns a new reference.
- PyFunction_GetAnnotations wasn't changed, it was returning the dict.
- the bug fix was, "oh! just have it call the same function to turn it from a tuple into a dict." but this function returns a new reference, and PyFunction_GetAnnotations shouldn't return a new reference.
added a commit that references this issue on Oct 6, 2022gh-97943: PyFunction_GetAnnotations should return a borrowed referenc… added 4 commits that reference this issue on Oct 6, 2022pythongh-97943: PyFunction_GetAnnotations should return a borrowed re… pythongh-97943: PyFunction_GetAnnotations should return a borrowed re… pythongh-97943: PyFunction_GetAnnotations should return a borrowed re… gh-97943: PyFunction_GetAnnotations should return a borrowed referenc… hauntsaninja commented on Oct 28, 2022 Thanks for spotting, looks like it's been fixed and backported
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|