By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,372 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,372 IT Pros & Developers. It's quick & easy.

Problem pickling exceptions in Python 2.5/2.6

P: n/a

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
Jun 27 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.