Skip to content

[C API] Add Py_NewRef() function to get a new strong reference to an object #86428

Description

@vstinner
BPO 42262
Nosy @vstinner, @erlend-aasland
PRs
  • bpo-42262: Add Py_NewRef() function #23152
  • bpo-42262: Py_NewRef() casts its argument to PyObject* #23626
  • 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 = <Date 2020-11-05.14:11:51.499>
    created_at = <Date 2020-11-04.16:20:34.933>
    labels = ['expert-C-API', '3.10']
    title = '[C API] Add Py_NewRef() function to get a new strong reference to an object'
    updated_at = <Date 2020-12-03.13:01:17.892>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2020-12-03.13:01:17.892>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-11-05.14:11:51.499>
    closer = 'vstinner'
    components = ['C API']
    creation = <Date 2020-11-04.16:20:34.933>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42262
    keywords = ['patch']
    message_count = 4.0
    messages = ['380337', '380339', '380409', '382407']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'hodgestar', 'erlendaasland']
    pr_nums = ['23152', '23626']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue42262'
    versions = ['Python 3.10']

    Activity

    1. vstinner commented on Nov 4, 2020

      @vstinner
      MemberAuthor

      In C, the following pattern (A) is very common:

          Py_INCREF(sysdict);
          interp->sysdict = sysdict;
      • (1) Increment the reference counter
      • (2) Store a reference to the object somewhere

      Similar pattern (B) using return:

          Py_INCREF(temp);
          return temp;

      The problem in these patterns is that the object has to be written twice. I propose to add a simple new Py_NewRef() function which returns a new strong reference. In short, it's just:

      static inline PyObject* Py_NewRef(PyObject *obj)
      {
          Py_INCREF(obj);
          return obj;
      }

      (The actual implementation is just a little bit more complex, to also export it as a regular function.)

      Pattern (A) becomes:

      interp->sysdict = Py_NewRef(sysdict);
      

      Pattern (B) becomes:

      return Py_NewRef(temp);
      

      Py_NewRef() might help to prepare a C extension to be converted to HPy which uses a HPy_Dup() function. HPy_Dup() is different than "Py_INCREF(obj), obj" for subtle reasons. I let you dig into HPy documentation for the rationale:

      https://hpy.readthedocs.io/en/latest/api.html#handles

      Even if you ignore HPy, Py_NewRef() avoids to repeat the object variable, and so makes the code simpler. Simple example:

      -#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
      +#define Py_RETURN_NONE return Py_NewRef(Py_None)

    2. vstinner commented on Nov 4, 2020

      @vstinner
      MemberAuthor

      The problem in these patterns is that the object has to be written twice.

      I'm talking about the variable name which has to be repeated (written twice) in the source code.

    3. vstinner commented on Nov 5, 2020

      @vstinner
      MemberAuthor

      New changeset 53a03aa by Victor Stinner in branch 'master':
      bpo-42262: Add Py_NewRef() and Py_XNewRef() (GH-23152)
      53a03aa

    4. vstinner commented on Dec 3, 2020

      @vstinner
      MemberAuthor

      New changeset 8b6c4a9 by Victor Stinner in branch 'master':
      bpo-42262: Py_NewRef() casts its argument to PyObject* (GH-23626)
      8b6c4a9

    5. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions