[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: weak reference callback
From:       Paul Pogonyshev <pogonyshev () gmx ! net>
Date:       2009-08-29 21:14:38
Message-ID: 200908300014.38644.pogonyshev () gmx ! net
[Download RAW message or body]

Christian Heimes wrote:
> Paul Pogonyshev wrote:
> > Hi,
> > 
> > Is weak reference callback called immediately after the referenced
> > object is deleted or at arbitrary point in time after that?  I.e. is
> > it possible to see a dead reference before the callback is called?
> > 
> > More formally, will this ever raise?
> > 
> >     callback_called = False
> >     def note_deletion (ref):
> >         callback_called = True
> > 
> >     x = ...
> >     ref = weakref.ref (x, note_deletion)
> > 
> >     ...
> > 
> >     if ref () is None and not callback_called:
> >         raise RuntimeError ("reference is dead, yet callback hasn't been called yet")
> 
> Yes, you'll definitely see a RuntimeError because you forgot to declare
> callback_called as a global variable. :)

Thanks.  Any useful answers though?

    callback_called = False
    def note_deletion (ref):
        global callback_called
        callback_called = True

    x = ...
    ref = weakref.ref (x, note_deletion)

    ...

    if ref () is None and not callback_called:
        raise RuntimeError ("reference is dead, yet callback hasn't been called yet")

Paul
-- 
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic