[IronPython] pickling exceptions and differences between IronPythonand cpython
Bruce Christensen
t-bruch at microsoft.com
Fri Jul 14 20:48:11 CEST 2006
Also, as a workaround, you can add a simple __getstate__ method to the
Exception class that ignores clsException if it's present:
C:\Ip\IronPython>ipy
IronPython 1.0.2386 (Beta) on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import sys; sys.path.append(r'c:\Python24\lib')
>>> import pickle
>>> try:
... a
... except Exception, e:
... pass
...
>>> e
<exceptions.NameError instance at 0x000000000000002B>
>>> pickle.dumps(e)
Traceback (most recent call last):
File , line 0, in <stdin>##44
File c:\Python24\lib\pickle.py, line 1386, in dumps
File c:\Python24\lib\pickle.py, line 231, in dump
File c:\Python24\lib\pickle.py, line 293, in save
File c:\Python24\lib\pickle.py, line 739, in save_inst
File c:\Python24\lib\pickle.py, line 293, in save
File c:\Python24\lib\pickle.py, line 663, in save_dict
File c:\Python24\lib\pickle.py, line 677, in _batch_setitems
File c:\Python24\lib\pickle.py, line 313, in save
TypeError: can't pickle NameError instance (non-default __reduce__
needed)
>>> def getstate(self):
... dict = self.__dict__.copy()
... if 'clsException' in dict:
... del dict['clsException']
... return dict
...
>>> Exception.__getstate__ = getstate
>>> pickle.dumps(e)
'(iexceptions\nNameError\np0\n(dp2\nS\'msg\'\np3\nS"name \'a\' not
defined"\np4\
nsS\'args\'\np5\n(g4\ntp6\nsb.'
>>>
-----Original Message-----
From: Bruce Christensen
Sent: Friday, July 14, 2006 9:34 AM
To: 'Discussion of IronPython'
Subject: RE: [IronPython] pickling exceptions and differences between
IronPythonand cpython
Thanks for the feedback! We should definitely support pickling
exceptions that originated as CLR exceptions. I've opened
http://www.codeplex.com/WorkItem/View.aspx?ProjectName=IronPython&WorkIt
emId=945 to track it.
--Bruce
-----Original Message-----
From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Jason Ferrara
Sent: Friday, July 14, 2006 8:48 AM
To: users at lists.ironpython.com
Subject: [IronPython] pickling exceptions and differences between
IronPythonand cpython
Using the pickle modules from cpython to pickle built in exceptions
fails under IronPython because IronPython adds a clsException
attribute which pickle can't handle. And example is below.
Should this be considered a bug?
IronPython 1.0.60712 (Beta) on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import pickle
>>> a=[]
>>> try:
... a[4]
... except Exception, e:
... pickle.dumps(e)
...
Traceback (most recent call last):
File , line 0, in <stdin>##43
File z:\iptest\pickle.py, line 1386, in dumps
File z:\iptest\pickle.py, line 231, in dump
File z:\iptest\pickle.py, line 293, in save
File z:\iptest\pickle.py, line 739, in save_inst
File z:\iptest\pickle.py, line 293, in save
File z:\iptest\pickle.py, line 663, in save_dict
File z:\iptest\pickle.py, line 677, in _batch_setitems
File z:\iptest\pickle.py, line 313, in save
TypeError: can't pickle IndexOutOfRangeException instance (non-
default __reduce__ needed)
>>> pickle.dumps(e.clsException)
Traceback (most recent call last):
File , line 0, in <stdin>##72
File z:\iptest\pickle.py, line 1386, in dumps
File z:\iptest\pickle.py, line 231, in dump
File z:\iptest\pickle.py, line 313, in save
TypeError: can't pickle IndexOutOfRangeException instance (non-
default __reduce__ needed)
>>> del e.clsException
>>> pickle.dumps(e)
"(iexceptions\nIndexError\np0\n(dp1\nS'msg'\np2\nS'index out of
range: 4'\np3\nsS'args'\np4\n(g3\ntp5\nsb."
>>>
while in cpython:
Python 2.4.2 (#1, Jun 14 2006, 09:51:02)
[GCC 4.0.1 (Apple Computer, Inc. build 5247)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> a=[]
>>> try:
... a[4]
... except Exception, e:
... pickle.dumps(e)
...
"(iexceptions\nIndexError\np0\n(dp1\nS'args'\np2\n(S'list index out
of range'\np3\ntp4\nsb."
>>>
_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
More information about the Ironpython-users
mailing list