[IronPython] Calling a Python function with passed in args/kwargs from C#

Dino Viehland dinov at microsoft.com
Sat Apr 24 01:27:15 CEST 2010


Jeff wrote:
> I'm trying to call a Python function from C# using args/kwargs from
> the C# function, like so:
> 
>     public void Frob(CodeContext context, [ParamDictionary]IDictionary
> kwargs, params object[] args)
>     {
>         context.LanguageContext.Operations.Invoke(foo, args, kwargs);
>     }
> 
> foo is a Python callable object - in this case it's just:
> 
>     def foo(*args, **kwargs):
>         print args
>         print kwargs
> 
> so that I can what is actually passed to it.
> 
> I'd like to call foo with the equivalent of Python's foo(*args,
> **kwargs). Passing it the way I did above just passes the arguments
> into args without expanding them. Is there any way to trigger the
> argument expansion from C#?


I think you want PythonCalls.CallWithKeywordArgs.  You'll need an IDictionary<object, object>
instead of a plain old IDictionary though (or you'll need to unpack the args for the other
overload). Either way we end up calling this with a binder which  should splat both arguments.

If perf is important for this call we could talk about opening up 
PythonContext.Invoke(CallSignature) so you could create a call site which uses whatever
signature you want.




More information about the Ironpython-users mailing list