Message114557
Nicholas Cole noted on python-list that the behaviour of weakref.proxy with respect to equality changed between 2.x and 3.x:
Python 2.7 (r27:82500, Aug 15 2010, 14:21:15)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import weakref
>>> s = set()
>>> s == weakref.proxy(s)
False
Python 3.1.2 (r312:79147, Aug 20 2010, 20:06:00)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import weakref
>>> s = set()
>>> s == weakref.proxy(s)
True
This seems to be an inadvertent change resulting from the switch from 3-way comparisons to rich comparisons: the 2.x source implements tp_compare for proxy objects. The tp_compare slot *does* unwrap the proxy objects before comparing, but since the tp_compare slot in general is only ever called (for objects implemented in C) when the types of the objects being compared are the same, a proxy object won't compare equal to its referent.
I believe that Nicholas ran into this when using weakrefs as elements of containers. Nicholas: is that right? Care to elaborate?
The 3.x source changes this to use tp_richcompare (see r51533), so now a proxy object *does* compare equal to its referent.
The 3.x behaviour seems better to my limited eyes, and the 2.x behaviour has been around a long time without causing problems, so we probably don't want to change the behaviour in either case. But it might be good to document the difference somewhere. |
|
Date |
User |
Action |
Args |
2010-08-21 19:44:22 | mark.dickinson | set | recipients:
+ mark.dickinson |
2010-08-21 19:44:22 | mark.dickinson | set | messageid: <1282419862.51.0.264413322839.issue9658@psf.upfronthosting.co.za> |
2010-08-21 19:44:20 | mark.dickinson | link | issue9658 messages |
2010-08-21 19:44:20 | mark.dickinson | create | |
|