0
votes

I cannot find up to date syntax for putting an Ironpython ScriptEngine into it's own AppDomain in C#.

The only syntax I have seen so far is by creating the ScriptEngine like so:

AppDomain sandbox = AppDomain.CreateDomain("sandbox");
ScriptEngine engine = Python.CreateEngine( sandbox );

This does not work, at least not in IPY 2.7.7, resulting in the following Exception:

SerializationException: Could not find type 'System.Collections.Generic.IList`1[[Microsoft.Scripting.Hosting.LanguageSetup, Microsoft.Scripting, Version=1.1.2.22, Culture=neutral, PublicKeyToken=7f709c5b713576e1]]'.
(wrapper xdomain-invoke) System.AppDomain:CreateInstanceAndUnwrap (string,string,bool,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo,object[],System.Security.Policy.Evidence)
(wrapper remoting-invoke-with-check) System.AppDomain:CreateInstanceAndUnwrap (string,string,bool,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo,object[],System.Security.Policy.Evidence)
Microsoft.Scripting.Hosting.ScriptRuntime.CreateRemote (System.AppDomain domain, Microsoft.Scripting.Hosting.ScriptRuntimeSetup setup)
IronPython.Hosting.Python.CreateRuntime (System.AppDomain domain)
IronPython.Hosting.Python.CreateEngine (System.AppDomain domain) 

I have also tried to give the full set of parameters as I understand them as seen here: http://answers.unity3d.com/questions/242901/problem-with-appdomainspermissionset-ironpython-in.html

But since the syntax of creating the Scriptengine with the AppDomain as it's parameter is deprecated or not available in 2.7.7, it results in the same error.

As well, I do need to add a dictionary of the Ironpython options like "FullFrames" etc., which is what Python.CreateEngine expects.

I need to put Ironpython in it's own AppDomain since there appears to be some sort of memory leak that causes crashes in Unity after a few assembly loads, or multiple instantiations of the engine ( which I do null out and shutdown the runtime, and garbage collect when the main routine has finished in IPY )

Here is what I am working with so far:

Dictionary<string, object> options = new Dictionary<string, object>();
options["Frames"] = true;
options["FullFrames"] = true;
options["LightweightScopes"] = true;
options["ShowClrExceptions"] = true;
options["ExceptionDetail"] = true;

AppDomain sandbox = AppDomain.CreateDomain("sandbox");
// Don't know where to go from here to get my engine into the AppDomain.

engine = Python.CreateEngine(options);
scope = engine.CreateScope();

var paths = engine.GetSearchPaths();
paths.Add(@"C:\Python\IronPython2.7.7\Lib");
paths.Add(@"C:\Python\IronPython2.7.7\Lib\site-packages");
engine.SetSearchPaths(paths);

scope.SetVariable("test", "12345");

try
{
    source.Execute(scope);
}
catch (SystemExitException e)
{
    Debug.Log("IronPython exited with the following code ( " + e + " )");
}
catch (Exception e)
{
    ExceptionOperations eo = engine.GetService<ExceptionOperations>();
    string error = eo.FormatException(e);
    Debug.Log("<color=red>IPYError : </color>" + error);
}

engine.Runtime.Shutdown();
engine = null;
runtime = null;
source = null;
scope = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
1

1 Answers

0
votes

The issue was that you must first load the assembly for Microsoft.Scripting.Hosting into the appdomain, along with all it's dependent assemblies, such as MSCorLib.dll. Since the IronPython ScriptEngine assembly is meant to be in it's own AppDomain, it no longer has access to the assemblies such as 'System.Collections.Generic.IList'. Once I did this, this line worked:

engine = Python.CreateEngine( sandbox, options);

As well, it fixed the Unity crashes, containing the IronPython memory leak. Downside is that you will not be able to use IronPython to manipulate the editor directly, short of some fancy permissions/Hotswapping etc.

At least no crashes anymore which is a god-send.