Problem pickling exceptions in Python 2.5/2.6

I

Irmen de Jong

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top