Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28404

Why derivated exception can not be pickled ?

Received by 10.224.87.77 with SMTP id v13mr12446605qal.5.1346774220760; Tue, 04 Sep 2012 08:57:00 -0700 (PDT)
Received by 10.52.23.145 with SMTP id m17mr2380306vdf.0.1346774220711; Tue, 04 Sep 2012 08:57:00 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!v8no973324qap.0!news-out.google.com!da15ni7637554qab.0!nntp.google.com!b19no1027307qas.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Tue, 4 Sep 2012 08:57:00 -0700 (PDT)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=31.35.138.175; posting-account=qQa_BAoAAAB6AF_SqtOQYran4yaGpqxv
NNTP-Posting-Host 31.35.138.175
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <7bb72fb5-5ab5-4748-acbf-c8e7666ee834@googlegroups.com> (permalink)
Subject Why derivated exception can not be pickled ?
From Mathieu Courtois <mathieu.courtois@gmail.com>
Injection-Date Tue, 04 Sep 2012 15:57:00 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.python:28404

Show key headers only | View raw


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.

Does anyone explain me why ?
Thanks.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Why derivated exception can not be pickled ? Mathieu Courtois <mathieu.courtois@gmail.com> - 2012-09-04 08:57 -0700
  Re: Why derivated exception can not be pickled ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-05 00:34 +0000
  Re: Why derivated exception can not be pickled ? Dieter Maurer <dieter@handshake.de> - 2012-09-05 08:02 +0200
    Re: Why derivated exception can not be pickled ? Mathieu Courtois <mathieu.courtois@gmail.com> - 2012-09-05 01:54 -0700
    Re: Why derivated exception can not be pickled ? Mathieu Courtois <mathieu.courtois@gmail.com> - 2012-09-05 01:54 -0700
  Re: Why derivated exception can not be pickled ? Mathieu Courtois <mathieu.courtois@gmail.com> - 2012-09-05 03:18 -0700

csiph-web