[IronPython] Serializing IronPython classes

Dino Viehland dinov at microsoft.com
Tue Oct 7 18:55:50 CEST 2008


Ok, upon looking into this some more, I'm sad to say it looks like this won't work right now :(.  We need to figure out a way to run the serialization for just the base class and how to do that isn't entirely obvious - so it probably won't make 2.0.  I've opened a bug to track the issue though: http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=18823  Feel free to vote on it so we'll fix it sooner.

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Serge
Sent: Monday, October 06, 2008 9:15 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Serializing IronPython classes

Okay now this is getting interesting. I assumed that the warning was about the child class, but I'm not sure. A bit of info: The parent C# class is called Object. I did the override on on __reduce_ex__  as you suggested but trying to pickle still produced the same error:

Type 'IronPython.NewTypes.Engine.Object_1$2' in Assembly 'Snippets.scripting, Version=0.0.0.0<http://0.0.0.0>, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

For curiosity, I called __reduce_ex__ on a child instance and got the following error: expected Vector2, got Object_1$2

There are several Microsoft.Xna.Framework.Vector2 members and it looks like they're being replaced by this Object_1$2 which I assumed at first to be the child class because of its name.
__reduce_ex__ on the parent instances works fine. I'm out of my depth here so I can't give more meaningful information.
On 10/7/08, Dino Viehland <dinov at microsoft.com<mailto:dinov at microsoft.com>> wrote:

Ahh, that sounds like a bad bug, but I think I know what's causing it - we're hitting the new.NET serialization support because __reduce_ex__ is now defined for you :)  Can you add an override that dispatches __reduce_ex__ to the object version, eg:



def __reduce_ex__(self, *args):

    return object.__reduce_ex__(self, *args)



We should probably do that automatically for user-defined instances which should be easy to do if this works for you.



From: users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com> [mailto:users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com>] On Behalf Of Serge
Sent: Monday, October 06, 2008 7:36 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Serializing IronPython classes



Thanks for the heads up, however trying to use cPickle, I still get the error saying that the child class is not marked as serializable..?

On 10/7/08, Dino Viehland <dinov at microsoft.com<mailto:dinov at microsoft.com>> wrote:

I would strongly encourage you to use cPickle or pickle instead of .NET serialization.  In 2.0 all .NET serializable types can also be pickled - they define __reduce_ex__ which handles this.



First off we should be setting the serializable bit on subclasses that are serializable - that's just a bug that we're not doing that.  But once we've done that the problem w/ .NET serialization is that ultimately we need a static method or type that we can point at that does the deserialization.  For a user defined type in Python we will need to be able to deserialize the type, the module the type lives in, and presumably even the ScriptRuntime which holds the module.  Pickle handles this by serializing the module & type name but w/o a ScriptRuntime we couldn't even get at that.  We might have been able to require a ScriptRuntime to be smuggled in the StreamingContext but it's not clear that it would work well.



So if you really want .NET serialization we can fix the bug - but you'll need to implement ISerializable and figure out some way to deal getting the class, module, and runtime information saved/restored yourself.



From: users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com> [mailto:users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com>] On Behalf Of Serge
Sent: Sunday, October 05, 2008 9:24 PM
To: users at lists.ironpython.com<mailto:users at lists.ironpython.com>
Subject: [IronPython] Serializing IronPython classes



I've run into problems with serialization. I have a serializable class defined in C# which gets extended from IP, however when I try to serialize a collection of both parent and child instances, I get an exception saying that the IP generated class is not marked as serializable.

With the lack of attributes, I am guessing I must do something else to enable serialization?

Regards, Serge.

_______________________________________________
Users mailing list
Users at lists.ironpython.com<mailto:Users at lists.ironpython.com>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



_______________________________________________
Users mailing list
Users at lists.ironpython.com<mailto:Users at lists.ironpython.com>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20081007/03b89eb2/attachment.html>


More information about the Ironpython-users mailing list