2016-04-27 9 views
0

IronPythonエンジンインスタンスへの参照を追加しようとすると、参照が期待通りに参照に追加されます。エンジンの別のインスタンスを作成すると、AddReferenceはエラーなしで実行されますが、参照は参照に追加されず、importステートメントは "no module named ..."で失敗します。2番目のIronPythonエンジンでclr.AddReferenceが失敗するのはなぜですか?

var engine = Python.CreateEngine(); 
dynamic clr = engine.Runtime.GetClrModule(); 
clr.AddReference("IronPython.StdLib"); 
var references = (IEnumerable<Assembly>)clr.References; 
Debug.Assert(references.Any(asm => asm.FullName.StartsWith("IronPython.StdLib"))); // ok 

var source = "import pydoc\npydoc.plain(pydoc.render_doc(str))"; 
var result = engine.Execute<string>(source); 
Debug.Assert(result.StartsWith("Python Library Documentation")); // ok 

var engine2 = Python.CreateEngine(); 
dynamic clr2 = engine2.Runtime.GetClrModule(); 
clr2.AddReference("IronPython.StdLib"); 
var references2 = (IEnumerable<Assembly>)clr.References; 
Debug.Assert(references2.Any(asm => asm.FullName.StartsWith("IronPython.StdLib"))); // fails 

result = engine.Execute<string>(source); // throws ImportException "no module named pydoc" 
Debug.Assert(result.StartsWith("Python Library Documentation")); 

Iは、(GACにインストールされている)とC#4.5、IronPython.StdLibはPYCとPythonの標準IIbのプリコンパイルされたDLLであるIronPythonの2.7.5のバイナリリリースしようとしました。

また、githubからの自己コンパイル済みのIronPython 2.7.5および2.7.6でも試しましたが、最初にengine.executeが参照が追加されていますが、で既に失敗しています。

私は何か間違っているのですか、それとも単なるバグですか?

答えて

0

同僚は、失敗の理由を発見しました。他の誰かがこの問題を突き抜けた場合に備えて、ここに投稿します。

あなたは、参照を追加した後のアセンブリをロードする必要があります。

var engine = Python.CreateEngine(); 
dynamic clr = engine.Runtime.GetClrModule(); 
clr.AddReference("IronPython.StdLib"); 

// load assembly into engine 
var assembly = Assembly.LoadFrom("IronPython.StdLib.dll"); 
engine.Runtime.LoadAssembly(assembly); 

var references = (IEnumerable<Assembly>)clr.References; 
Debug.Assert(references.Any(asm => asm.FullName.StartsWith("IronPython.StdLib"))); // ok 

var source = "import pydoc\npydoc.plain(pydoc.render_doc(str))"; 
var result = engine.Execute<string>(source); 
Debug.Assert(result.StartsWith("Python Library Documentation")); // ok 

var engine2 = Python.CreateEngine(); 
dynamic clr2 = engine2.Runtime.GetClrModule(); 
clr2.AddReference("IronPython.StdLib"); 

// load assembly into engine2 
engine2.Runtime.LoadAssembly(assembly); 

var references2 = (IEnumerable<Assembly>)clr.References; 
Debug.Assert(references2.Any(asm => asm.FullName.StartsWith("IronPython.StdLib"))); // does not fail 

result = engine.Execute<string>(source); // does not throw any more 
Debug.Assert(result.StartsWith("Python Library Documentation")); 
関連する問題