Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'debug': 0.05; 'method.': 0.05; 'defines': 0.07; "'w')": 0.09; 'arg': 0.09; 'called.': 0.09; 'expected.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Why': 0.09; 'way:': 0.09; 'def': 0.10; 'subject:not': 0.11; "'r')": 0.16; 'arg):': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:exception': 0.16; 'import': 0.21; 'skip:_ 20': 0.22; 'example': 0.23; 'pass': 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'interface': 0.27; 'header:X-Complaints-To:1': 0.28; 'obj': 0.29; 'pickle': 0.29; 'trigger': 0.29; 'writes:': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'subject: ?': 0.30; 'print': 0.32; 'to:addr:python-list': 0.33; 'there': 0.35; 'received:org': 0.36; 'charset:us-ascii': 0.36; 'skip:p 20': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'several': 0.39; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'more': 0.63; 'here': 0.65; 'detail.': 0.65; 'received:217': 0.68 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dieter Maurer Subject: Re: Why derivated exception can not be pickled ? Date: Wed, 05 Sep 2012 08:02:12 +0200 References: <7bb72fb5-5ab5-4748-acbf-c8e7666ee834@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Gmane-NNTP-Posting-Host: pd9e08fe4.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) Cancel-Lock: sha1:Wir/kTgi+LiAcmwfPpBzSDvjGUc= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346824947 news.xs4all.nl 6982 [2001:888:2000:d::a6]:59961 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28454 Mathieu Courtois writes: > Here is my example : > > > import cPickle > > ParentClass = object # works > ParentClass = Exception # does not > > class MyError(ParentClass): > def __init__(self, arg): > self.arg = arg > > def __getstate__(self): > print '#DBG pass in getstate' > odict = self.__dict__.copy() > return odict > > def __setstate__(self, state): > print '#DBG pass in setstate' > self.__dict__.update(state) > > exc = MyError('IDMESS') > > fo = open('pick.1', 'w') > cPickle.dump(exc, fo) > fo.close() > > fo = open('pick.1', 'r') > obj = cPickle.load(fo) > fo.close() > > > 1. With ParentClass=object, it works as expected. > > 2. With ParentClass=Exception, __getstate__/__setstate__ are not called. The pickle interface is actually more complex and there are several ways an object can ensure picklability. For example, there is also a "__reduce__" method. I suppose, that "Exception" defines methods which trigger the use of an alternative picklability approach (different from "__getstate__/__setstate__"). I would approach your case the following way: Use "pickle" instead of "cPickle" and debug picking/unpickling to find out what happens in detail.