Skip to content

PyWeakref_NewRef docs are misleading #80384

Description

@tekknolagi
mannequin
BPO 36203
Nosy @Windsooon, @tekknolagi, @iritkatriel
PRs
  • bpo-36203: Check callback is callable in PyWeakref_NewRef #12244
  • bpo-36203: Check callback is callable in PyWeakref_NewRef #26273
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2019-03-06.01:03:50.658>
    labels = ['type-bug', '3.9', '3.10', '3.11', 'ctypes', 'docs']
    title = 'PyWeakref_NewRef docs are misleading'
    updated_at = <Date 2021-05-20.20:08:43.797>
    user = 'https://github.com/tekknolagi'

    bugs.python.org fields:

    activity = <Date 2021-05-20.20:08:43.797>
    actor = 'tekknolagi'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation', 'ctypes']
    creation = <Date 2019-03-06.01:03:50.658>
    creator = 'tekknolagi'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36203
    keywords = ['patch']
    message_count = 6.0
    messages = ['337255', '337292', '337321', '337371', '337372', '394045']
    nosy_count = 4.0
    nosy_names = ['docs@python', 'Windson Yang', 'tekknolagi', 'iritkatriel']
    pr_nums = ['12244', '26273']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue36203'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Linked PRs

    Activity

    1. tekknolagi commented on Mar 6, 2019

      tekknolagimannequin
      MannequinAuthor

      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.

    2. Windsooon commented on Mar 6, 2019

      Windsooonmannequin
      Mannequin

      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.

    3. tekknolagi commented on Mar 6, 2019

      tekknolagimannequin
      MannequinAuthor

      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\>


    4. Windsooon commented on Mar 7, 2019

      Windsooonmannequin
      Mannequin

      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.

    5. tekknolagi commented on Mar 7, 2019

      tekknolagimannequin
      MannequinAuthor

      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\>


    6. iritkatriel commented on May 20, 2021

      @iritkatriel
      Member

      Please add unit tests to the patch.

    7. 6 remaining items

    8. added
      3.13only security fixes
      3.14bugs and security fixes
      3.15bugs and security fixes
      3.16new features, bugs and security fixes
      and removed on Jun 5, 2026
    9. added a commit that references this issue on Jun 9, 2026
    10. serhiy-storchaka commented on Jun 9, 2026

      @serhiy-storchaka
      Member

      #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() and PyWeakref_NewProxy().
      #151146 fixes docs for PyWeakref_NewRef() and PyWeakref_NewProxy() in Python 3.15 (will be backported to 3.14 and 3.13).

    11. added 2 commits that reference this issue on Jun 10, 2026
    12. added 2 commits that reference this issue on Jun 10, 2026
    13. added a commit that references this issue on Jun 17, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.13only security fixes3.14bugs and security fixes3.15bugs and security fixes3.16new features, bugs and security fixesdocsDocumentation in the Doc dirtopic-ctypestype-bugAn unexpected behavior, bug, or error

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions