Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

.net - Debugging IronPython scripts in hosted (embedded) environment

I'm writing a C# application which has IronPython (2.0.1) embedded in it. The idea is to expose portions of the application to the IronPython scripts, which the users write.

I want to provide the ability to the users to be able to debug the scripts written by them, using the Visual Studio Debugger. Note that the scripts are run in the hosted environment and not through the IronPython executable (ipy.exe).

After a bit of Reflector magic on the IronPython assemblies, I came up with something which lets me do that, but I'm not sure if this is the prescribed way. Basically what I do is create a "ScriptRuntime" object with the "DebugMode" property set to true and then create a python based "ScriptEngine" from the "ScriptRuntime", which I use for hosting. Code below.

        ScriptRuntimeSetup setup = new ScriptRuntimeSetup();
        setup.DebugMode = true;
        setup.LanguageSetups.Add(Python.CreateLanguageSetup(null));

        ScriptRuntime runtime = new ScriptRuntime(setup);
        ScriptEngine engine = runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName);

Now when I execute the scripts in the hosted environment, using:

            ScriptSource script = engine.CreateScriptSourceFromFile(path);
            CompiledCode code = script.Compile();
            ScriptScope scope = engine.CreateScope();
            script.Execute(scope);

I can place breakpoints in the script files and they get hit, when the script is executed.

So, is there a better/easier way to do this?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OK, got it. There is an options dictionary which "Python.CreateEngine" can take as an argument. One can specify the debug mode in that.

        Dictionary<string, object> options = new Dictionary<string, object>();
        options["Debug"] = true;
        engine = Python.CreateEngine(options);

I think this is straightforward enough.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...