Now I am trying to make the internals visible to the scripts. I tried with
[assembly: InternalsVisibleToAttribute("MyDemoProject,PublicKey=blahblahblah")]
and[assembly: InternalsVisibleToAttribute("IronPython,PublicKey=blahblahblah")]
but it did not succeed... is there a way to make the internals visible to the scripts?
Ernesto El 05/01/2010 17:29, Dino Viehland escribió:
Is there anything different from the code below vs. what you're trying to do? Because this works for me in 2.6. It would seem like the only possible difference (based upon the error) is that there's some access ExecuteQuery on an MSSqlServerUtils object which I'm not seeing. If that's the case it may be that the type is internal or ExecuteQuery is not a public method on it. I seem to recall that we've tightened up some member access rules between 1.1 and 2.6. using System; using IronPython.Hosting; using IronPython.Runtime; using IronPython.Runtime.Operations; using Microsoft.Scripting; using Microsoft.Scripting.Hosting; using IronPython.Runtime.Exceptions; using System.Runtime.CompilerServices; using Microsoft.Scripting.Runtime; public class Foo { public static void Main(string[] args){ var pe = Python.CreateEngine(); var scope = pe.CreateScope(); scope.SetVariable("test", new Foo()); pe.Execute(@" def f(): x = globals() x[""foo""] = lambda : x[""test""].X() f() foo() ", scope); } public void X() { Console.WriteLine("Called"); } }-----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:58 AM To: Discussion of IronPython Subject: Re: [IronPython] can't execute method in globals > What is "gs" in InitDbUtils, should that be gl? yes, sorryAlso 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 resultBecause 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' atIronPython.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) atMicrosoft.Scripting.Interpreter.DynamicInstruction`3.Run(InterpretedFrameframe) atMicrosoft.Scripting.Interpreter.Interpreter.RunInstructions(InterpretedFrameframe) 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_______________________________________________ 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