On Mon, 2002-09-23 at 14:30, Chris Backas wrote:
> I'm once again having an issue with pickling sessions, but this time I know
> exactly what's going on. My servlet is creating a few private variables in
> awake() that are essentially function pointers, and apparently those can't
> be pickled. That's OK with me, they don't need to be (as they're always
> recreated in awake() anyway), but, is there someplace I can add code to del
> them before pickling occurs to make sure that it's succesful?
From the pickle documentation:
If it is desirable that the __init__() method be called on unpickling, a
class can define a method __getinitargs__(), which should return a tuple
containing the arguments to be passed to the class constructor
(__init__()). This method is called at pickle time; the tuple it returns
is incorporated in the pickle for the instance.
Classes can further influence how their instances are pickled -- if the
class defines the method __getstate__(), it is called and the return
state is pickled as the contents for the instance, and if the class
defines the method __setstate__(), it is called with the unpickled
state. (Note that these methods can also be used to implement copying
class instances.) If there is no __getstate__() method, the instance's
__dict__ is pickled. If there is no __setstate__() method, the pickled
object must be a dictionary and its items are assigned to the new
instance's dictionary. (If a class defines both __getstate__() and
__setstate__(), the state object needn't be a dictionary -- these
methods can do what they want.) This protocol is also used by the
shallow and deep copying operations defined in the copy module.
So, presumably you can implement __getstate__(), and have it return
self.__dict__ minus the unpickleable variables -- when it's unpickled
those variables won't be set, so you'll also have to check for that.
Many other tricks and hacks are also possible using these methods, of
(I wasn't aware that servlets got pickled, though, just the session...?)