Hi,<div><br></div><div>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.</div><div><br></div><div>
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:</div><div>
<br>
</div><div>module = sys.modules[module_name]</div><div>cls = getattr(module, class_name)</div><div>instance = cls.__new__()</div><div>instance.__dict__ = pickled_state</div><div><br></div><div>So, if your class does not exist at the top level, it can't be unpickled, so it can't be pickled.</div>
<div><br></div><div>The only way to generate classes dynamically AND pickle their content is if you store the generated class in some module. Something Like.</div><div><br></div><div>some_module.class_name = type('class_name', (<base class>,), dict())</div>
<div><br></div><div>However, this will only work if the above also happens before any class instance is ever unpickled from the ZODB.</div><div><br></div><div>Hope it makes sense,</div><div><br></div><div>Cheers,</div><div>
<br></div><div>Leo</div><div><br><div class="gmail_quote">On Wed, May 16, 2012 at 1:24 PM, Anurag Sharma <span dir="ltr"><<a href="mailto:a.sharma@chintan.in" target="_blank">a.sharma@chintan.in</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear All,<br><br>This seems to be generally a problem with pickle but I hope I will be able to get some inputs for the problem.<br>
<br>In one function of my grok.Application class I'm dynamically creating a class<br><br>
self.VarName = type('class name', (<base class>,), dict())<br><b> <base class> does inherit grok.Container.</b><br><br>While running this I'm getting following error:<br><br>Module <span title="c:\documents and settings\user\.buildout\eggs\zodb3-3.10.5-py2.7-win32.egg\ZODB\serialize.py">ZODB.serialize</span>:<b>422</b> in <code>serialize</code> <a href="http://localhost:8080/bizopine#" target="_blank"> <img border="0" height="9" width="9"> </a><br>
<code><a href="http://localhost:8080/bizopine#" target="_blank">>> </a><font color="#0000AF"><b>return</b></font> <font color="#333333">self</font><b>.</b><font color="#333333">_dump</font><b>(</b><font color="#333333">meta</font><b>,</b> <font color="#333333">obj</font><b>.</b><font color="#333333">__getstate__</font><b>(</b><b>)</b><b>)</b></code><br>
Module <span title="c:\documents and settings\user\.buildout\eggs\zodb3-3.10.5-py2.7-win32.egg\ZODB\serialize.py">ZODB.serialize</span>:<b>431</b> in <code>_dump</code> <a href="http://localhost:8080/bizopine#" target="_blank"> <img border="0" height="9" width="9"> </a><br>
<code><a href="http://localhost:8080/bizopine#" target="_blank">>> </a><font color="#333333">self</font><b>.</b><font color="#333333">_p</font><b>.</b><font color="#333333">dump</font><b>(</b><font color="#333333">state</font><b>)</b></code><br>
<b>PicklingError: Can't pickle <class '<appname>.app.<class name>'>: attribute lookup </b><b><appname>.app.<class name></b><b> failed</b><br><br>Pickling documentation does say that :<br>
<h2>What can be pickled and unpickled</h2>classes that are defined at the top level of a module<br><br><b>But is it the problem here and if yes then what can be the possible solution for this ?</b><br><br>Thanks,<br>Anurag
<br>_______________________________________________<br>
Grok-dev mailing list<br>
<a href="mailto:Grok-dev@zope.org">Grok-dev@zope.org</a><br>
<a href="https://mail.zope.org/mailman/listinfo/grok-dev" target="_blank">https://mail.zope.org/mailman/listinfo/grok-dev</a><br>
<br></blockquote></div><br></div>