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

__setattr__ does not always overload operators #69980

Closed
DominikSchmid mannequin opened this issue Dec 4, 2015 · 8 comments
Closed

__setattr__ does not always overload operators #69980

DominikSchmid mannequin opened this issue Dec 4, 2015 · 8 comments
Assignees
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@DominikSchmid
Copy link
Mannequin

DominikSchmid mannequin commented Dec 4, 2015

BPO 25794
Nosy @berkerpeksag, @serhiy-storchaka, @eryksun, @mlouielu
PRs
  • bpo-25794: Fix type.__setattr__() for non-interned attribute names. #1652
  • [3.6] bpo-25794: Fix type.__setattr__() for non-interned attribute names. (GH-1652) #1673
  • [3.5] bpo-25794: Fix type.__setattr__() for non-interned attribute names. (GH-1652) #1674
  • [2.7] bpo-25794: Fix type.__setattr__() for non-interned or unicode attribute names. (GH-1652) #1675
  • Files
  • bug.py: source code reproducing the bug
  • issue25794_2.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 = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2017-05-20.07:25:37.980>
    created_at = <Date 2015-12-04.08:52:36.937>
    labels = ['interpreter-core', 'type-bug', '3.7']
    title = '__setattr__ does not always overload operators'
    updated_at = <Date 2017-05-20.07:25:37.960>
    user = 'https://bugs.python.org/DominikSchmid'

    bugs.python.org fields:

    activity = <Date 2017-05-20.07:25:37.960>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2017-05-20.07:25:37.980>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2015-12-04.08:52:36.937>
    creator = 'Dominik Schmid'
    dependencies = []
    files = ['41234', '41236']
    hgrepos = []
    issue_num = 25794
    keywords = ['patch']
    message_count = 8.0
    messages = ['255853', '255859', '255862', '292176', '294006', '294015', '294017', '294018']
    nosy_count = 5.0
    nosy_names = ['berker.peksag', 'serhiy.storchaka', 'eryksun', 'Dominik Schmid', 'louielu']
    pr_nums = ['1652', '1673', '1674', '1675']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue25794'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6', 'Python 3.7']

    @DominikSchmid
    Copy link
    Mannequin Author

    DominikSchmid mannequin commented Dec 4, 2015

    While implementing my own Integer class that keeps track of when operations are applied I noticed that setattr had a strange behaviour when I tried to wrap operator functions.

    When the attribute string had a different id to its literal it failed to overload the operator.
    Are we doing a 'is' rather than a '==' somewhere in setattr?

    expected result:
    139723705431168 a.__add__(b)= (5) a+b= (5)
    139723705431168 a.__add__(b)= (5) a+b= (5)
    139723704361584 a.__add__(b)= (5) a+b= (5)

    actual result:
    139723705431168    a.__add__(b)= (5)    a+b= (5)
    139723705431168    a.__add__(b)= (5)    a+b= (5)
    139723704361584    a.__add__(b)= (5)    a+b=
    Traceback (most recent call last):
      File "/home/dom/Documents/leastOps/bug.py", line 41, in <module>
        testSetattr(funcName3)
      File "/home/dom/Documents/leastOps/bug.py", line 28, in testSetattr
        print '   a+b=', a+b
    TypeError: unsupported operand type(s) for +: 'Integer' and 'Integer'

    version:
    2.7.10 (default, Oct 14 2015, 16:09:02)
    [GCC 5.2.1 20151010]
    ubuntu 14.10

    @DominikSchmid DominikSchmid mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Dec 4, 2015
    @eryksun
    Copy link
    Contributor

    eryksun commented Dec 4, 2015

    Normally Python code calls built-in setattr, which calls the C API PyObject_SetAttr. This API interns the attribute name before calling the type's tp_setattro or tp_setattr function. Interning the string is a critical step, since the implementation for updating slots assumes the name is interned.

    The __setattr__ slot wrapper calls wrap_setattr in Objects/typeobject.c. In line with how PyObject_SetAttr works, the attached patch interns the name in wrap_setattr before calling the wrapped setattrofunc. For good measure it also applies the same change to wrap_delattr, though that's not strictly necessary.

    @eryksun
    Copy link
    Contributor

    eryksun commented Dec 4, 2015

    This updated patch calls PyUnicode_Check to ensure the name is a string before calling PyUnicode_InternInPlace.

    @serhiy-storchaka
    Copy link
    Member

    Eryk, could you create a pull request? Please take into account my and Berker's comments on Rietveld.

    @serhiy-storchaka
    Copy link
    Member

    New changeset d896985 by Serhiy Storchaka in branch 'master':
    bpo-25794: Fix type.__setattr__() for non-interned attribute names. (bpo-1652)
    d896985

    @serhiy-storchaka
    Copy link
    Member

    New changeset e9f9b04 by Serhiy Storchaka in branch '2.7':
    [2.7] bpo-25794: Fix type.__setattr__() for non-interned or unicode attribute names. (GH-1652) (bpo-1675)
    e9f9b04

    @serhiy-storchaka
    Copy link
    Member

    New changeset 193f7e0 by Serhiy Storchaka in branch '3.6':
    [3.6] bpo-25794: Fix type.__setattr__() for non-interned attribute names. (GH-1652) (bpo-1673)
    193f7e0

    @serhiy-storchaka
    Copy link
    Member

    New changeset 4a86fe9 by Serhiy Storchaka in branch '3.5':
    [3.5] bpo-25794: Fix type.__setattr__() for non-interned attribute names. (GH-1652) (bpo-1674)
    4a86fe9

    @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.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants