在托管(嵌入式)环境中调试IronPython脚本 [英] Debugging IronPython scripts in hosted (embedded) environment

查看:404
本文介绍了在托管(嵌入式)环境中调试IronPython脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个嵌入了IronPython(2.0.1)的C#应用​​程序。这个想法是将应用程序的某些部分暴露给用户写的IronPython脚本。



我想提供使用Visual Studio Debugger能够调试用户编写的脚本的能力。请注意,这些脚本在托管环境中运行,而不是通过IronPython可执行文件(ipy.exe)运行。



在IronPython程序集上有一点Reflector魔法之后,我来了可以让我这样做的东西,但我不知道这是否是规定的方式。基本上我所做的是创建一个DebugMode属性设置为true的ScriptRuntime对象,然后从ScriptRuntime中创建一个基于python的ScriptEngine,用于托管。

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

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

现在,当我在托管环境中执行脚本时,使用:

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

我可以在脚本文件中放置断点,并在脚本执行时得到命中。 p>

所以,有没有更好/更容易的方法呢?



谢谢

解决方案

有一个选项字典Python.CreateEngine可以作为参数。可以在其中指定调试模式。

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

我觉得这很简单。


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

解决方案

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.

这篇关于在托管(嵌入式)环境中调试IronPython脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆