Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24980

Re: Question about weakref

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'test,': 0.05; 'raises': 0.07; 'subject:Question': 0.07; 'python': 0.09; 'exception,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'b):': 0.16; 'compare.': 0.16; 'compares': 0.16; 'reproduce': 0.16; 'sure.': 0.16; 'using,': 0.16; 'wrote:': 0.17; 'element': 0.17; 'handles': 0.18; 'aspect': 0.22; 'explicit': 0.22; 'noted': 0.22; 'received :mail-bk0-f46.google.com': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'object,': 0.27; 'received:209.85.214.46': 0.27; 'message-id:@mail.gmail.com': 0.27; 'comparison': 0.29; 'equality': 0.29; 'relies': 0.29; 'searches': 0.29; 'objects': 0.29; "i'm": 0.29; 'knows': 0.30; 'fri,': 0.30; 'implement': 0.32; 'true.': 0.33; 'problem': 0.33; 'that,': 0.34; 'received:google.com': 0.34; 'self': 0.34; 'problem,': 0.35; 'received:209.85': 0.35; 'really': 0.36; "wasn't": 0.36; 'method': 0.36; 'too': 0.36; 'itself': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'received:209.85.214': 0.39; 'step': 0.39; 'build': 0.39; 'list,': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'remove': 0.61; 'first': 0.61; 'here': 0.65; 'identity.': 0.65; 'jul': 0.65; 'carefully': 0.71; 'soon,': 0.75; 'locally': 0.84; 'ref': 0.84; 'spoke': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=BieWRSDgIg1baPT7JDEzMUGrHPALjliAeFomIK4dAsw=; b=msiQKVTRlSU3qcokjPP3SpBC4jWeM162ZGMq2pGR9dkOHMKmXM+Ji8SfgtSb/axj5V ao5C+0kwOhc0JeatcokE/jN5HFw+uLsK0/sWmQTLkJvI4l1Jr9TMUdHOd/oOg85cQM4O 5q8W6NVgb7u6W5U1QC7S+QmxPRKulytMNdoSgdsUmN/42oQf80oPoAAdyo6ivczFY0Q5 wJrWAicUbVq+m8uVGIfuVe35kedBXVWzuWmB0dTzQdY+bFx0Y4JV5fyUsxjbLo6IJTwC +YEbUvMcRk1A+KdqeNz00W/xe9sEQTuytgtE4WMbeLdphIWbONqPWpuNUTJhsyiM7g5V vWAw==
MIME-Version 1.0
In-Reply-To <CALwzidmAZavM-R1UXE_Qb-6vmk-518fZ-vJGd2GEHbTROAn2Vg@mail.gmail.com>
References <jt1hg2$bmp$1@dough.gmane.org> <87hatmr3xf.fsf@handshake.de> <jt3tc0$5q6$1@dough.gmane.org> <87a9ze85h2.fsf@handshake.de> <jt62f2$t89$1@dough.gmane.org> <CALwzidmAZavM-R1UXE_Qb-6vmk-518fZ-vJGd2GEHbTROAn2Vg@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 6 Jul 2012 11:48:36 -0600
Subject Re: Question about weakref
To Frank Millman <frank@chagford.com>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1866.1341596949.4697.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1341596949 news.xs4all.nl 6963 [2001:888:2000:d::a6]:40765
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24980

Show key headers only | View raw


On Fri, Jul 6, 2012 at 11:04 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On that, I'm really not sure.  I tried to reproduce the problem
> locally and wasn't able to.  What build of Python are you using, and
> on what platform?

I spoke too soon, I am able to reproduce it.  I think what's going on
here is that when you try to remove the proxy from the list, the
list.remove() call searches for the object by *equality*, not by
identity.  The problem is that at the time of the callback, the
referent is no longer available to implement the equality test, as
noted in the weakref.ref() documentation.  As long as the proxy
happens to be the first element of the list, this is not a problem,
because the proxy evidently short-circuits self == self to return
True.  If it's not the first element of the list, though, then the
first comparison compares the proxy to some other object, and the
proxy raises an exception, because without the referent it no longer
knows how to compare.  If you change your del_b() method to the
following, though, it works:

    def del_b(self, b):
        for i, x in enumerate(self.array):
            if b is x:
                del self.array[i]

This works because it carefully only handles the proxy object itself
and no longer relies on any aspect of the referent for deletion.  It's
not a problem for weakref.ref, because ref objects require an explicit
dereferencing step to access the referent.

Cheers,
Ian

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Question about weakref Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-06 11:48 -0600

csiph-web