[IronPython] IronPython.Runtime.Types.PythonType Is not marked as Serializable Exception

mohammad mustaq mustaq2001 at gmail.com
Wed Apr 7 07:10:39 CEST 2010


Hi Dino,

If have tweaked your code to reproduce the exception that I am facing. Let
me know if you need more details.

thanks,
Mustaq

using System;
using Microsoft.Scripting;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

class Foo
{
    public static void Main(string[] args)
    {
        AppDomain ad = AppDomain.CreateDomain("foo");
        var engine = Python.CreateEngine(ad);
        engine.Runtime.LoadAssembly(
typeof(MbrBase).Assembly);

        var code = engine.CreateScriptSourceFromString(@"
import MbrBase
class C(MbrBase):
    pass

a = C()
", SourceCodeKind.Statements);

        var scope = engine.CreateScope();
        code.Execute(scope);

        Console.WriteLine("Trying to do it... {0}",
AppDomain.CurrentDomain.Id <http://appdomain.currentdomain.id/>);
        MbrBase mbr = (MbrBase)scope.GetVariable("a");

//     MY CHANGES

        string isSubClassCode = String.Format("issubclass({0},{1})", "C",
"MbrBase");
        ScriptSource script =
engine.CreateScriptSourceFromString(isSubClassCode,
SourceCodeKind.Expression);
        bool result = (bool)script.Execute(scope);

        if (result == true)
        {
            ObjectOperations ops = engine.Operations;

            object subClass = scope.GetVariable("C");
            object instance = ops.Call(subClass);

            mbr = instance as MbrBase;
        }

//      END OF MY CHANGE

        mbr.DoItVirtually();
        mbr.DoIt();
        Console.ReadKey();
    }
}

public class MbrBase : MarshalByRefObject
{
    public virtual void DoItVirtually()
    {
        Console.WriteLine("Did it virtually {0}",
AppDomain.CurrentDomain.Id<http://appdomain.currentdomain.id/>
);
    }

    public void DoIt()
    {
        Console.WriteLine("Did it {0}",
AppDomain.CurrentDomain.Id<http://appdomain.currentdomain.id/>
);
    }
}


On Tue, Mar 30, 2010 at 10:12 PM, Dino Viehland <dinov at microsoft.com> wrote:

>  This works for me w/ 2.6.  I’ve included my simple repro below which
> creates a new script engine in a remote app domain, loads my assembly in,
> runs some code which subclasses the MBRO base class, instantiates an
> instance of this class, and then calls it from a remote app domain.  The key
> thing here is that when an MBRO is involved a PythonType should not need to
> be serialized – the type should live in the remote app domain and all
> execution of that code should also happen in the remote app domain where we
> have access to the local PythonType object.  Are you also subclassing types
> which don’t derive from MBRO?  It might help to run IronPython w/
> -X:ExceptionDetail if the exception is propagating through IronPython –
> that’ll give a better stack trace to understand what’s going on.  Or if you
> can tweak the simple repro below to match the behavior you’re seeing that’d
> be helpful as well.
>
>
>
> using System;
>
> using Microsoft.Scripting;
>
> using IronPython.Hosting;
>
>
>
> class Foo {
>
>     public static void Main(string[] args) {
>
>         AppDomain ad = AppDomain.CreateDomain("foo");
>
>         var engine = Python.CreateEngine(ad);
>
>         engine.Runtime.LoadAssembly(typeof(MbrBase).Assembly);
>
>
>
>         var code = engine.CreateScriptSourceFromString(@"
>
> import MbrBase
>
> class C(MbrBase):
>
>     pass
>
>
>
> a = C()
>
> ", SourceCodeKind.Statements);
>
>
>
>         var scope = engine.CreateScope();
>
>         code.Execute(scope);
>
>
>
>         Console.WriteLine("Trying to do it... {0}",
> AppDomain.CurrentDomain.Id);
>
>         MbrBase mbr = (MbrBase)scope.GetVariable("a");
>
>         mbr.DoItVirtually();
>
>         mbr.DoIt();
>
>     }
>
> }
>
>
>
> public class MbrBase : MarshalByRefObject {
>
>     public virtual void DoItVirtually() {
>
>         Console.WriteLine("Did it virtually {0}",
> AppDomain.CurrentDomain.Id);
>
>     }
>
>
>
>     public void DoIt() {
>
>         Console.WriteLine("Did it {0}", AppDomain.CurrentDomain.Id);
>
>     }
>
> }
>
>
>
> *From:* users-bounces at lists.ironpython.com [mailto:
> users-bounces at lists.ironpython.com] *On Behalf Of *mohammad mustaq
> *Sent:* Monday, March 29, 2010 8:13 PM
> *To:* users at lists.ironpython.com
> *Subject:* [IronPython] IronPython.Runtime.Types.PythonType Is not marked
> as Serializable Exception
>
>
>
> Hi,
>
> I have IronPython embedded in my C# application. I face issues while
> creating the python engine in a different appdomain.
>
> It is imposed that every class in IronPython inherit the .NET base class
> say ClassA. ClassA is derived from MarshalByRefObj as I need to pass an
> instance of this class to a new appdomain.
> I create a new appdomain and pass the instance of ClassA to this Appdomain.
> While calling a method in python class through the instance of ClassA I get
> an exception mentioning that "Type 'IronPython.Runtime.Types.PythonTyp
>
> e' in Assembly 'IronPython, Version=2.0.0.0, Culture=neutral,
> PublicKeyToken=31bf3856ad364e35' is not marked as serializable".
>
> How do I Serialize this PythonType. One way that i know is to modify the
> IronPython source and mark the required types as Serializable (but i do not
> know where it will lead to and its consequences). Could you please suggest a
> way to perform the required operation. Should you need more details let me
> know.
>
> thanks,
> Mustaq
>
> P.S. I have used both IronPython 2.0 and 2.6.
>
> _______________________________________________
> Users mailing list
> 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/20100407/0245b603/attachment.html>


More information about the Ironpython-users mailing list