I'd just use ObjectOperations for this one - that should be as simple as: engine.Operations.Call(type);
and pass any arguments you want. -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Slide Sent: Thursday, May 29, 2008 2:42 PM To: Discussion of IronPython Subject: Re: [IronPython] hosting and SetVariable Dino, Thanks for all the help, it has helped me move forward quite a bit! It turns out I had an error in the python that was being covered up by the way I was doing some things. I fixed that and am left with one last question. Given a PythonType, it looks like there are a bunch of different ways to create an instance through __new__ calls. My question is, where do I get the CodeContext from? I know I can create a new one using "new," but it requires a Scope object, which I don't have HostingHelpers to get from a ScriptScope. So, can you give me a quick example of creating an instance if I have a PythonType? Something that will hopefully be useful going forward as well :-) Thanks! slide On Thu, May 29, 2008 at 1:22 PM, Dino Viehland <[EMAIL PROTECTED]> wrote: > There's something strange going on. This small program on 2.0B2: > > using System; > using System.Collections.Generic; > using System.Reflection; > > using Microsoft.Scripting; > using Microsoft.Scripting.Hosting; > > class Test { > public static void Main(string[]args) { > ScriptRuntime runtime = ScriptRuntime.Create(); > runtime.Globals.ClearVariables(); > ScriptEngine engine = runtime.GetEngine("py"); > CompiledCode code = > engine.CreateScriptSourceFromFile("test.py").Compile(); > ScriptScope scope = code.MakeOptimizedScope(); > code.Execute(scope); > foreach(KeyValuePair<string, object> kvp in scope.Items) { > Console.WriteLine("{0} {1}", kvp.Key, kvp.Value); > } > } > } > > With test.py containing: > > class x(object): pass > > prints: > > __builtins__ IronPython.Runtime.PythonDictionary > x IronPython.Runtime.Types.PythonType > > I'm not sure what the difference would be off the top of my head as that's > basically what you're doing minus the additional setup. Can you try reducing > some of the additional steps and seeing if you can get a basic type to show > up? > > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Slide > Sent: Thursday, May 29, 2008 12:52 PM > To: Discussion of IronPython > Subject: Re: [IronPython] hosting and SetVariable > > ScriptScope.Item doesn't have my class definition in there. Perhaps I > am not doing the initial setup correctly. > > > _runtime.Globals.ClearVariables(); > > _engine = _runtime.GetEngine("py"); > > foreach (KeyValuePair<string, object> obj in _objects) > _runtime.Globals.SetVariable(obj.Key, obj.Value); > > _error_listener = new ScriptErrorListener(); > > // setup the paths to search for modules > List<string> paths = new List<string>(); > paths.Add(Environment.CurrentDirectory); > > paths.Add(Path.Combine(Environment.CurrentDirectory, "Lib")); > paths.Add(Path.GetDirectoryName(path)); > > string[] probe_paths = > Directory.GetDirectories(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)); > paths.AddRange(probe_paths); > > _engine.SetScriptSourceSearchPaths(paths.ToArray()); > > CompiledCode code = > _engine.CreateScriptSourceFromFile(path).Compile(_error_listener); > _scope = code.MakeOptimizedScope(); > code.Execute(_scope); > _engine.PublishModule("__main__", _scope); > > // add all the references the "Host" wanted. > foreach (string assembly in _assemblies) > _runtime.LoadAssembly(Assembly.LoadFrom(assembly)); > > > The code I am passing in contains the following: > > import ScriptTester > > class MyClass(ScriptTester.ITest): > def DoIt(self, msg): > print msg > > When I iterate over _scope.Items, I don't see "MyClass" in the list of items. > > Thanks > > On Thu, May 29, 2008 at 12:46 PM, Dino Viehland > <[EMAIL PROTECTED]> wrote: >> Sorry, I didn't realize that was a recent addition. Can you just iterate >> over ScriptScope.Items - I checked and it's there in B2 :). >> >> As for beta 3 the plan is sometime in June. >> >> >> -----Original Message----- >> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Slide >> Sent: Thursday, May 29, 2008 12:39 PM >> To: Discussion of IronPython >> Subject: Re: [IronPython] hosting and SetVariable >> >> That class is not available in Beta2. I also downloaded the latest >> source and checked the implementation of GetScope, it calls >> ScriptScope.Scope, which also doesn't exist in Beta2. Is Beta3 with >> all this stuff coming out soon? >> >> thanks >> >> On Thu, May 29, 2008 at 12:17 PM, Seshadri Pillailokam Vijayaraghavan >> <[EMAIL PROTECTED]> wrote: >>> It's fully qualified path is >>> (Runtime\)'Microsoft.Scripting.Hosting.HostingHelpers' >>> >>> >>> -----Original Message----- >>> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Slide >>> Sent: Thursday, May 29, 2008 12:13 PM >>> To: Discussion of IronPython >>> Subject: Re: [IronPython] hosting and SetVariable >>> >>> It may be able to be accomplished with ObjectOperations. What I am >>> trying to do is find all the types defined in my ScriptScope and >>> determine if they are subclasses (or implementers) of another type. I >>> used to do this using the PythonType class with UnderlyingSystemType >>> and so forth, but all that stuff is gone in the beta 2 I downloaded. >>> Also, I didn't see HostingHelpers.GetScope anywhere, is this from an >>> external library (I checked Microsoft.Scripting, >>> Microsoft.Scripting.Core and the IronPython assemblies) >>> >>> On Thu, May 29, 2008 at 11:56 AM, Dino Viehland >>> <[EMAIL PROTECTED]> wrote: >>>> Unfortunately there isn't any specific documentation. Can what you used >>>> to do be accomplished with the ObjectOperations class? >>>> >>>> Otherwise there's a HostingHelpers.GetScope which you could use to get a >>>> Scope from a ScriptScope and you can probably re-use your old magic. >>>> >>>> -----Original Message----- >>>> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Slide >>>> Sent: Thursday, May 29, 2008 11:34 AM >>>> To: Discussion of IronPython >>>> Subject: Re: [IronPython] hosting and SetVariable >>>> >>>> Thanks that will be helpful. Is there any documentation specific to >>>> IronPython? I would like to do some introspection on modules and such >>>> (to retrieve types defined in the module and so forth) and I used to >>>> do that with some PythonType magic which doesn't seem to work anymore >>>> (ScriptScope is not convertable to Scope when calling Get__dict__). >>>> >>>> Thanks again! >>>> >>>> On Thu, May 29, 2008 at 10:11 AM, Seshadri Pillailokam Vijayaraghavan >>>> <[EMAIL PROTECTED]> wrote: >>>>> The latest Hosting API Spec is available at >>>>> http://compilerlab.members.winisp.net/. >>>>> It's available as both doc >>>>> (http://compilerlab.members.winisp.net/dlr-spec-hosting.doc) or PDF >>>>> (http://compilerlab.members.winisp.net/dlr-spec-hosting.pdf). >>>>> >>>>> Thanks >>>>> Sesh >>>>> -----Original Message----- >>>>> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Slide >>>>> Sent: Thursday, May 29, 2008 8:42 AM >>>>> To: Discussion of IronPython >>>>> Subject: Re: [IronPython] hosting and SetVariable >>>>> >>>>> Apparently I need to update to the latest IP2.0 beta. I don't even see >>>>> CurrentManager.Globals, thanks for the info. >>>>> >>>>> On Thu, May 29, 2008 at 8:37 AM, Dino Viehland >>>>> <[EMAIL PROTECTED]> wrote: >>>>>> What you want to do is put the variables in >>>>>> ScriptDomainManager.Globals/ScriptRuntime.Globals and then you can >>>>>> import them. For example: >>>>>> >>>>>> ScriptDomainManager.CurrentManager.Globals.SetVariable("hello_world", >>>>>> "hi!"); >>>>>> >>>>>> Then you can do: >>>>>> >>>>>> import hello_world >>>>>> >>>>>> and we'll pick it up from globals. Just FYI >>>>>> IronPython.Hosting.PythonEngine is going away (along w/ CurrentEngine) >>>>>> so you're better off creating a ScriptRuntime and getting a ScriptEngine >>>>>> from it by name. >>>>>> >>>>>> -----Original Message----- >>>>>> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Slide >>>>>> Sent: Thursday, May 29, 2008 8:14 AM >>>>>> To: Discussion of IronPython >>>>>> Subject: [IronPython] hosting and SetVariable >>>>>> >>>>>> Perhaps this is just missing knowledge on my part, but here goes. >>>>>> >>>>>> I am using IP2.0 with the hosting API to add scripting support to my >>>>>> application. I use the following code to create my main module from >>>>>> the script file the user selects: >>>>>> >>>>>> ICompiledCode code = >>>>>> IronPython.Hosting.PythonEngine.CurrentEngine.Compile(SourceUnit.CreateFileUnit(_context, >>>>>> path), _error_sink); >>>>>> __main__ = code.MakeModule("__main__") as ScriptScope; >>>>>> >>>>>> // create our module. >>>>>> ScriptDomainManager.CurrentManager.PublishModule(__main__, "__main__"); >>>>>> >>>>>> // add all the references the "Host" wanted. >>>>>> foreach (string assembly in _assemblies) >>>>>> >>>>>> Microsoft.Scripting.ClrModule.GetInstance().AddReference(Path.GetFileNameWithoutExtension(assembly)); >>>>>> >>>>>> // set all the variables that the "Host" wanted set. >>>>>> foreach (KeyValuePair<string, object> obj in _objects) >>>>>> __main__.SetVariable(obj.Key, obj.Value); >>>>>> >>>>>> The last foreach is setting up my object model which is available to >>>>>> my scripts. My problem arises when I want to use one of these >>>>>> variables from a script that is imported from the user selected script >>>>>> file. >>>>>> >>>>>> For instance, if I add an object _objects.Add("MyObj", someObj), I can >>>>>> reference that object in the user script as MyObj with no problem. >>>>>> What I would like to do is have these variables global to all scripts >>>>>> that are imported, etc. I tried import __main__ in my other scripts, >>>>>> but __main__ doesn't show up in sys.modules (which is what I thought >>>>>> PublishModule would do, but thats a different story). >>>>>> >>>>>> Am I going about this the wrong way? >>>>>> >>>>>> Thanks >>>>>> >>>>>> slide >>>>>> >>>>>> -- >>>>>> slide-o-blog >>>>>> http://slide-o-blog.blogspot.com/ >>>>>> _______________________________________________ >>>>>> Users mailing list >>>>>> [email protected] >>>>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com >>>>>> _______________________________________________ >>>>>> Users mailing list >>>>>> [email protected] >>>>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com >>>>>> >>>>> >>>>> >>>>> >>>>> -- >>>>> slide-o-blog >>>>> http://slide-o-blog.blogspot.com/ >>>>> _______________________________________________ >>>>> Users mailing list >>>>> [email protected] >>>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com >>>>> _______________________________________________ >>>>> Users mailing list >>>>> [email protected] >>>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com >>>>> >>>> >>>> >>>> >>>> -- >>>> slide-o-blog >>>> http://slide-o-blog.blogspot.com/ >>>> _______________________________________________ >>>> Users mailing list >>>> [email protected] >>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com >>>> _______________________________________________ >>>> Users mailing list >>>> [email protected] >>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com >>>> >>> >>> >>> >>> -- >>> slide-o-blog >>> http://slide-o-blog.blogspot.com/ >>> _______________________________________________ >>> Users mailing list >>> [email protected] >>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com >>> _______________________________________________ >>> Users mailing list >>> [email protected] >>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com >>> >> >> >> >> -- >> slide-o-blog >> http://slide-o-blog.blogspot.com/ >> _______________________________________________ >> Users mailing list >> [email protected] >> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com >> _______________________________________________ >> Users mailing list >> [email protected] >> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com >> > > > > -- > slide-o-blog > http://slide-o-blog.blogspot.com/ > _______________________________________________ > Users mailing list > [email protected] > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > _______________________________________________ > Users mailing list > [email protected] > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > -- slide-o-blog http://slide-o-blog.blogspot.com/ _______________________________________________ Users mailing list [email protected] http://lists.ironpython.com/listinfo.cgi/users-ironpython.com _______________________________________________ Users mailing list [email protected] http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
