Issue 30469: Inconsistent Execution of Generic Descriptor Attributes (original) (raw)
Issue30469
Created on 2017-05-25 00:13 by Ryan Morshead, last changed 2022-04-11 14:58 by admin.
Messages (3) | ||
---|---|---|
msg294415 - (view) | Author: Ryan Morshead (Ryan Morshead) | Date: 2017-05-25 00:13 |
When the `__get__`, `__set__`, or `__delete__` attribute of a descriptor is not a method, and is instead a generic callable, the first argument of that callable is inconsistent: class Callable(object): def __call__(self, first, *args, **kwargs): print(first) class Descriptor(object): __set__ = Callable() __delete__ = Callable() __get__ = Callable() class MyClass(object): d = Descriptor() mc = MyClass() mc.d = 1 del mc.d mc.d Prints: <__main__.MyClass object at 0x10854cda0> <__main__.MyClass object at 0x10854cda0> <__main__.Descriptor object at 0x10855f240> As it turns out, this occurs because `slot_tp_descr_set` (shared by `__set__` and `__delete__`) and `slot_tp_descr_get` just aren't consistent in their implementation. See: https://stackoverflow.com/questions/44169370/strange-descriptor-behavior/44169805#44169805 Is this behavior intentional? If not, how ought this case be handled? | ||
msg294424 - (view) | Author: Ryan Morshead (Ryan Morshead) | Date: 2017-05-25 01:45 |
Is there a reason that `call_method` is not used in `slot_tp_descr_get` | ||
msg294428 - (view) | Author: Eryk Sun (eryksun) * ![]() |
Date: 2017-05-25 03:14 |
It looks like slot_tp_descr_get bypasses call_method in order to unset tp->tp_descr_get for descriptors that don't define __get__. I don't know where that's an issue. If a class doesn't define __get__, its tp_descr_get slot should already be NULL. That said, I don't see why it's calling _PyType_LookupId directly instead of lookup_maybe. Using lookup_maybe would bind __get__ if it's a descriptor, and we want that, no? Then slot_tp_descr_get wouldn't have to manually hack `self` into the call, which is wrong in this case. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:58:46 | admin | set | github: 74654 |
2021-03-05 13:49:26 | rhettinger | set | nosy: + rhettinger |
2021-03-05 13:43:39 | eryksun | set | versions: + Python 3.8, Python 3.9, Python 3.10, - Python 2.7, Python 3.5, Python 3.6, Python 3.7 |
2017-05-25 03:14:02 | eryksun | set | versions: + Python 3.5, Python 3.6, Python 3.7nosy: + eryksunmessages: + components: + Interpreter Core, - ctypes |
2017-05-25 01:45:01 | Ryan Morshead | set | messages: + |
2017-05-25 00:13:08 | Ryan Morshead | create |