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
Comments
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. expected result: 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: |
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. |
This updated patch calls PyUnicode_Check to ensure the name is a string before calling PyUnicode_InternInPlace. |
Eryk, could you create a pull request? Please take into account my and Berker's comments on Rietveld. |
type.__setattr__()
for non-interned attribute names. #1652type.__setattr__()
for non-interned attribute names. (GH-1652) #1673type.__setattr__()
for non-interned attribute names. (GH-1652) #1674type.__setattr__()
for non-interned or unicode attribute names. (GH-1652) #1675Note: 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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: