Embedding IronPython – C# Calling Python Script Part 1

Embedding IronPython is quite trivial and is documented here.

You can call functions in the embedded python module by
(1) retrieving the function using Evaluate()
(2) then apply the function using the Call() method

// IronPython 1.x only
using (IronPython.Hosting.PythonEngine engine 
  = new IronPython.Hosting.PythonEngine())
{
  engine.Execute(@"
  def foo(a, b):
    return a+b*2");

  // (1) Retrieve the function 
  IronPython.Runtime.Calls.ICallable foo
   = (IronPython.Runtime.Calls.ICallable) engine.Evaluate("foo");

  // (2) Apply function 
  object result = foo.Call(3, 25);
}

In IronPython 2.x, ICallable was removed in favour of dynamic sites.

// IronPython 2.x
using (IronPython.Hosting.PythonEngine engine 
  = new IronPython.Hosting.PythonEngine())
{
  engine.Execute(@"
  def foo(a, b):
    return a+b*2");

  // (1) Retrieve the function 
  dynamic foo
   = engine.Evaluate("foo");

  // (2) Check it is callable
  if (IronPython.Runtime.Operations.PythonOps.IsCallable(DefaultContext.Default, function))
  {
    // (3) Apply function 
    object result = foo(3, 25);
  }
}

The ICallable.Call method can only be called varargs-style, and can’t be called using keyword arguments.
(This is probably a bug.)

Call(params object[] args)

To use keyword arguments, you’ll need to cast the function into PythonFunction.

using (IronPython.Hosting.PythonEngine engine 
  = new IronPython.Hosting.PythonEngine())
{
  engine.Execute(@"
  def foo(*args, **kwargs):
    return 'args=%s kwargs=%s' % (args, kwargs)");

  // (1) Retrieve the function. It might be a method or a function
  dynamic foo
   = engine.Evaluate("foo");

  // (2) Apply function/method
  object results = IronPython.Runtime.Operations.PythonCalls.CallWithKeywordArgs(
     DefaultContext.Default,
     function,
     new object[] { "arg1", "arg2", "arg3" },
     new Dictionary<object, object>()
     {
        {"keywordarg1", 101},
        {"keywordarg2", 202},
        {"keywordarg3", 303}
     });;
}

Note:
Unfortunately, Python classes aren’t CLR classes, and couldn’t be accessed directly or subclassed by C# by referencing a built assembly. You’ll have to go through the hosting interfaces to get an instance of the class.


About this entry