#!/usr/bin/python2.7 # -*- coding: utf-8 -*- import sys print 'Python version:' print sys.version print print '''Test cPickle with classes derived from Exception.''' #====================================================================== # Some utility functions def banner(title, c='-'): print c*80 print title print '-'*80 import cPickle def pickle_unpickle(obj): print "before pickle str(obj):" print ">>> '%s'" % obj s = cPickle.dumps(obj) try: tc = cPickle.loads(s) print 'after pickle str(obj):' print ">>> '%s'" % tc except: print "\nunpickling fails ", print "EXCEPTION ### "*3 + "EXCEPTION\n" print sys.exc_info()[1] return s #====================================================================== # Pickling/unpickling Exceptions works; example ValueError print banner('Class ValueError: pickling/unpickling exceptions works fine.', '=') t = ValueError('just some custom text in ValueError') s = pickle_unpickle(t) #====================================================================== # Now we define a class derived from 'Exception' # We pickle/unpickle it and get errors, unless we define a class 'Exception' # ourselves. class TestException(Exception): """Class TestException(Exception): derived class: error occurs, if superclass 'Exception' is not defined in this module!""" def __init__(self, msg, details=""): self.msg = msg self.details = details def __str__(self): return self.msg print banner(TestException.__doc__, '=') x = TestException('Exception is neither imported nor defined') s = pickle_unpickle(x) #---------------------------------------------------------------------- from exceptions import Exception class TestException(Exception): """Class TestException(Exception): derived class: error occurs, even though superclass 'Exception' is imported!""" def __init__(self, msg, details=""): self.msg = msg self.details = details def __str__(self): return self.msg print banner(TestException.__doc__, '=') x = TestException('Exception is imported') s = pickle_unpickle(x) #---------------------------------------------------------------------- class Exception: """Class Exception: our own custom definition of class 'Exception'.""" def __init__(self, msg, details=""): self.msg = msg self.details = details def __str__(self): return 'Exception: ' + self.msg print banner(Exception.__doc__, '=') #---------------------------------------------------------------------- class TestException(Exception): """Class TestException(Exception): derived class: NO error occurs, since we have a custom definition of superclass 'Exception'!""" """Class TestException(Exception): Abgeleitete Klasse: KEIN Fehler! Oberklasse 'Exception' wurde hier im Modul definiert!""" def __init__(self, msg, details=""): self.msg = msg self.details = details def __str__(self): return self.msg print banner(TestException.__doc__, '=') x = TestException('Exception is custom defined') s = pickle_unpickle(x) #====================================================================== # From that I conclude, that something in the definition of 'Exception' # cannot be pickled.