This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyWeakref_NewRef docs are misleading
Type: behavior Stage: patch review
Components: ctypes, Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Windson Yang, docs@python, iritkatriel, tekknolagi
Priority: normal Keywords: patch

Created on 2019-03-06 01:03 by tekknolagi, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 12244 closed python-dev, 2019-03-09 00:04
PR 26273 open tekknolagi, 2021-05-20 20:08
Messages (6)
msg337255 - (view) Author: Maxwell Bernstein (tekknolagi) * Date: 2019-03-06 01:03
The docs for `PyWeakref_NewRef` state "if callback is not callable, None, or NULL, this will return NULL and raise TypeError". It does not appear as though there is a callable check for the callback.
msg337292 - (view) Author: Windson Yang (Windson Yang) * Date: 2019-03-06 11:32
Yes, Maxwell. I guess the docs are misleading, the code locate in https://github.com/python/cpython/blob/master/Objects/weakrefobject.c#L748

if (callback == Py_None)
        callback = NULL;
    if (callback == NULL)
        /* return existing weak reference if it exists */
        result = ref;
    if (result != NULL)
        Py_INCREF(result);
    else {
        ...
    }

However, I'm not sure we should fix the docs or the code here.
msg337321 - (view) Author: Maxwell Bernstein (tekknolagi) * Date: 2019-03-06 16:33
NewProxy checks if it's callable, so I suppose the code should be fixed.

On Wed, Mar 6, 2019, 03:32 Windson Yang <report@bugs.python.org> wrote:

>
> Windson Yang <wiwindson@outlook.com> added the comment:
>
> Yes, Maxwell. I guess the docs are misleading, the code locate in
> https://github.com/python/cpython/blob/master/Objects/weakrefobject.c#L748
>
> if (callback == Py_None)
>         callback = NULL;
>     if (callback == NULL)
>         /* return existing weak reference if it exists */
>         result = ref;
>     if (result != NULL)
>         Py_INCREF(result);
>     else {
>         ...
>     }
>
> However, I'm not sure we should fix the docs or the code here.
>
> ----------
> nosy: +Windson Yang
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue36203>
> _______________________________________
>
msg337371 - (view) Author: Windson Yang (Windson Yang) * Date: 2019-03-07 08:20
It looks to me the fix is easy, we just will return NULL and raise TypeError when the callback is not callable, None, or NULL. I'm not an expert in C, but I would love to create a PR for it if you don't have time.
msg337372 - (view) Author: Maxwell Bernstein (tekknolagi) * Date: 2019-03-07 08:23
I can likely do it tomorrow. If not I'll update this.

On Thu, Mar 7, 2019, 00:20 Windson Yang <report@bugs.python.org> wrote:

>
> Windson Yang <wiwindson@outlook.com> added the comment:
>
> It looks to me the fix is easy, we just will return NULL and raise
> TypeError when the callback is not callable, None, or NULL. I'm not an
> expert in C, but I would love to create a PR for it if you don't have time.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue36203>
> _______________________________________
>
msg394045 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-05-20 18:07
Please add unit tests to the patch.
History
Date User Action Args
2022-04-11 14:59:12adminsetgithub: 80384
2021-05-20 20:08:43tekknolagisetpull_requests: + pull_request24879
2021-05-20 18:07:38iritkatrielsetnosy: + iritkatriel

messages: + msg394045
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.6, Python 3.7, Python 3.8
2019-03-09 00:04:19python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request12232
2019-03-07 08:23:58tekknolagisetmessages: + msg337372
2019-03-07 08:20:45Windson Yangsetmessages: + msg337371
2019-03-06 16:33:19tekknolagisetmessages: + msg337321
2019-03-06 11:32:12Windson Yangsetnosy: + Windson Yang
messages: + msg337292
2019-03-06 07:38:12SilentGhostsetassignee: docs@python

type: behavior
components: + Documentation, ctypes
nosy: + docs@python
2019-03-06 01:03:50tekknolagicreate