|
From: Michael F. <fuz...@vo...> - 2008-09-08 09:00:52
|
Looks like we need to support pickle after all. *sigh*
-------- Original Message --------
Subject: ConfigObj and pickle bug
Date: Mon, 08 Sep 2008 10:30:26 +0200
From: Christian Heimes <c.h...@se...>
To: Michael Foord <fuz...@vo...>
CC: Dirk Rothe <d....@se...>
Dear Michael!
I run into some trouble when I used a ConfigObj object as an argument to
parallel python. After some debugging I spotted a design issue in your code.
configobj.Section is a subclass of dict but it doesn't overwrite the
pickle hook __reduce__. Pickling a section or configobj keeps the data
in self (aka the dict) but not the data in self.__dict__. I came up with
a quick hotfix for the problem.
# ---
import configobj
import copy_reg
def configobj_factory(cls):
return cls.__new__(cls)
copy_reg.constructor(configobj_factory)
class SectionPatch(object):
def __setstate__(self, state):
dict.update(self, state[0])
self.__dict__.update(state[1])
def __reduce__(self):
state = (dict(self), dict(self.__dict__))
return (configobj_factory, (self.__class__, ), state)
def patch(cls, patchcls):
bases = cls.__bases__
if not patchcls in bases:
cls.__bases__ = (patchcls,) + bases
patch(configobj.Section, SectionPatch)
# ---
Greeting
Christian
--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/
http://www.trypython.org/
http://www.ironpython.info/
http://www.resolverhacks.net/
http://www.theotherdelia.co.uk/
|