how to go around non-picklable object types?
Aaron "Castironpi" Brady
castironpi at gmail.com
Mon Sep 15 20:59:54 CEST 2008
On Sep 15, 12:36 pm, "inhahe" <inh... at gmail.com> wrote:
> There are certain types of objects that can't be pickled, like file objects.
> My question is how could I pickle a hierarchy while automatically skipping
> all such objects instead of just aborting the pickle when it comes across
> The only thing I can think of is to make my own classes that wrap file
> objects and so forth and use __getstate__ to leave out the underlying file,
> etc. objects, or to set __getstate__ for every class that holds an offending
> object type and filter such objects out of dictionaries when I pickle those.
> And I don't even have a list of common object types that would make it
> Is there a better way? thx..
Nice to hear from you again. This is one possibility.
class TolerantPickler( pickle.Pickler ):
def save(self, obj):
pickle.Pickler.save( self, obj )
pickle.Pickler.save( self, None )
Then every time the native Pickler object tries to call 'save' it gets
your protected call instead. If the call fails with TypeError, just
Here are 'dumps' and a test.
from StringIO import StringIO
def dumps(obj, protocol=None):
file = StringIO()
class A: pass
a.a= open( 'temp.dat' )
b= dumps( a )
c= pickle.loads( b )
print c.a, c.b
<__main__.A instance at 0x00A7D1E8>
It may accomplish some of what you want. If it works, a pat on the
back to the writer of the Pickler class for using encapsulation.
More information about the Python-list