2011-01-27 10 views
2

IronTextBoxControl2のフォークに[rlcompleter][1]機能を追加しようとしています(RevitPythonShell)。 rlcompleter.pyIronPython 2.0のホスティング時に "__main__"モジュールを公開するには?

import __main__ 

しかし、私はラインとハードのトラブルを抱えています。これは私がそのようなモジュールを持っていないためです。私は、IronPython 2.0のコードベースを調べて、モジュールを公開する方法を見つけようとしています。

私はIronPython.Runtime.PythonContextのインスタンスからPublishModule(string name, PythonModule module)という方法でそうすることができます。しかし、私は次のように問題抱えている:

  • は私がを持って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
    • ある
    • もしPythonのためにScriptEngineがあるならば、ちょうどキャストできます。

    うわー。だから今私は最終的に私のPythonContext!だから私はCreateModulePublishModuleに電話をかけ、紅茶の前に家に帰るつもりです!

    待機。まず、CreateModuleの2番目の引数はMicrosoft.Scripting.Runtime.Scopeです。私たちが持っているのはMicrosoft.Scripting.Hosting.ScriptScopeです。アクセサーがinternalの場合を除いて、Scopeが含まれます。だから私たちは最初にいくつかの反省をする必要があります。ここで

    は私の提案されたソリューションによって拡張私のサンプルコードです:

    /// <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; 
    } 
    
    関連する問題