[ZODB-Dev] How do you "wake up" sleepy persistent objects?

Jim Fulton jim@zope.com
Fri, 12 Oct 2001 10:45:29 -0400


"Patrick K. O'Brien" wrote:
> 
> Here is my situation. I'm working on a sample app that uses ZODB, but not
> Zope. The interface to the application is, at this point, simply the Python
> shell. Specifically, the Python shell that I created in wxPython/Scintilla,
> called PyCrust (http://sourceforge.net/projects/pycrust/). PyCrust provides
> call tips for methods and an auto-completion popup list whenever you type an
> object name followed by a dot. I noticed that ghosted objects did not
> display all of their attributes in the autocompletion list the first time
> you typed a dot, but they would the second time (because we just woke them
> up). Since I wanted to see the proper autocompletion list the first time
> around, I changed the method that generates the popup list to the following:
> 
> def getAllAttributeNames(object):
>     """Return list of all attributes, including inherited, for an object.
> 
>     Recursively walk through a class and all base classes.
>     """
>     attributes = []
>     # Wake up sleepy objects - a hack for ZODB objects in "ghost" state.
>     wakeupcall = dir(object)
>     del wakeupcall
>     # Get attributes available through the normal convention.
>     attributes += dir(object)
>     # For a class instance, get the attributes for the class.
>     if hasattr(object, '__class__'):
>         # Break a circular reference. This happens with extension classes.
>         if object.__class__ is object:
>             pass
>         else:
>             attributes += getAllAttributeNames(object.__class__)
>     # Also get attributes from any and all parent classes.
>     if hasattr(object, '__bases__'):
>         for base in object.__bases__:
>             attributes += getAllAttributeNames(base)
>     return attributes
> 
> This does what I want. Do you consider this case of waking up ghosts to be
> acceptable?

It's an acceptable work-around for the incompatability of dir and ZODB.
I'll not that the point is overcoming the dir problem, not waking up objects.

> Or do you have a suggestion for a better way to handle this
> situation?

Eventually we need to fix the dir/ZODB interaction. This hack is
a reasonable work-around.

Jim

--
Jim Fulton           mailto:jim@zope.com       Python Powered!        
CTO                  (888) 344-4332            http://www.python.org  
Zope Corporation     http://www.zope.com       http://www.zope.org