[Python-Dev] Making proxy types easier to write and maintain (original) (raw)

Brett Cannon bcannon at gmail.com
Wed Mar 19 20:32:50 CET 2014


On Wed Mar 19 2014 at 2:46:48 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:

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 / tpproxy) that would be used as a fallback by PyObjectLookupSpecial to fetch the lookup target, i.e.: def PyObjectLookupSpecial(obj, name): tp = type(obj) try: return getattr(tp, name) except AttributeError: return getattr(tp.tpproxy(), name) What do you think?

Without having the code in front of me, would this only be for magic methods and attributes, or all attributes? IOW would this mean that if I assign an object to proxy it would take care of the uses of getattr for proxying? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20140319/1afee8b8/attachment.html>



More information about the Python-Dev mailing list