[IronPython] can't execute method in globals

Ernesto Cullen ecullen at fibertel.com.ar
Tue Jan 5 20:24:51 CET 2010


hi all
     I have problems trying to update (from Ipy 1.1) code which uses 
IronPython scripts from an app in C#. If someone can shed a light on why 
this does not work, please...

Ernesto Cullen


I have this code in Ipy 1.1:

EngineOptions engineOptions = new EngineOptions();
engineOptions.ExceptionDetail = true;
engineOptions.ClrDebuggingEnabled = true;
engineOptions.ShowClrExceptions = true;
_pythonEngine = new PythonEngine(engineOptions);

_pythonEngine.Globals["DbUtils"] = new DbUtils(); //class with db 
methods like ExecuteQuery
...

Then I execute a script like this

MSSqlServerUtils.py
def InitDbUtils():
   gl = globals()
   dbu = gs["DbUtils"]
   gl["ExecuteQuery"] = lambda q: dbu.ExecuteQuery(q)
...
InitDbUtils()

dbInfo = ExecuteQuery("SELECT ISNULL(SERVERPROPERTY('InstanceName'),'') 
instanceName")  # (*)
...

This works in Ipy 1.1, the query is executed, and returns a DataTable 
from the ExecuteQuery method of DbUtils class.

In Ipy 2.6 I changed the initialization:
_scriptEngine = Python.CreateEngine(options); //options is a 
Dictionary<String,Object>
_scriptScope = _scriptEngine.CreateScope();
_scriptScope.SetVariable("DbUtils", new DbUtils());
...
and when the (*) line is reached, an exception is thrown:

'MSSqlServerUtils' object has no attribute 'ExecuteQuery'
    at 
IronPython.Runtime.Binding.PythonGetMemberBinder.FastErrorGet`1.GetError(CallSite 
site, TSelfType target, CodeContext context)
    at 
System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite 
site, T0 arg0, T1 arg1)
    at <unnamed>$3.<lambda$1204>$1204(PythonFunction $function, Object 
q) in DbUtils.py:line 11
    at IronPython.Runtime.PythonFunction.FunctionCaller`1.Call1(CallSite 
site, CodeContext context, Object func, T0 arg0)
    at 
System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite 
site, T0 arg0, T1 arg1, T2 arg2)
    at <unnamed>$4.__init__$1243(PythonFunction $function, Object self) 
in MSSQLServer.py:line 157
    at IronPython.Runtime.PythonFunction.FunctionCaller`1.Call1(CallSite 
site, CodeContext context, Object func, T0 arg0)
    at 
System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite 
site, T0 arg0, T1 arg1, T2 arg2)
    at CallSite.Target(Closure , CallSite , CodeContext , Object )
    at 
System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite 
site, T0 arg0, T1 arg1)
    at CallSite.Target(Closure , CallSite , CodeContext , Object )
    at 
System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite 
site, T0 arg0, T1 arg1)
    at <unnamed>$4.Start$1244(PythonFunction $function) in 
MSSQLServer.py:line 162
    at IronPython.Runtime.PythonFunction.FunctionCaller.Call0(CallSite 
site, CodeContext context, Object func)
    at 
System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite 
site, T0 arg0, T1 arg1)
    at 
Microsoft.Scripting.Interpreter.DynamicInstruction`3.Run(InterpretedFrameframe)
    at 
Microsoft.Scripting.Interpreter.Interpreter.RunInstructions(InterpretedFrame 
frame)
    at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame 
frame)
    at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 
arg0, T1 arg1)
    at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
    at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
    at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
    at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
    at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink 
errorSink)
    at Microsoft.Scripting.SourceUnit.Execute(Scope scope)
    at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
    at Microsoft.Scripting.Hosting.ScriptEngine.Execute(String 
expression, ScriptScope scope)

-- 
Tell me and I forget. Teach me and I remember. Involve me and I learn.
Benjamin Franklin




More information about the Ironpython-users mailing list