Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
vstinner opened this issue Nov 4, 2020 · 4 comments
Closed
Labels
3.10 only security fixes topic-C-API

Comments

@vstinner
Copy link
Member

vstinner commented Nov 4, 2020

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']

    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 4, 2020

    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)

    @vstinner vstinner added 3.10 only security fixes topic-C-API labels Nov 4, 2020
    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 4, 2020

    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.

    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 5, 2020

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

    @vstinner
    Copy link
    Member Author

    vstinner commented Dec 3, 2020

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

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes topic-C-API
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant