2011-01-27 3 views
2

IronTextBoxControl2 포크에 [rlcompleter][1] 기능을 추가하려고합니다 (RevitPythonShell). rlcompleter.py에서IronPython 2.0을 호스팅 할 때 "__main__"모듈을 게시하는 방법은 무엇입니까?

import __main__ 

:

그러나, 나는 라인 하드에 문제가 있어요. 내가 그런 모듈을 가지고 있지 않기 때문입니다. 필자는 IronPython 2.0 코드베이스를 검색하여 모듈을 게시하는 방법을 찾아 내려고했습니다.

으로 IronPython.Runtime.PythonContext의 인스턴스에서 처리 할 수 ​​있습니다. 하지만 다음에 문제가 있어요 :

  • 내가이 무엇 PythonModule 예를

을 얻는 PythonContext 예를

  • 를 얻기를하는 ScriptEngineScriptScope입니다. 범위를 모듈 __main__으로 게시하고 싶습니다. 내가 알기로 범위는 이미 모듈에 부착되어 있지만 액세스 방법을 알 수는 없습니다. 여기

    내가 함께이 넥타이 싶습니다 코드의 포인트입니다 :

    /// <summary> 
        /// Set up an IronPython environment - for interactive shell or for canned scripts 
        /// </summary> 
        public void SetupEnvironment(ScriptEngine engine, ScriptScope scope) 
        {       
         // add variables from Revit 
         scope.SetVariable("__revit__", _commandData.Application); 
         scope.SetVariable("__commandData__", _commandData); 
         scope.SetVariable("__message__", _message); 
         scope.SetVariable("__elements__", _elements); 
         scope.SetVariable("__result__", (int)Result.Succeeded);       
    
         // add preconfigures variables 
         scope.SetVariable("__vars__", RevitPythonShellApplication.GetVariables()); 
    
         /* register scope as __main__ module here?! */   
    
         // add the search paths 
         AddSearchPaths(engine); 
        } 
    
  • 답변

    1

    은 신경 쓰지 마, 내가 IronPython의 2.0.3 소스 코드를 가서 해낸 다음

    • PythonContextScriptEngine의 언어 컨텍스트 Microsoft.Scripting.Hosting.Providers.HostingHelpers.GetLanguageContext(ScriptEngine) 방법을 통해 얻어 질 수 Microsoft.Scripting.Runtime.LanguageContext
    • 이다
    • 파이썬에 대해 ScriptEngine이 있으면 잘 던질 수 있습니다.

    와우. 그래서 이제 마침내 내 PythonContext! 그래서 저는 CreateModulePublishModule으로 전화를 걸어 차에 집에 갈 것입니다!

    대기. 첫째, CreateModule의 두 번째 인수는 Microsoft.Scripting.Runtime.Scope입니다. 우리가 가진 것은 Microsoft.Scripting.Hosting.ScriptScope입니다. Scope이 포함되며, 접근자는 internal입니다. 그래서 우리는 먼저 약간의 성찰을해야합니다. 여기

    는 내 제안 된 솔루션이 내 샘플 코드를 증강한다 : 나는 목적에 유형의 모든 네임 스페이스를 유지

    /// <summary> 
    /// Set up an IronPython environment - for interactive shell or for canned scripts 
    /// </summary> 
    public void SetupEnvironment(ScriptEngine engine, ScriptScope scriptScope) 
    { 
        // add variables from Revit 
        scriptScope.SetVariable("__revit__", _commandData.Application); 
        scriptScope.SetVariable("__commandData__", _commandData); 
        scriptScope.SetVariable("__message__", _message); 
        scriptScope.SetVariable("__elements__", _elements); 
        scriptScope.SetVariable("__result__", (int)Result.Succeeded);   
    
        // add preconfigures variables 
        scriptScope.SetVariable("__vars__", RevitPythonShellApplication.GetVariables()); 
    
        // add the current scope as module '__main__' 
        var languageContext = Microsoft.Scripting.Hosting.Providers.HostingHelpers.GetLanguageContext(engine); 
        var pythonContext = (IronPython.Runtime.PythonContext)languageContext; 
        var module = pythonContext.CreateModule(null, GetScope(scriptScope), null, IronPython.Runtime.ModuleOptions.None);    
        pythonContext.PublishModule("__main__", module); 
    
        // add the search paths 
        AddSearchPaths(engine); 
    } 
    
    /// <summary> 
    /// Be nasty and reach into the ScriptScope to get at its private '_scope' member, 
    /// since the accessor 'ScriptScope.Scope' was defined 'internal'. 
    /// </summary> 
    private Microsoft.Scripting.Runtime.Scope GetScope(ScriptScope scriptScope) 
    { 
        var field = scriptScope.GetType().GetField(
         "_scope", 
         System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); 
        return (Microsoft.Scripting.Runtime.Scope) field.GetValue(scriptScope); 
    } 
    

    . 필자는 IronPython 코드를 따르기가 어렵다는 것을 알았고 어디에 정의되어 있는지 혼란스러워합니다.

    2

    같은 문제가 발생했습니다. ScriptEngine.Execute *를 사용하여 코드를 실행하려고했습니다. ScriptEngine.CreateScriptSourceFromFile을 사용하여 밝혀진 다음 ExecuteProgram을 호출하면 문제가 해결됩니다.trouble executing Selenium python unittests in C# with ScriptEngine (.NET 3.5)

    참고 : 다음 전체 솔루션을 다음

    체크 아웃은 IronPython 2.7은이 모든 일이 쉽게 :

    public void SetupEnvironment(ScriptEngine engine, out ScriptScope scope) 
    { 
        scope = IronPython.Hosting.Python.CreateModule(engine, "__main__"); 
        return; 
    } 
    
    관련 문제