> What is "gs" in InitDbUtils, should that be gl?

yes, sorry

Also can you include the line of code where you're actually executing
the code from your host?
The main code (the lambda definitions etc) is executed like this:

ScriptSource source = _scriptEngine.CreateScriptSourceFromString(script_string, file_path, SourceCodeKind.Statements);
source.Execute(_scriptScope);

then, the main entry point is executed separately:

_scriptEngine.Execute("Start()", _scriptScope)

where Start() is a function in MSSqlServerUtils.py.

Are you setting ScriptRuntime.Globals to
_scriptScope?
I've tested with and without it, same result

Because I think Globals may have changed meaning between
1.1 and 2.0/2.6.  In 2.x Globals is made available via imports but I don't
remember how it was exposed in 1.1.  I think what you want to do is call the
Execute overload that takes a ScriptScope and pass in _scriptScope.
Alternately you want to create a ScriptSource, Compile it, and then Execute
it against the _scriptScope.  But it's hard to tell w/o the full context.
Hope it is clear now. The actual code is pretty big, and it is part of a company project so I can't send it fully. In essence, what I am trying to do is execute some c# methods from python scripts -but don't want to expose the whole class to the scripts, only the methods as functions.

Ernesto


-----Original Message-----
From: users-boun...@lists.ironpython.com [mailto:users-
boun...@lists.ironpython.com] On Behalf Of Ernesto Cullen
Sent: Tuesday, January 05, 2010 11:25 AM
To: users@lists.ironpython.com
Subject: [IronPython] can't execute method in globals

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(CallS
ite
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

_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


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

_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to