Note that there are some explanatory texts on larger screens.

plurals
  1. POEmbedding IronPython, built-in help command, my CLR objects
    text
    copied!<p>I'm embedding IronPython (2.6.1) in a C# assembly and exposing several objects to scripts which are executed with PythonEngine.ExecuteFile. I expose them either with</p> <pre><code>scope.SetVariable("SomeObject", new SomeObject()) </code></pre> <p>or</p> <pre><code>engine.Execute("from MyNamespace import SomeObject", scope) </code></pre> <p>depending on how the scripts use them. My application assembly is added to the engine with</p> <pre><code>engine.Runtime.LoadAssembly(Assembly.GetExecutingAssembly()) </code></pre> <p>Now a script can execute <code>help(SomeObject)</code> and dump the nice little help info(*), however <strong>it's incomplete</strong>. None of the object's events or properties (public of course) show up and many of the 'built-in' members are missing as well.</p> <p>Here's the odd part; If I fire up ipy.exe and execute the following:</p> <pre><code>import sys sys.path.append('&lt;location of my app&gt;') import clr clr.AddReferenceToFile('myapp.exe') from MyNamespace import SomeObject help(SomeObject) </code></pre> <p>I get a different dump, complete with all the missing members!</p> <p><strong>Why do the two differ?</strong></p> <p><em>Bonus question</em>: Assuming I get it working correctly, is it possible to add descriptive text on my CLR objects to the output of help()? Like you can from within the script, on your python-native types? My first guess was the DescriptionAttribute, but that didn't work.</p> <p>(*) Obviously a final, working script wouldn't do this but it is exceedingly helpful while writing/testing the script.</p> <p><strong>Answered</strong></p> <p>Here is a complete console program that illustrates how to import the site which replaces the usless internal help() with the standard python library help().</p> <pre><code>using System; using System.Collections.Generic; using System.Reflection; using IronPython.Hosting; using IronPython.Runtime; using Microsoft.Scripting.Hosting.Providers; namespace MyApp { class Program { static void Main(string[] args) { // Work around issue w/ pydoc - piping to more doesn't work so instead indicate that we're a dumb terminal if (Environment.GetEnvironmentVariable("TERM") == null) Environment.SetEnvironmentVariable("TERM", "dumb"); var engine = Python.CreateEngine(); // Add standard Python library path (is there a better way to do this??) PythonContext context = HostingHelpers.GetLanguageContext(engine) as PythonContext; ICollection&lt;string&gt; paths = context.GetSearchPaths(); paths.Add(@"C:\Program Files (x86)\IronPython 2.6\Lib"); context.SetSearchPaths(paths); // Import site module engine.ImportModule("site"); engine.Runtime.LoadAssembly(Assembly.GetEntryAssembly()); var scope = engine.CreateScope(); scope.SetVariable("SomeObject", new SomeObject()); engine.Execute("help(SomeObject)", scope); } } /// &lt;summary&gt; /// Description of SomeObject. /// &lt;/summary&gt; public class SomeObject { /// &lt;summary&gt; /// Description of SomeProperty. /// &lt;/summary&gt; public int SomeProperty { get; set; } /// &lt;summary&gt; /// Description of SomeMethod. /// &lt;/summary&gt; public void SomeMethod() { } /// &lt;summary&gt; /// Description of SomeEvent. /// &lt;/summary&gt; public event EventHandler SomeEvent; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload