[IronPython] PythonEngine in separate AppDomain?

Dino Viehland dinov at exchange.microsoft.com
Thu May 24 18:16:28 CEST 2007

The only change I had to make to get your simple sample code to work was change PythonInstance to derive from MarshalByRefObject.  I wonder if typeof(Program).Assembly.Location could end up pointing to a location which is not fully trusted (e.g. a network share?)  It seems highly unlikely but it's the only explanation I can come up with (.NET determines what level of trust to grant assemblies based upon what zone the code came from - where the zone is the same zones that IE uses).

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

using IronPython.Hosting;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Modules;

namespace ConsoleApplication1
    public class PythonInstance : MarshalByRefObject
        // IronPython Modules
        private static PythonEngine _python;

        public PythonInstance()
            // Create Python Engine
            EngineOptions options = new EngineOptions();
            options.ExceptionDetail = true;
            options.ClrDebuggingEnabled = true;

            PythonInstance._python = new PythonEngine(options);

        public void AddPythonPath(string path)

    class Program
        static void Main(string[] args)
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationName = Assembly.GetEntryAssembly().FullName;
            setup.ApplicationBase = Path.GetDirectoryName(typeof(Program).Assembly.Location);

            AppDomain domain = AppDomain.CreateDomain("PythonDomain", null, setup);

            // Create Test Engine in remote domain
            object obj = domain.CreateInstanceAndUnwrap("ConsoleApplication1",
            PythonInstance python = obj as PythonInstance;


From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of michael_sweeney at agilent.com
Sent: Wednesday, May 23, 2007 9:22 AM
To: users at lists.ironpython.com
Subject: Re: [IronPython] PythonEngine in separate AppDomain?

Hi Dino,

I get this error with or without the permission code. Here is the problem I am trying to solve...

We have a test tool that allows the load/reload of Python test scripts. It is implemented in CPython. We manipulate the sys.modules dictionary to "clean" out any modules the test module loaded. This "cleaning" allows the developers to quickly run/edit/reload/run the test scripts. We are porting this application to IronPython by hosting the PythonEngine in a C# application. The new tool allows the user to load .NET assemblies as well as the classic python modules. The ability to reload is handy. Since it is possible to unload AppDomains for the assembly based tests, I was thinking that just putting the PythonEngine in a separate AppDomain I could handle the unload the same way. This is when I started running into permission issues.

I wanted to avoid the "cleaning" of the sys.modules dictionary because it has proved error-prone in the CPython implementation. I may have to start going down this path.

Thanks for your feedback.


From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Tuesday, May 22, 2007 1:54 PM
To: Discussion of IronPython
Subject: Re: [IronPython] PythonEngine in separate AppDomain?

Are you getting this even if you don't do anything w/ permissions?

You're going to have a tough time running IronPython in partial trust before .NET 2.0 SP1 or Orcas.  In SP1 of the CLR (which is the CLR included w/ Orcas and I believe will ship stand-alone as its own SP at some point) various portions of Reflection.Emit have had the appropriate security hardening to ensure they work properly in partial trust.  Unfortunately both of those are still beta code :(.

Thanks in advance...


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20070524/8cc4a105/attachment.html>

More information about the Ironpython-users mailing list