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

Py_(X)SETREF macros #47331

Closed
pitrou opened this issue Jun 11, 2008 · 12 comments
Closed

Py_(X)SETREF macros #47331

pitrou opened this issue Jun 11, 2008 · 12 comments
Labels
extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@pitrou
Copy link
Member

pitrou commented Jun 11, 2008

BPO 3081
Nosy @rhettinger, @jcea, @mdickinson, @abalkin, @pitrou, @benjaminp
Files
  • py_setref.patch
  • py_setref.patch
  • 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 2010-04-08.21:18:04.251>
    created_at = <Date 2008-06-11.19:13:50.975>
    labels = ['extension-modules', 'interpreter-core', 'type-feature']
    title = 'Py_(X)SETREF macros'
    updated_at = <Date 2010-04-08.21:18:04.251>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2010-04-08.21:18:04.251>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-04-08.21:18:04.251>
    closer = 'rhettinger'
    components = ['Extension Modules', 'Interpreter Core']
    creation = <Date 2008-06-11.19:13:50.975>
    creator = 'pitrou'
    dependencies = []
    files = ['10591', '10761']
    hgrepos = []
    issue_num = 3081
    keywords = ['patch']
    message_count = 12.0
    messages = ['68009', '68010', '68012', '68013', '68161', '68898', '68904', '68906', '70798', '102636', '102644', '102645']
    nosy_count = 8.0
    nosy_names = ['rhettinger', 'jcea', 'exarkun', 'mark.dickinson', 'belopolsky', 'pitrou', '_doublep', 'benjamin.peterson']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue3081'
    versions = ['Python 2.6', 'Python 3.0']

    @pitrou
    Copy link
    Member Author

    pitrou commented Jun 11, 2008

    This is an implementation of the Py_SETREF and Py_XSETREF macros
    proposed in http://mail.python.org/pipermail/python-dev/2008-May/079862.html

    As an example, I added a few conversions among the extension modules.

    @pitrou pitrou added extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jun 11, 2008
    @pitrou
    Copy link
    Member Author

    pitrou commented Jun 11, 2008

    FWIW, I also wanted to propose for Py_INCREF(op) to evaluate as (op), so
    that it can be used as return or assignment value, e.g.:
    return Py_INCREF(result);
    or:
    self->var = Py_INCREF(obj);

    but it's perhaps a bit more controversial.

    @benjaminp benjaminp added the type-feature A feature request or enhancement label Jun 11, 2008
    @pitrou
    Copy link
    Member Author

    pitrou commented Jun 11, 2008

    Benjamin, the patch is against py3k, also it might also apply cleanly on
    trunk...

    @benjaminp
    Copy link
    Contributor

    Sorry for the confusion. It seems to me this sort of thing would be
    useful in 2.6, too, so I marked it.

    @abalkin
    Copy link
    Member

    abalkin commented Jun 13, 2008

    A comment on the patch:

    Since object.h may be included from C++ extensions, you should not use a
    C++ keyword "new" as a variable name.

    @pitrou
    Copy link
    Member Author

    pitrou commented Jun 28, 2008

    This new patch avoids using temporary variables named "new", it also
    adopts the "do { ... } while (0)" idiom for definition of the macros.

    @rhettinger
    Copy link
    Contributor

    -1 on the new macros. The mnemonic doesn't work for me and the example
    code fragments are to my eyes less readable than before. These add to
    the learning curve for reading and writing C extensions and provide
    nearly zero benefits.

    Assigning from an INCREF feels weird. It is somewhat at odds with our
    coding style where we tend to stick with dirt simple C, trying to put
    operations on different lines rather than combining too many step in a
    single line.

    @pitrou
    Copy link
    Member Author

    pitrou commented Jun 28, 2008

    Le samedi 28 juin 2008 à 20:12 +0000, Raymond Hettinger a écrit :

    Raymond Hettinger <rhettinger@users.sourceforge.net> added the comment:

    -1 on the new macros. The mnemonic doesn't work for me and the example
    code fragments are to my eyes less readable than before. These add to
    the learning curve for reading and writing C extensions and provide
    nearly zero benefits.

    They might not be ideal, but I think they can be helpful to avoid
    writing incorrect code out of laziness. There is already Py_CLEAR with
    similar purposes.

    Assigning from an INCREF feels weird. It is somewhat at odds with our
    coding style where we tend to stick with dirt simple C, trying to put
    operations on different lines rather than combining too many step in a
    single line.

    Ok.

    @doublep
    Copy link
    Mannequin

    doublep mannequin commented Aug 6, 2008

    Just to note, I proposed similar macro on the mailing list under the
    name Py_ASSIGN.

    @exarkun
    Copy link
    Mannequin

    exarkun mannequin commented Apr 8, 2010

    The name suggests a different behavior to me - I assumed it would set the reference count to a specific value. Maybe this is the kind of thing Raymond had in mind when he said "The mnemonic doesn't work for me".

    @pitrou
    Copy link
    Member Author

    pitrou commented Apr 8, 2010

    Py_ASSIGN could be a better name, but given the enthusiasm generated by this proposal, I think we might just as well close the issue.

    @mdickinson
    Copy link
    Member

    I agree with Raymond about the Py_INCREF. I could see more uses for this macro without the Py_INCREF, especially for things like in-place arithmetic operations. The following pattern appears a lot in code like Objects/longobject.c:

    Old code:

    /* add one to x */
    temp = PyNumber_Add(x, one);
    Py_DECREF(x);
    x = temp;
    if (x == NULL)
        return NULL;

    With a non-INCREF version of Py_SETREF:

    /* add one to x */
    Py_SETREF(x, PyNumber_Add(x, one));
    if (x == NULL)
        return NULL;

    @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
    extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants