Repository navigation
PyWeakref_NewRef docs are misleading #80384
Description
Activity
The docs for
PyWeakref_NewRefstate "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.- addeddocsDocumentation in the Doc dirDocumentation in the Doc dir
on Mar 6, 2019 - addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Mar 6, 2019 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.
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#L748if (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\>
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.
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\>
Please add unit tests to the patch.
- added3.9 (EOL)end of lifeend of life3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes
on May 20, 2021 6 remaining items
- added3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesand removed3.9 (EOL)end of lifeend of life
on Jun 5, 2026 #151130 added a base for new C API tests. Now it is trivial to add new tests.
#151145 adds checks for callback in
weakref.ref(),weakref.proxy(),PyWeakref_NewRef()andPyWeakref_NewProxy().
#151146 fixes docs forPyWeakref_NewRef()andPyWeakref_NewProxy()in Python 3.15 (will be backported to 3.14 and 3.13).
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsTodo
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs