[Python-Dev] Making proxy types easier to write and maintain (original) (raw)
Antoine Pitrou solipsis at pitrou.net
Wed Mar 19 19:46:07 CET 2014
- Previous message: [Python-Dev] asyncio.wait(FIRST_COMPLETED) returns more than one completions - 3.4rc2
- Next message: [Python-Dev] Making proxy types easier to write and maintain
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hello,
It is known to be cumbersome to write a proxy type that will correctly proxy all special methods (this has to be done manually on the type, since special methods are not looked up on the instance: a getattr method would not work).
Recently we've had reports of two stdlib types that forgot to implement some special methods:
- weakref.proxy doesn't implement reversed: http://bugs.python.org/issue19359
- mock.MagicMock doesn't implement truediv: http://bugs.python.org/issue20968
In http://bugs.python.org/issue19359#msg213530 I proposed to introduce a "proxy protocol" (proxy / tp_proxy) that would be used as a fallback by _PyObject_LookupSpecial to fetch the lookup target, i.e.:
def _PyObject_LookupSpecial(obj, name): tp = type(obj) try: return getattr(tp, name) except AttributeError: return getattr(tp.tp_proxy(), name)
What do you think?
Regards
Antoine.
- Previous message: [Python-Dev] asyncio.wait(FIRST_COMPLETED) returns more than one completions - 3.4rc2
- Next message: [Python-Dev] Making proxy types easier to write and maintain
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]