[issue19359] reversed() does not work with weakref.proxy of sequences

Raymond Hettinger report at bugs.python.org
Fri Mar 14 10:00:02 CET 2014


Raymond Hettinger added the comment:

Thanks for looking at this.

Putting in a special case for weak references isn't the way to go.  The problem is that _PyObject_LookupSpecial isn't working well with weakref proxies.

A solution for reversed() could be to replace ``PyObject_GetAttrString(seq, "__reversed__")`` with the slower call to ``_PyObject_LookupSpecial(seq, "__reversed__", &reversed_cache);``.

The unfortunate downside is that this would slow down the common cases.

Another solution is to patch _PyObject_LookupSpecial to make it smarter with respect to weakref objects or possibly use a fallback to PyObject_GetAttrString when a method isn't found using the speedy lookup.

The advantage of fixing _PyObject_LookupSpecial is that it fixes all uses of _PyObject_LookupSpecial not just reversed().  Also, it would keep the current speed benefit for the common case.

I'm reassigning to Benjamin because it was his optimized that broke the ability to find the __reversed__ method on a proxy object.

Another issue that needs to be looked at is whether PySequence_Check() needs to be made aware of weakref proxies.

----------
assignee: rhettinger -> benjamin.peterson
nosy: +benjamin.peterson, haypo
priority: low -> normal
stage:  -> needs patch
type: enhancement -> behavior
versions: +Python 2.7, Python 3.4
Added file: http://bugs.python.org/file34413/reversed_list_proxy_test.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19359>
_______________________________________


More information about the Python-bugs-list mailing list