tags:

views:

210

answers:

2

We embed ironpython in our app sob that scripts can be executed in the context of our application.

I use Python.CreateEngine() and ScriptScope.Execute() to execute python scripts.

We have out own editor(written in C#) that can load ironpython scripts and run it.

There are 2 problems I need to solve.

  1. If I have a print statement in ironpython script, how can i show it my editor(how will I tell python engine to redirect output to some handler in my C# code)

  2. I plan to use unittest.py for running unittest. When I run the following runner = unittest.TextTestRunner() runner.run(testsuite)

the output is redirected to standard output but need a way for it to be redirected to my C# editor output window so that user can see the results. This question might be related to 1

Any help is appreciated G

+2  A: 

You can provide a custom Stream or TextWriter which will be used for all output. You can provide those by using one of the ScriptRuntime.IO.SetOutput overloads. Your implementation of Stream or TextWriter should receive the strings and then output them to your editor window (potentially marshalling back onto the UI thread if you're running the script on a 2ndary execution thread).

Dino Viehland
+1  A: 

Here's a snippet for sending output to a file.

       System.IO.FileStream fs = new System.IO.FileStream("c:\\temp\\ipy.log", System.IO.FileMode.Create);
       engine.Runtime.IO.SetOutput(fs, Encoding.ASCII);

For sending the GUI to a UI control, like a text box, you would need to create your own stream and have it implement a Write method that would print to the text box.

Tom E