Problem pickling exceptions in Python 2.5/2.6

Irmen de Jong irmen.NOSPAM at xs4all.nl
Sun Jun 8 12:11:16 CEST 2008


I'm having troubles pickling classes that extend Exception.

Given the following source:

class Foo(object):
     def __init__(self, m):
         self.m=m

class Bar(Exception):
     def __init__(self, m):
         self.m=m

import pickle
s=pickle.dumps(Foo("test"))
pickle.loads(s)  # normal object works
s=pickle.dumps(Bar("test"))
pickle.loads(s)  # exception object fails



When running this in Python 2.5 or Python 2.6a, it crashes on the last line, with:

[C:\]c:\python25\python.exe pickling.py
Traceback (most recent call last):
   File "pickling.py", line 14, in <module>
     pickle.loads(s)  # exception object fails
   File "C:\Python25\lib\pickle.py", line 1374, in loads
     return Unpickler(file).load()
   File "C:\Python25\lib\pickle.py", line 858, in load
     dispatch[key](self)
   File "C:\Python25\lib\pickle.py", line 1133, in load_reduc
     value = func(*args)
TypeError: __init__() takes exactly 2 arguments (1 given)


This used to work fine in Python 2.3 and 2.4...
What has been changed in 2.5? Is there a way to get rid of the error?
(I tried some stuff with __getinitargs__ and __getnewargs__ but couldn't get it to work).


--irmen



More information about the Python-list mailing list