[IronPython] C# and IronPython Interface.

Kaveripakam, Sathish Sathish.Kaveripakam at phonak.com
Wed Nov 28 02:30:23 PST 2007


Hi All,

Please find my updated Entry.cs and ArithmeticOperations.py files:

Entry.cs :

using System;
using System.Collections;
using System.IO;
using System.Text;
using IronPython.Compiler;
using IronPython.Hosting;
using IronPython.Runtime;
using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;


public class Entry
{
    public static void Main(string[] args)
    {
        int a = 2, b = 2, x = 3;

        PythonEngine pe = new PythonEngine();
        EngineModule mod = pe.CreateModule();              

        pe.Execute(File.ReadAllText("ArithmeticOperations.py"), mod);
        object func = mod.Globals["ArithmeticOperations"];

        Object res = Ops.Call(func, a, b);
        Console.WriteLine(res);

    }
}

ArithmeticOperations.py:


def ArithmeticOperations(a,b):		
		return 


When I executed the above project (which includes IronMath and
IronPython dll's for V1.1), I find the run time exception, when it is
trying to execute the line

	
"pe.Execute(File.ReadAllText("ArithmeticOperations.py"), mod);" 

The trace for the above problem is as follows:

System.IO.IOException was unhandled
  Message="Could not add reference to assembly Microsoft.Scripting"
  Source="IronPython"
  StackTrace:
       at IronPython.Modules.ClrModule.AddReference(String name)
       at IronPython.Modules.ClrModule.AddReference(Object reference)
       at IronPython.Modules.ClrModule.AddReference(Object[] references)
       at AddReference##11(Object , Object )
       at IronPython.Runtime.Calls.CallTarget2.Invoke(Object arg0,
Object arg1)
       at IronPython.Runtime.Calls.FastCallable2.Call(ICallerContext
context, Object arg0, Object arg1)
       at IronPython.Runtime.Calls.FastCallable2.Call(ICallerContext
context, Object[] args)
       at
IronPython.Compiler.MethodBinder.MethodTarget.Call(ICallerContext
context, Object[] args)
       at IronPython.Compiler.MethodBinder.TargetSet.Call(ICallerContext
context, CallType callType, Object[] args)
       at
IronPython.Compiler.MethodBinder.TargetSet.Call2(ICallerContext context,
Object arg0, Object arg1)
       at
IronPython.Runtime.Calls.FastCallableWithContextAny.CallInstance(ICaller
Context context, Object arg0, Object arg1)
       at
IronPython.Runtime.Calls.BoundBuiltinFunction.Call(ICallerContext
context, Object arg0)
       at
IronPython.Runtime.Operations.Ops.CallWithContext(ICallerContext
context, Object func, Object arg0)
       at <string>##1(ModuleScope )
       at IronPython.Hosting.CompiledCodeDelegate.Invoke(ModuleScope
moduleScope)
       at IronPython.Hosting.CompiledCode.Run(ModuleScope moduleScope)
       at IronPython.Hosting.CompiledCode.Execute(EngineModule
engineModule, IDictionary`2 locals)
       at IronPython.Hosting.PythonEngine.Execute(String scriptCode,
String fileName, EngineModule engineModule, IDictionary`2 locals)
       at IronPython.Hosting.PythonEngine.Execute(String scriptCode,
EngineModule engineModule)
       at Entry.Main(String[] args) in
C:\TESTPROJS\Project3\Project1\Entry.cs:line 53
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
       at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
       at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()


In this regard, I wanted to know am I missing something primitive in the
Project Settings ?

Regards

 

-----Original Message-----
From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Dienstag, 27. November 2007 18:32
To: Discussion of IronPython
Subject: Re: [IronPython] C# and IronPython Interface.

You're going to want to do something like (only compiled w/ Outlook, so
there may be some small compile errors here):

PythonEngine pe = new PythonEngine();
EngineModule mod = pe.CreateModule();
pe.Execute(File.ReadAllText("ArithmeticOperations.py"), mod); object
func = mod.Globals["ArithmeticOperations"];

That gets you the function...  From there you can call it one of several
ways:

Object res = Ops.Call(func, a, b);

        Or

delegate object MyDelegate(object a, object b);

MyDelegate dlg = Converter.Convert<MyDelegate>(func);
Object res = dlg(a, b);

In v1.x you can also use the CreateLambda/CreateMethod APIs if you have
the body of the code.  In general we prefer the 2nd form more where you
convert it to a delegate because it fits in better with .NET.  You can
also do things like have the delegate take strongly typed parameters and
there's the possibility that could turn into more optimized code in v2.0
at some point in the future.

From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Kaveripakam,
Sathish
Sent: Monday, November 26, 2007 8:01 AM
To: users at lists.ironpython.com
Subject: [IronPython] C# and IronPython Interface.

HI All,

I have c#  code which should call a Iron Python function. I am using
Iron python 1.1 version and Visual Studio 2005. The details of my files
are as follows:


 C# code (Entry.cs):

using System;
using System.Collections;
using IronPython.Hosting;
public class Entry
{
            public static void Main(string[] args)
            {
                        int a= 2, b= 4;
                        // OVER HERE I WOULD LIKE TO CALL IRONPYTHON
Function: ArithmeticOperations which takes arguments a,b
                }
}
PYTHON SCRIPT(ArithmeticOperations.py):
def ArithmeticOperations( a,b):
print "I am in ArithmeticOperation functions..."
return  a+b

In my project setup, I gave reference to include : IronPython, IronMath
dll's In this regard, please do let me know, how to call the python
script "Arithmetic Operations" from my Entry.cs file ? Also, please do
let me know, if I have to include any of the libraries ?
Regards,
Sathish



________________________________________
Legal Notice:
The information in this electronic transmission may contain confidential
or legally privileged information and is intended solely for the
individual(s) named above. If you are not an intended recipient or an
authorized agent, you are hereby notified that reading, distributing, or
otherwise disseminating, copying or taking any action based on the
contents of this transmission is strictly prohibited. Any unauthorized
interception of this transmission is illegal under law. If you have
received this transmission in error, please notify the sender by
telephone [at the number indicated above/on +41 58 928 0101] as soon as
possible and then destroy all copies of this transmission.
________________________________________
_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
******************** 
Legal Notice: 
The information in this electronic transmission may contain confidential or
legally privileged information and is intended solely for the individual(s)
named above. If you are not an intended recipient or an authorized agent,
you are hereby notified that reading, distributing, or otherwise
disseminating, copying or taking any action based on the contents of this
transmission is strictly prohibited. Any unauthorized interception of this
transmission is illegal under law. If you have received this transmission in
error, please notify the sender by telephone [at the number indicated above/
on +41 58 928 0101] as soon as possible and then destroy all copies of this
transmission. 
********************


More information about the Users mailing list