[Grok-dev] pickling of dynamic classes
Leonardo Rochael Almeida
leorochael at gmail.com
Wed May 16 22:27:27 UTC 2012
You've hit the explanation exactly for your problem problem. The Pickle
module (and ZODB, by extension) can only store instances of classes that
exist at the top of modules.
This is because Pickle, when storing instances, stores the state of the
object (it's __dict__) with the name of the class and the name of the
module. When unpickling, It'll try to do the equivalent of:
module = sys.modules[module_name]
cls = getattr(module, class_name)
instance = cls.__new__()
instance.__dict__ = pickled_state
So, if your class does not exist at the top level, it can't be unpickled,
so it can't be pickled.
The only way to generate classes dynamically AND pickle their content is if
you store the generated class in some module. Something Like.
some_module.class_name = type('class_name', (<base class>,), dict())
However, this will only work if the above also happens before any class
instance is ever unpickled from the ZODB.
Hope it makes sense,
On Wed, May 16, 2012 at 1:24 PM, Anurag Sharma <a.sharma at chintan.in> wrote:
> Dear All,
> This seems to be generally a problem with pickle but I hope I will be able
> to get some inputs for the problem.
> In one function of my grok.Application class I'm dynamically creating a
> self.VarName = type('class name', (<base class>,), dict())
> * <base class> does inherit grok.Container.*
> While running this I'm getting following error:
> Module ZODB.serialize:*422* in serialize <http://localhost:8080/bizopine#>
> >> <http://localhost:8080/bizopine#>*return* self*.*_dump*(*meta*,* obj*.
> Module ZODB.serialize:*431* in _dump <http://localhost:8080/bizopine#>
> >> <http://localhost:8080/bizopine#>self*.*_p*.*dump*(*state*)*
> *PicklingError: Can't pickle <class '<appname>.app.<class name>'>:
> attribute lookup **<appname>.app.<class name>** failed*
> Pickling documentation does say that :
> What can be pickled and unpickledclasses that are defined at the top
> level of a module
> *But is it the problem here and if yes then what can be the possible
> solution for this ?*
> Grok-dev mailing list
> Grok-dev at zope.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Grok-dev