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


Groups > comp.lang.python > #28404

Why derivated exception can not be pickled ?

Newsgroups comp.lang.python
Date 2012-09-04 08:57 -0700
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>

Show all headers | 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