[IronPython] Bug in Ipy 2.6 when pickling exceptions

Dino Viehland dinov at microsoft.com
Sun Dec 20 20:44:25 CET 2009


CPython does the exact same thing:

Python 2.6.3 (r263rc1:75186, Oct  2 2009, 20:40:30) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class my_exc(Exception):
...     def __init__(self, message, param):
...             super(my_exc, self).__init__(message)
...             self.param = param
...
>>>
>>> e = my_exc('message','param')
>>> import pickle
>>> pickle.loads(pickle.dumps(e))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\pickle.py", line 1374, in loads
    return Unpickler(file).load()
  File "C:\Python26\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python26\lib\pickle.py", line 1133, in load_reduce
    value = func(*args)
TypeError: __init__() takes exactly 3 arguments (2 given)
>>>

You can override __reduce_ex__ so that it'll match up with your __init__ method:

class my_exc(Exception):
    def __init__(self, message, param):
            super(my_exc, self).__init__(message)
            self.param = param
    def __reduce_ex__(self, protocol):
           return (my_exc, (self.message, self.param))

e = my_exc('message','param')
import pickle
pickle.loads(pickle.dumps(e))

It looks like you should be able to just override __reduce__ but it looks like IronPython calls __reduce_ex__ where CPython calls __reduce__ for some reason.  I've filed that as bug 25731 http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=25731



From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Idan Zaltzberg
Sent: Sunday, December 20, 2009 2:57 AM
To: users at lists.ironpython.com
Subject: [IronPython] Bug in Ipy 2.6 when pickling exceptions

Hi,
I'm using IronPython 2.6 final.
When deriving from Exception class, and creating a constructor with a different signature (not 1 argument), I get an exception when pickling and unpickling.
Seems like the pickling process "neglects" some of the information so the unpickling fails.
This can be reproduced with:

>>> class my_exc(Exception):
...     def __init__(self, message, param):
...             super(my_exc, self).__init__(message)
...             self.param = param
...
>>> e = my_exc('message','param')
>>> import pickle
>>> pickle.loads(pickle.dumps(e))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Systems\3rd_party\IronPython\2.6\Lib\pickle.py", line 1374, in loads
  File "C:\Systems\3rd_party\IronPython\2.6\Lib\pickle.py", line 858, in load
  File "C:\Systems\3rd_party\IronPython\2.6\Lib\pickle.py", line 1133, in load_reduce
TypeError: __init__() takes exactly 3 arguments (2 given)


Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20091220/c12a624f/attachment.html>


More information about the Ironpython-users mailing list