Created
December 9, 2014 18:05
-
-
Save ironpythonbot/ea4551ecb020c5855d75 to your computer and use it in GitHub Desktop.
CodePlex Issue #33242 Plain Text Attachments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Security; | |
using System.Security.Permissions; | |
using System.Security.Policy; | |
using IronPython.Compiler; | |
using IronPython.Hosting; | |
using IronPython.Runtime; | |
using Microsoft.Scripting.Hosting; | |
namespace StressTests | |
{ | |
class Program | |
{ | |
static AppDomain _AppDomain; | |
static void Main(string[] args) | |
{ | |
string script = "i = 0"; | |
ScriptEngine engine = CreateEngine(true); | |
ScriptSource src = engine.CreateScriptSourceFromString(script); | |
CompiledCode code = src.Compile(); | |
while (true) | |
{ | |
ScriptScope scope = engine.CreateScope(); | |
code.Execute(scope); | |
} | |
} | |
static ScriptEngine CreateEngine(bool isSandboxed) | |
{ | |
if (isSandboxed) | |
_AppDomain = CreateAppDomain(); | |
else | |
_AppDomain = AppDomain.CurrentDomain; | |
Dictionary<String, Object> options = new Dictionary<string, object>(); | |
options["LightweightScopes"] = true; | |
var engine = Python.CreateEngine(_AppDomain, options); | |
PythonCompilerOptions pco = (PythonCompilerOptions)engine.GetCompilerOptions(); | |
pco.Module &= ~ModuleOptions.Optimized; | |
return engine; | |
} | |
static AppDomain CreateAppDomain() | |
{ | |
var setup = new AppDomainSetup(); | |
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; | |
var ps = new PermissionSet(PermissionState.None); | |
ps.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); | |
ps.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, AppDomain.CurrentDomain.BaseDirectory)); | |
var TrustedAssemblies = new Type[] | |
{ | |
typeof(DateTime), | |
typeof(Uri), | |
typeof(IronPython.BytesConversionAttribute), | |
typeof(IronPython.Modules.ArrayModule), | |
typeof(Enumerable) | |
}.Select(t => t.Assembly.Evidence.GetHostEvidence<StrongName>()).ToArray(); | |
return AppDomain.CreateDomain("Sandbox", null, setup, ps, TrustedAssemblies); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment