weakref.proxy behaviour in python 3.0

Discussion in 'Python' started by Nicholas Cole, Aug 21, 2010.

  1. Dear List,

    I've searched for information on this without success. Has
    weakref.proxy changed in Python 3? I couldn't see any note in the
    documentation, but the following code behaves differently on Python
    2.6.1 and Python 3:

    import weakref
    class Test(object): pass

    realobject = Test()
    pobject = weakref.proxy(realobject)
    l = [pobject,]

    print(realobject in l) # On python 2.6 this prints False, on Python 3 True.

    Is this an expected change?

    Best wishes,

    Nicholas
     
    Nicholas Cole, Aug 21, 2010
    #1
    1. Advertising

  2. On Aug 21, 1:13 pm, Nicholas Cole <> wrote:
    > I've searched for information on this without success.  Has
    > weakref.proxy changed in Python 3?  I couldn't see any note in the
    > documentation, but the following code behaves differently on Python
    > 2.6.1 and Python 3:
    >
    > import weakref
    > class Test(object): pass
    >
    > realobject = Test()
    > pobject = weakref.proxy(realobject)
    > l = [pobject,]
    >
    > print(realobject in l)   # On python 2.6 this prints False, on Python 3 True.


    So the change underlying what you're seeing is that comparisons in 3.x
    'unwrap' the proxy, while in 2.x they don't:

    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

    It looks to me as though this could be a long-standing defect in
    Python 2.x, unwittingly(?) corrected in Python 3.x when 3-way
    comparisons were removed in favour of rich comparisons. See

    http://svn.python.org/view?view=rev&revision=51533

    For 2.7, the proxy source (in Objects/weakrefobject.c) defines a
    'proxy_compare' function that's used in the 'tp_compare' slot for
    proxy objects, and that proxy_compare function has code to unwrap the
    proxy objects when necessary so that comparisons are done on the real
    underlying objects.

    *But* C-level tp_compare slots only ever get called when both objects
    have the same type, so when comparing a real object with the proxy for
    that object, that's never.

    In 3.x, that's replace with a proxy_richcompare function for the
    tp_richcompare slot.

    So my guess is that the change was unintentional.

    It's probably worth a bug report. Even if the behaviour isn't going
    to change in either 2.x or 3.x (and it probably isn't), it might be
    possible to clarify the docs.

    --
    Mark
     
    Mark Dickinson, Aug 21, 2010
    #2
    1. Advertising

  3. On Sat, Aug 21, 2010 at 3:31 PM, Mark Dickinson <> wrote:

    [SNIP]

    > So my guess is that the change was unintentional.
    >
    > It's probably worth a bug report.  Even if the behaviour isn't going
    > to change in either 2.x or 3.x (and it probably isn't), it might be
    > possible to clarify the docs.


    Dear Mark,

    I think the docs should be fixed: it would be good to have a list of
    key examples where the behaviour is different. Although the new
    behaviour is better, it certainly tripped me up badly.

    I'm happy to fill a report out, but since you seem to know much more
    about the internals, I wonder if a bug report written by you would be
    more useful!

    Just in case it helps, one thing that does seem to be the case is that
    two different proxy objects to the same real object get compared in
    the same way on both versions. So this code:

    a = weakref.proxy(the_real_object)
    b = weakref.proxy(the_real_object)

    this_list = [ a, ]

    l.remove(a) # Obviously works on both - just here for clarity.

    l.remove(the_real_object) # Fails on python 2.6

    l.remove(b) # gives an empty list on python 2.6 and python 3.


    Very best wishes,

    Nicholas
     
    Nicholas Cole, Aug 21, 2010
    #3
  4. On Aug 21, 5:06 pm, Nicholas Cole <> wrote:
    > On Sat, Aug 21, 2010 at 3:31 PM, Mark Dickinson <> wrote:
    >
    > [SNIP]
    >
    > > So my guess is that the change was unintentional.

    >
    > > It's probably worth a bug report.  Even if the behaviour isn't going
    > > to change in either 2.x or 3.x (and it probably isn't), it might be
    > > possible to clarify the docs.

    >
    > I think the docs should be fixed: it would be good to have a list of
    > key examples where the behaviour is different.  Although the new
    > behaviour is better, it certainly tripped me up badly.
    >
    > I'm happy to fill a report out, but since you seem to know much more
    > about the internals, I wonder if a bug report written by you would be
    > more useful!


    http://bugs.python.org/issue9658

    Please do log in and add any extra comments you feel appropriate.

    --
    Mark
     
    Mark Dickinson, Aug 21, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ames Andreas (MPA/DF)

    weakref and thread safety (in python 2.1)

    Ames Andreas (MPA/DF), Jul 22, 2003, in forum: Python
    Replies:
    1
    Views:
    438
    Duncan Booth
    Jul 22, 2003
  2. William Trenker

    Using weakref with execfile?

    William Trenker, Dec 21, 2003, in forum: Python
    Replies:
    1
    Views:
    282
    Martin v. Loewis
    Dec 22, 2003
  3. Walter Haslbeck

    problem with weakref.proxy

    Walter Haslbeck, Jan 31, 2004, in forum: Python
    Replies:
    5
    Views:
    545
    Walter Haslbeck
    Jan 31, 2004
  4. John Nagle
    Replies:
    2
    Views:
    1,051
    John Nagle
    Feb 26, 2007
  5. Klein Stéphane
    Replies:
    3
    Views:
    613
    Steve Holden
    Dec 20, 2009
Loading...

Share This Page