Re: [IronPython] IronPython and C# not giving the same result
I was able to successfully run that python code under both IronPython 2.0.3 and IronPython 2.6.1 and it produced a list of all top-level applications. I modified the one line if rae.Current.Name == 'Untitled - Notepad': to be if not rae.Current.Name == '': just to see what was actually being found. Note: I'm running Windows XP SP3. From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of yngipy hernan Sent: Tuesday, June 15, 2010 10:09 PM To: Discussion of IronPython Subject: [IronPython] IronPython and C# not giving the same result Hi All, I have tried to transliterate the following C# code to IronPython: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Automation; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { AutomationElement eNode; eNode = TreeWalker.ControlViewWalker.GetFirstChild(AutomationElement.RootElement); while(eNode != null) { if (eNode.Current.Namehttp://eNode.Current.Name == Untitled - Notepad) { AutomationElement ae; ae = TreeWalker.ControlViewWalker.GetFirstChild(eNode); System.Console.WriteLine(---); while (ae != null) { System.Console.WriteLine(Name: {0}, ae.Current.Namehttp://ae.Current.Name); ae = TreeWalker.ControlViewWalker.GetNextSibling( ae); } System.Console.WriteLine(---); } eNode = TreeWalker.ControlViewWalker.GetNextSibling(eNode); } } } } The output of this program looks like: --- Name: test1 Name: Untitled - Notepad Name: Application --- This is my IronPython code: import clr clr.AddReference('UIAutomationTypes') clr.AddReference('UIAutomationProvider') clr.AddReference('UIAutomationClient') import System.Windows.Automation as swu rae = swu.TreeWalker.ControlViewWalker.GetFirstChild( swu.AutomationElement.RootElement ) while rae: if rae.Current.Namehttp://rae.Current.Name == 'Untitled - Notepad': print '-'*24 ae = swu.TreeWalker.ControlViewWalker.GetFirstChild( rae ) while ae: print ae.Current.Namehttp://ae.Current.Name ae = swu.TreeWalker.ControlViewWalker.GetNextSibling( ae ) print '-'*24 rae = swu.TreeWalker.ControlViewWalker.GetNextSibling( rae ) The output of this shows: test1 I read somewhere that IronPython used to have issues with UI Automation. Is still the case? Regards, Yngipy ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] WPF/IronPython event handlers question - multiple event handlers
This link, http://msdn.microsoft.com/en-us/vcsharp/bb508935.aspx, has more information about event handlers and the order in which they are called. The order is not specified in the CLR reference and therefore you should not depend on a specific order. From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Curt Hagenlocher Sent: Monday, June 07, 2010 2:41 PM To: Discussion of IronPython Subject: Re: [IronPython] WPF/IronPython event handlers question - multiple event handlers These are CLR-level issues. The first basically comes down to the fact that there's really no way to enumerate the handlers which have already been added to an event. As for the second, there is no contract that defines the order on which the handler will be called. But I suspect that the default implementation of an event -- which corresponds to just declaring public event EventHandler MyEvent; in C# -- would probably give consistent in-order results. On Mon, Jun 7, 2010 at 1:26 PM, Ken MacDonald drken...@gmail.commailto:drken...@gmail.com wrote: Hi, I've got a whole stack of event handlers, assigned via the usual something like my_button.Click += button_was_clicked As it turns out, the initialization for the thing that has all these handlers may need to be run several times, and it seems like I ended up with multiple copies of the same event handler, and button_was_clicked() now gets invoked multiple times for EACH time I click my_button. It seems to be solved by just skipping the handler assignment if I've already done it before, but it seems odd that the same EH would be assigned and fired twice. Is this really what I'm seeing? I can see a case for assigning multiple different EH's: my_button.Click += event_handler_1 my_button.Click += event_handler_2 but then this begs the question, is there a defined order in which the EH's will fire? I looked thru multiple python/IronPython books and googled, but came up empty on both questions. Hopefully someone here understands these things better than I do Ken ___ Users mailing list Users@lists.ironpython.commailto: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
Re: [IronPython] ImportError: No module named
Richard, Try changing the suffix on the files to be .py instead of .ipy. It appears that in IronPython 2.0 and 2.6, the import statement requires the modules to end in .py -- at least in the situation you are describing. -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Richard Steventon Sent: Thursday, April 08, 2010 1:13 PM To: users@lists.ironpython.com Subject: [IronPython] ImportError: No module named Hi all, I am moving some code from Python to IronPython (latest) so I can access Excel XLSX files. The code is a loose bunch of files in a single directory. ie: main.ipy accessFunctions.ipy Within main.ipy, I do: from accessFunctions import * Which gives: ImportError: No module named accessFunctions Checked the path. That's ok. So I googled, and found Ben Hall's blog: http://blog.benhall.me.uk/2008/05/ironpython-classes-within-separate.html Which shows that it should work Any ideas ? -Bye -Richard ___ 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
Re: [IronPython] socket.getfqdn problem on 2.6.1 RC1.
In IronPython, using socket.getfqdn('127.0.0.1') returns the local host name. Under CPython, this always returns 'localhost'. The CPython documentation for getfqdn() does explicitly state If name is omitted or empty, it is interpreted as the local host. Therefore, if you change your call to leave out the '0.0.0.0', then the code will behave the same on both CPython and IronPython. IronPython: import socket socket.getfqdn() mydomain.name.here CPython: import socket socket.getfqdn() mydomain.name.here -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Carlos Alberto Costa Beppler Sent: Wednesday, April 07, 2010 7:26 AM To: Discussion of IronPython Subject: [IronPython] socket.getfqdn problem on 2.6.1 RC1. Hi, I´m trying to use the HTTPServer class from de BaseHTTPServer.py on standard library on IronPython 2.6.1 RC1. But the server stops with an socket exception on startup as shown bellow. C:\Program Files\Toolsipy IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.3607 Type help, copyright, credits or license for more information. import BaseHTTPServer BaseHTTPServer.test() Traceback (most recent call last): File stdin, line 1, in module File C:\Program Files\IronPython\Lib\BaseHTTPServer.py, line 584, in test File C:\Program Files\IronPython\Lib\SocketServer.py, line 400, in __init__ File C:\Program Files\IronPython\Lib\BaseHTTPServer.py, line 110, in server_ bind ValueError: IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address. Parameter name: hostNameOrAddress This is because the method getfqdn of the socket class is blowing up when the string '0.0.0.0' is passed to it. For example: C:\Program Files\Toolsipy IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.3607 Type help, copyright, credits or license for more information. import socket socket.getfqdn('0.0.0.0') Traceback (most recent call last): File stdin, line 1, in module ValueError: IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address. Parameter name: hostNameOrAddress On CPython the same call returns the current host name, like bellow. C:\Users\bepplerpython Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. import socket socket.getfqdn('0.0.0.0') 'Beppler.mps.interno' Is it possible to fix this for the 2.6.1 release? ___ 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
[IronPython] IronPython 2.6.1 RC1, ctypes, and c_int.value
Thank you for your efforts with IronPython 2.6.1. I am looking forward to the final product. I was testing 2.6.1 RC1 to see if an issue I was having with v2.6.0 was fixed and I ran into an interesting problem with pyReadLine (v1.13), which I had installed under CPython v2.6.4. A python-based program I am testing requires the readline module but I am getting an odd error when readline is loaded. The error is SystemError: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. I created a simple test case that shows the problem in action. This test case runs fine under CPython v2.6.4 but fails under IronPython v2.6.1 RC1. Please ignore the fact that it is really weird-looking code and not something that would normally be used. The test case demonstrates the problem in as few lines as possible while exercising the same steps that readline uses (see the install_readline() function in readline's Console.py). import ctypes msvcrt = ctypes.cdll.LoadLibrary(msvcrt) systemfn = ctypes.c_int.from_address(ctypes.addressof(msvcrt.system)) systemfn.value = ctypes.c_int.from_address(ctypes.addressof(msvcrt.system)).value The last line triggers the error: SystemError: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Apparently readline overwrites a function pointer (PyOS_ReadlineFunctionPointer) inside the DLL that implements the sys module so that the line input functionality passes through the python readline module. Since readline uses the sys.dllhandle to get access to the DLL and in IronPython v2.6.1 RC1 the sys.dllhandle is 0, I'm thinking the pyReadline package won't work on IronPython even if the above error is fixed. ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] IronPython 2.6.1 RC1, ctypes, and c_int.value
Dino, The readline module is patching the PyOS_ReadLineFunctionPointer variable in the DLL that implements the sys module (in CPython's case, this is the Python26.dll). Since IronPython doesn't support intercepting line reads in this way (or at all, apparently), this isn't going to work on IronPython in any event. From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Dino Viehland Sent: Thursday, February 11, 2010 10:40 AM To: Discussion of IronPython Subject: Re: [IronPython] IronPython 2.6.1 RC1, ctypes, and c_int.value I've created http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=26185 but I already have a fix for this. From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Dino Viehland Sent: Thursday, February 11, 2010 10:14 AM To: Discussion of IronPython Subject: Re: [IronPython] IronPython 2.6.1 RC1, ctypes, and c_int.value Actually this looks like a bug in IronPython. ctypes.addressof(msvcrt.system) is returning the wrong value - in CPython it's returning the address which points to the function. In IronPython it's returning the address of the function it's self. Hopefully the real code is doing something more useful than this though because this seems to only be patching the ctypes pointer to the function, not any other pointers to the function. So every call to system() will still go directly to the msvcrt impl. I would assume this code really wanted to patch the exported address over in msvcrt.dll so all callers would be intercepted. This doesn't even patch it if someone gets a 2nd copy of msvcrt via ctypes: On my machine ctypes.addressof(msvcrt.system) is 12ff940 and msvcrt.system is _FuncPtr object at 0x012FF918. Then I do: msvcrt2 = ctypes.cdll.LoadLibrary(msvcrt) msvcrt2.system I get a new _FuncPtr object: msvcrt2.system _FuncPtr object at 0x012FF990 hex(ctypes.addressof(msvcrt2.system)) '0x12ff9b8' And now a different address would be patched. From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Curt Hagenlocher Sent: Thursday, February 11, 2010 7:50 AM To: Discussion of IronPython Subject: Re: [IronPython] IronPython 2.6.1 RC1, ctypes, and c_int.value Wow, that's awful. I imagine that you would need to use VirtualProtect to make that page writeable in order for this to work. I can't see how modifying IronPython's ctypes to make this possible would be a good idea -- the vast majority of uses for ctypes don't involve writing into arbitrary DLL code pages, and it's an extremely useful protection against stray writes. On Thu, Feb 11, 2010 at 7:39 AM, Lepisto, Stephen P stephen.p.lepi...@intel.commailto:stephen.p.lepi...@intel.com wrote: Thank you for your efforts with IronPython 2.6.1. I am looking forward to the final product. I was testing 2.6.1 RC1 to see if an issue I was having with v2.6.0 was fixed and I ran into an interesting problem with pyReadLine (v1.13), which I had installed under CPython v2.6.4. A python-based program I am testing requires the readline module but I am getting an odd error when readline is loaded. The error is SystemError: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. I created a simple test case that shows the problem in action. This test case runs fine under CPython v2.6.4 but fails under IronPython v2.6.1 RC1. Please ignore the fact that it is really weird-looking code and not something that would normally be used. The test case demonstrates the problem in as few lines as possible while exercising the same steps that readline uses (see the install_readline() function in readline's Console.py). import ctypes msvcrt = ctypes.cdll.LoadLibrary(msvcrt) systemfn = ctypes.c_int.from_address(ctypes.addressof(msvcrt.system)) systemfn.value = ctypes.c_int.from_address(ctypes.addressof(msvcrt.system)).value The last line triggers the error: SystemError: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Apparently readline overwrites a function pointer (PyOS_ReadlineFunctionPointer) inside the DLL that implements the sys module so that the line input functionality passes through the python readline module. Since readline uses the sys.dllhandle to get access to the DLL and in IronPython v2.6.1 RC1 the sys.dllhandle is 0, I'm thinking the pyReadline package won't work on IronPython even if the above error is fixed. ___ Users mailing list Users@lists.ironpython.commailto: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
[IronPython] Apparent bug with redirecting output in IronPython 2.6
Hi, I've been evaluating a suite of python code originally targeting CPython 2.5 to see what needs to be fixed/changed to run under IronPython 2.6. I ran into a problem in IronPython 2.6 where I redirect sys.stdout through a class object that forwards output to the original sys.stdout. When control returns to the python command prompt, an exception is displayed in an infinite loop. If I redirect sys.stderr first (to the same object) before redirecting sys.stdout, IronPython 2.6 shows two exception errors then abruptly exits. Since this particular part of my python code runs fine under CPython 2.5, 2.6 and IronPython 2.0.3, I'm thinking it may be a bug in IronPython 2.6. What follows is a reproduction case showing IronPython 2.6 exiting after two exceptions. Note that the redirection is actually working and that the exception occurs only when the python console prompt is displayed. 1. Create a file called testredirect.py with the following code: import sys class _StreamLog(object): def __init__(self, ostream): self.ostream = ostream def write(self, *args): self.ostream.write({) self.ostream.write(*args) self.ostream.write(}) def flush(self): self.ostream.flush() if not sys.stderr.__class__ == _StreamLog: sys.stderr = _StreamLog(sys.stderr) if not sys.stdout.__class__ == _StreamLog: sys.stdout = _StreamLog(sys.stdout) print sys.stderr, Redirected stderr\n print sys.stdout, Redirected stdout\n 2. Open an IronPython 2.6 console and import the testredirect module. This produces the following output: C:\ipy IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.1433 Type help, copyright, credits or license for more information. import testredirect {Redirected stderr }{ }{Redirected stdout }{ }{ }{Traceback (most recent call last): SystemError: Object reference not set to an instance of an object. }Unhandled exception: Traceback (most recent call last): SystemError: Object reference not set to an instance of an object. 4. Rerunning the above test with -X:ExceptionDetail enabled, I get the following: C:\ ipy -X:ExceptionDetail IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.1433 Type help, copyright, credits or license for more information. import testredirect {Redirected stderr }{ }{Redirected stdout }{ }{ }{Object reference not set to an instance of an object. at IronPython.Runtime.OutputWriter.Flush() at Microsoft.Scripting.Hosting.Shell.BasicConsole.Write(String text, Style style) at Microsoft.Scripting.Hosting.Shell.CommandLine.ReadStatement(Boolean continueInteraction) at IronPython.Hosting.PythonCommandLine.RunOneInteraction() at IronPython.Hosting.PythonCommandLine.TryInteractiveActionWorker() at IronPython.Hosting.PythonCommandLine.TryInteractiveAction() at Microsoft.Scripting.Hosting.Shell.CommandLine.RunInteractiveLoop() SystemError: Object reference not set to an instance of an object. }Unhandled exception: Object reference not set to an instance of an object. at IronPython.Runtime.OutputWriter.Flush() at Microsoft.Scripting.Hosting.Shell.BasicConsole.Write(String text, Style style) at Microsoft.Scripting.Hosting.Shell.BasicConsole.WriteLine(String text, Style style) at IronPython.Runtime.Operations.PythonOps.PrintException(CodeContext context, Exception exception, IConsole console) at IronPython.Hosting.PythonCommandLine.UnhandledException(Exception e) at Microsoft.Scripting.Hosting.Shell.CommandLine.RunInteractiveLoop() at IronPython.Hosting.PythonCommandLine.RunInteractive() at Microsoft.Scripting.Hosting.Shell.CommandLine.Run() at IronPython.Hosting.PythonCommandLine.Run() at Microsoft.Scripting.Hosting.Shell.CommandLine.Run(ScriptEngine engine, IConsole console, ConsoleOptions options) at Microsoft.Scripting.Hosting.Shell.ConsoleHost.RunCommandLine() SystemError: Object reference not set to an instance of an object. ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Type analysis of expression
What about treating the return type of the python expression as object then coerce the object into the type required by the database? If the coercion fails, then that could be treated as a type error. For example, after using python to evaluate the expression '2+(4*5)', the returned object could be converted to an integer using System.Convert.ToInt32() or to a string with System.Convert.ToString(). System.Convert throws InvalidCastException for those cases that cannot be converted. -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Michael Foord Sent: Monday, October 19, 2009 4:21 AM To: Discussion of IronPython Subject: Re: [IronPython] Type analysis of expression Christian Schmidt wrote: Hello, we are using IronPython embedded into our application to evaluate user defined expression. The variables used in the expressions are strongly typed and the results need to be written back into a database. I know that the return type of a dynamic expression cannot be determined in general. But what would be a pragmatic way to guess the return type of an expression? Hehe. If you're evaluating arbitrary functions provided by the user then I don't know of any way of 'inferring' the return type - beyond parsing it and modelling the type flow through the expression (which would be a lot of work). All the best, Michael Thanks, Christian ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com -- http://www.ironpythoninaction.com/ http://www.voidspace.org.uk/blog ___ 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
Re: [IronPython] Type analysis of expression
Jonathan, Even if the variables going into the expression are strongly typed, python will evaluate the expression however it can, with the result being some type based on the coercion python applied to each variable. However, I read Christian's problem as one where he needed to get the value into a database and to me that was the deciding factor as to which type to coerce the result of the expression, regardless of the original types of the variables going into the expression. -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Jonathan Hartley Sent: Monday, October 19, 2009 7:19 AM To: Discussion of IronPython Subject: Re: [IronPython] Type analysis of expression Hey all, Stephen, I infer that Christian can't evaluate the expression yet. (If he could, then deriving the type of the result would be trivial) Presumably the variables in the expression don't yet have known values. So he has an expression which cannot be evaluated, but he wants to guess what type the return value would be if the variables in the expression were known. The variables in the expression do have known types. So I wonder if you could pick a prototypical value for each variable, (eg. set all floats to 1.0, and all integers to 1) and then evaluate the expression to see what type the result is? Obviously this won't work for all expressions (eg. x / (x - 1)), but maybe it would work enough of the time, depending on what form your expressions take. Am I misunderstanding entirely, or only partially? Jonathan Lepisto, Stephen P wrote: What about treating the return type of the python expression as object then coerce the object into the type required by the database? If the coercion fails, then that could be treated as a type error. For example, after using python to evaluate the expression '2+(4*5)', the returned object could be converted to an integer using System.Convert.ToInt32() or to a string with System.Convert.ToString(). System.Convert throws InvalidCastException for those cases that cannot be converted. -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Michael Foord Sent: Monday, October 19, 2009 4:21 AM To: Discussion of IronPython Subject: Re: [IronPython] Type analysis of expression Christian Schmidt wrote: Hello, we are using IronPython embedded into our application to evaluate user defined expression. The variables used in the expressions are strongly typed and the results need to be written back into a database. I know that the return type of a dynamic expression cannot be determined in general. But what would be a pragmatic way to guess the return type of an expression? Hehe. If you're evaluating arbitrary functions provided by the user then I don't know of any way of 'inferring' the return type - beyond parsing it and modelling the type flow through the expression (which would be a lot of work). All the best, Michael Thanks, Christian ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com -- Jonathan Hartley Made of meat. http://tartley.com tart...@tartley.com +44 7737 062 225 twitter/skype: tartley ___ 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
Re: [IronPython] Type analysis of expression
Christian, Would it be possible to leave the columns as text then coerce at the time the data is read from the database? In other words, convert the result of the python expression to a string and store the string in the database. Then, when the result is needed, coerce the string to a more suitable type. In other words, defer when the type needs to be known until the value is used. -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Christian Schmidt Sent: Monday, October 19, 2009 12:12 PM To: Discussion of IronPython Subject: Re: [IronPython] Type analysis of expression Hi all, thanks for your discussion. Actually I'm having an in-memory table of strongly typed columns. The user can provide per-row (python-)expressions as additional columns. Now if the user wants his result to be exported to a database (e.g. SQL Server or Oracle) I need to set a type for each column - also for the expression columns. I thought there might be a way to figure out the return type in a similar way for example Boo (boo.codehaus.org) does at compile time. When an expression is parsed at runtime, the interpreter also needs to decide which .NET-functions to call. For strongly typed input these functions should normally have typed return values... Wouldn't this work somehow? If the Boo way is not possible then the only option is evaluating the expressions for some random rows and coerce to a common type. What would be the rules? int-float-string is trivial, but what about decimal, int64, double, ...? I assume python must have implemented these rules somewhere. How would one have to implement the general function: Type GetCoercedType(IEnumerableobject list) { ... } Thanks, Christian ___ 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
Re: [IronPython] IronPython in Action and Pro IronPython
IronPython in Action does a good job at explaining how to embed IronPython (an entire, lengthy chapter is devoted to it). It answered several questions I had about the process. It does not cover IronPython 2.6, but the approach is, I believe, very similar to 2.0. I have not read Pro IronPython. -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Slide Sent: Friday, July 10, 2009 2:57 PM To: Discussion of IronPython Subject: [IronPython] IronPython in Action and Pro IronPython I am assuming the authors of these two books are possibly on this mailing list, so I am not sure if this is really a good question to ask :-) , but can anyone comment on these two books, their accuracy to the latest IronPython versions and how they do at explaining embedding the runtime to make applications scriptable and such? http://www.amazon.com/IronPython-Action-Michael-Foord/dp/1933988339/ref=sr_1_1?ie=UTF8s=booksqid=1247262876sr=8-1 http://www.amazon.com/Pro-IronPython-Alan-Harris/dp/1430219629/ref=sr_1_2?ie=UTF8s=booksqid=1247262876sr=8-2 Thanks, slide -- slide-o-blog http://slide-o-blog.blogspot.com/ ___ 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
Re: [IronPython] IronPython question
You have to have a version of CPython installed such as v2.5 (for IronPython 2.0) or v2.6 (for IronPython 2.6). From IronPython: import sys sys.path.append(r'\c:\python25\lib') # adjust to whatever version of CPython you have installed. import urllib From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Marty Nelson Sent: Monday, June 29, 2009 1:55 PM To: Discussion of IronPython Subject: [IronPython] FW: IronPython question Using the Iron Python libraries for extensibility in a C# app, what do we need to do to make the Python module loadable? Quick question: I am trying to use a built-in Python module (not .NET) in a SN script, and I am not sure how to reference it. I tried: import urllib and I am getting: Symyx.Framework.Scripting.ScriptExecutionException: Exception executing script 'test.toolbarItem1'. No module named urllib --- IronPython.Runtime.Exceptions.ImportException: No module named urllib Is it possible to use Python built-in modules? === Notice: This e-mail message, together with any attachments, contains information of Symyx Technologies, Inc. or any of its affiliates or subsidiaries that may be confidential, proprietary, copyrighted, privileged and/or protected work product, and is meant solely for the intended recipient. If you are not the intended recipient, and have received this message in error, please contact the sender immediately, permanently delete the original and any copies of this email and any attachments thereto. ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Random Number Generation
If global.asax can set an environment variable, you can set (or append to) IRONPYTHONPATH to the path if interest. When IronPython starts up, it takes the paths in IRONPYTHONPATH and adds them to the sys.path list. From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Adam Brand Sent: Thursday, June 25, 2009 12:16 PM To: Discussion of IronPython Subject: Re: [IronPython] Random Number Generation Awesome, thanks that worked. I was using IronPython for ASP.net and did not have a pointer to the standard library. One last question though if anyone knows...is there a way to do this sys.path.append from Global.asax? I know there is a python equivalent global.py, but we right now have a bunch of code in global.asax and would need to rewrite that in python if not. Or can they co-exist? Thanks, Adam Adam Brand SilverKey Technologies From: users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com] On Behalf Of David DiCato Sent: Wednesday, June 24, 2009 9:31 PM To: Discussion of IronPython Subject: Re: [IronPython] Random Number Generation Seo is correct; in order to import random, you need the CPython standard library in sys.path. There are 3 ways to do this: 1. Run IronPython from the standard library directory (the working directory is in sys.path by default) 2. Append the standard lib directory to sys.path for invocation of IronPython, e.g.: import sys sys.path.append(r'c:\Program Files\IronPython 2.6\Lib') 3. (Recommended) Set the environment variable IRONPYTHONPATH to point to the standard lib directory System.Random is implemented in terms of .NET integers, which are 32-bit. When your script passed 99, IronPython tried to represent it in 32 bits, causing an arithmetic overflow. In a pinch, you can use slightly more complicated logic to suit your needs, e.g.: var_utmn = randgen.Next(1,10) * 10 + randgen.Next(9) But using the CPython library is much cleaner :). A final word of advice: Both standard libraries' random number generators use the convention that the first argument is inclusive and the second is exclusive. This means that your code will generate random numbers from 10 to 98, which may or may not be what you want. Good luck, - David From: users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com] On Behalf Of Adam Brand Sent: Wednesday, June 24, 2009 5:22 PM To: Discussion of IronPython Subject: [IronPython] Random Number Generation I feel newbish writing this, but I'm having problems generating random numbers in IronPython. I tried import random but that doesn't seem to work (module not found). I tried creating a System.Random but when I run var_utmn = randgen.Next(10,99) I get a buffer overflow. Any ideas? The random number needs to be above those two numbers indicated above. Thanks, Adam ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] clarification on current state of embedding
From MSDN (http://msdn.microsoft.com/en-us/library/bb383796.aspx): Differences Between .NET Framework Versions All three versions of the .NET Framework are based on version 2.0 of the CLR. The versions of the .NET Framework differ from each other in the list of assemblies that each makes available for you to reference in your projects. For example, LINQ is a new technology that is included in Visual Studio 2008. .NET Framework 3.5 is the only version of the .NET Framework that has LINQ-related assemblies. Therefore, you cannot use LINQ unless your project specifically targets .NET Framework 3.5. Similarly, Windows Presentation Foundation (WPF) is included in Windows Vista. You cannot build WPF applications unless your project targets .NET Framework 3.0 and later versions of the .NET Framework. In addition, .Net 3.0 changed the compiler to add support for lambdas in C#, although the resulting code can still run on .Net 2.0 runtime. The lambdas are converted to anonymous functions by the compiler. There may be other syntactic sugar changes as well. In Visual Studio, when you select the target runtime, you are primarily telling Visual Studio (and the underlying .Net compiler tools) to filter out elements from later versions of .Net. Of course, if you include a reference to a .Net 3.5 assembly, you have to make sure .Net 3.5 is installed on the target system. -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Michael Foord Sent: Thursday, June 18, 2009 4:49 AM To: Discussion of IronPython Subject: Re: [IronPython] clarification on current state of embedding Justin Regele wrote: This brings up another question I've encountered. My understanding was that IPy had problems with 3.5, and so I have been targeting 2.0. But when I try to reference the IronPython and Microsoft.Scripting assemblies, Visual Studio says I need 3.5 I've created many projects in Visual Studio referencing IronPython assemblies and targeting .NET 2.0. Which means I don't know what is wrong, but it *should* work fine... :-) Michael Foord On Wed, Jun 17, 2009 at 6:05 PM, Curt Hagenlocher c...@hagenlocher.org mailto:c...@hagenlocher.org wrote: Good point! On Wed, Jun 17, 2009 at 5:12 PM, Michael Foord fuzzy...@voidspace.org.uk mailto:fuzzy...@voidspace.org.uk wrote: Curt Hagenlocher wrote: ...except through the hosting API *and through the new C# dynamic functionality in .NET 4.0*. And how do you get to the classes to use them with the new dynamic functionality if it isn't through the hosting API? What you do with them once you get them is your own business of course... Michael On Wed, Jun 17, 2009 at 5:06 PM, Michael Foord fuzzy...@voidspace.org.uk mailto:fuzzy...@voidspace.org.uk mailto:fuzzy...@voidspace.org.uk mailto:fuzzy...@voidspace.org.uk wrote: Justin Regele wrote: What is the status of referencing IPy libraries compiled to dlls by other CLR languages? Google turned up that as of 1.1 you had to use the embedding/hosting api's, since the dlls were not compatible with say C# assemblies. There were allusions made to this being changed. You can't access Python classes except through the hosting API. There are no (public) plans for this to change anytime soon. Michael ___ Users mailing list Users@lists.ironpython.com mailto:Users@lists.ironpython.com mailto:Users@lists.ironpython.com mailto:Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com --http://www.ironpythoninaction.com/ http://www.voidspace.org.uk/blog ___ Users mailing list Users@lists.ironpython.com mailto:Users@lists.ironpython.com mailto:Users@lists.ironpython.com mailto:Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com mailto:Users@lists.ironpython.com
Re: [IronPython] Embedding IronPython and calling python functions from C#
What I do when I want to work with python modules from embedded IronPython is set the IronPython search path with the path of the module to load then create and execute a small python script that loads the main python modules into the current scope. string moduleName = Path.GetFileName(modulePath); string path = Path.GetDirectoryName(modulePath); ICollectionstring paths = _pythonEngine.GetSearchPaths(); if (!paths.Contains(path)) { paths.Add(path); _pythonEngine.SetSearchPaths(paths); } string modInvoke = String.Format(import {0}\nfrom {0} import *\n, moduleName); ScriptSource source = _pythonEngine.CreateScriptSourceFromString(modInvoke, Microsoft.Scripting.SourceCodeKind.Statements); Where modulePath is the full path to the python module or package to load. I can then invoke methods or access attributes using the IronPython scope. In this way, I can interact with the python modules for as long as necessary before closing down the scope/engine. From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Dody Gunawinata Sent: Thursday, June 11, 2009 9:09 AM To: Discussion of IronPython Subject: Re: [IronPython] Embedding IronPython and calling python functions from C# You can read all yours file scripts, then using StringBuilder to combine then and call CreateScriptSourceFromString() Then you can call your functions within the combined scripts normally. Pretty much you are creating a giant source code on the fly. On Thu, Jun 11, 2009 at 5:32 PM, Patrick van der Willik patr...@toolmaker.nlmailto:patr...@toolmaker.nl wrote: I'm currently attempting to embed the IronPython 2 runtimes into an existing application written in C#. However, I find the amount of documentation lacking on what I'm trying to do. I currently have a proof-of-concept version which uses Lua and LuaInterface, but the people who have to write the scripts dislike Lua(Well, more hate it with a passion) and would love to see this working with Python. My host application is a networked application that must trigger certain scripts functions on events generated by the connected clients. The idea is that when my application starts, it will load the IronPython script environment, launches an 'autoexec.py' which will load various other scripts files and do some housekeeping. Once this all is completed, it will start listening to incoming connections. However, in various scenarios, the application has to trigger scripted functions when data is received from a client. Which script function is called is different per client and per event. I have events for connecting, logging on, disconnecting and a set of data specific events after receiving data. This highly depends on the received packets. My question here is: How do I embed IronPython in such a fashion that I can load my scripts and then trigger various functions within that? I've seen many examples that just call CreateScriptSourceFromString() or File each time in which just 1 piece of code is implemented. This is not suitable for the needs here because the scripted systems can become quite complex. With regards, Patrick ___ Users mailing list Users@lists.ironpython.commailto:Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com -- nomadlife.orghttp://nomadlife.org ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] with_statement in IronPython 2.6 Alpha
Elise, PYTHONPATH is an environment variable and can be set on Windows by going to Control Panel -- System. Select the Advanced tab then click on the Environment Variables button. In the lower pane, click the New button and enter PYTHONPATH for the Variable name and enter the path or paths you want in the Variable value. Use semi-colons to separate multiple paths. Click OK, click OK, and click OK to exit. Note: Visual Studio needs to be closed then reopened for it to recognize the new environment variable (this is true for Visual Studio 2005 and earlier, I actually haven't tested Visual Studio 2008 to see if it properly responds to a change in environment variables). As for adding a reference to an assembly from IronPython, I found good success with using AddReference() with sys.path.append() instead of AddReferenceToFile(). For example, sys.path.append(rC:\Users\i-ellang\Documents\Infer.NET2.2\bin\Debug) clr.AddReference(Infer.Compiler) clr.AddReference(Infer.Runtime) From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Elise Langham (Elanit) Sent: Thursday, May 21, 2009 4:23 AM To: users@lists.ironpython.com Subject: Re: [IronPython] with_statement in IronPython 2.6 Alpha Hi Dave, Thanks for the reply- I'm running in the Visual Studio under debug. I don't know how to set pythonpath on a windows machine Most of the info is for C shell and linux. I find that the with statement works fine when just using the command line ipy.exe but it can't pick up The references to imported modules. The references work fine when running in Visual Studio though import System from System import Console from System import Array from System import IO import clr import sys sys.path.append(rC:\Users\i-ellang\Documents\Infer.NET2.2\bin\Debug) clr.AddReferenceToFile(Infer.Compiler.dll) clr.AddReferenceToFile(Infer.Runtime.dll) think this only works when the .dlls are in the IronPython ipy.exe directory though. 'C:\\Users\\i-ellang\\IronPython 2.6' Hope you can help, Elise ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Compiling IronPythonConsole.exe
Andrew, I've had no problems building IronPythonConsole.exe from the source drop for v2.0.1. What kind of build errors are you getting? From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Andrew Evans Sent: Wednesday, May 06, 2009 8:53 AM To: users@lists.ironpython.com Subject: [IronPython] Compiling IronPythonConsole.exe Hello list I am new here :-) I am trying to figure out how to get IronPythonConsole.exe as I need it for Pyreadline... I am using 2.01 version of IronPython I have tried compiling from source to but to no avail. Is IronPythonConsole obsolete? Thanks in Advance ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Compiling IronPythonConsole.exe
Andrew, based on your traceback output, I reconstructed the problem on my system and I get a similar error with IronPython 2.0.1 when trying to import readline. However, on my system, I get the following error: import sys sys.path.append(rc:\python25\Lib\site-packages) import readline Traceback (most recent call last): File stdin, line 1, in module File c:\python25\Lib\site-packages\readline\__init__.py, line 1, in c:\python25\Lib\site-packages\readline\__init__.py File c:\python25\Lib\site-packages\readline\PyReadline.py, line 12, in c:\python25\Lib\site-packages\readline\PyReadline.py ImportError: No module named win32con The win32con module is a .pyd module in python25. IronPython 2.0 cannot load .pyd modules. From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Andrew Evans Sent: Wednesday, May 06, 2009 11:30 AM To: Discussion of IronPython Subject: Re: [IronPython] Compiling IronPythonConsole.exe hmm same error No Module named IronPythonConsole I removed everything related to that but I still get that error in the same file I am confused... it doesn't seem like my system is cached I have rebooted now several times anyway here is the exact error I really appreciate your help so far :-) IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.3053 Type help, copyright, credits or license for more information. import readline Traceback (most recent call last): File C:\Python25\Lib\site-packages\pyreadline\console\__init__.py, line 8, i n C:\Python25\Lib\site-packages\pyreadline\console\__init__.py File C:\Python25\Lib\site-packages\pyreadline\console\ironpython_console.py, line 28, in C:\Python25\Lib\site-packages\pyreadline\console\ironpython_console .py File , line unknown, in module File Snippets.scripting, line unknown, in Initialize File Snippets.scripting, line unknown, in Initialize File Snippets.scripting, line unknown, in Initialize File Snippets.scripting, line unknown, in Initialize ImportError: No module named IronPythonConsole On Wed, May 6, 2009 at 10:54 AM, Dino Viehland di...@microsoft.commailto:di...@microsoft.com wrote: IConsole now lives in Microsoft.Scripting.Hosting.Shell, so you should be able to change this to: clr.AddReference('Microsoft.Scripting') ... from Microsoft.Scripting.Hosting.Shell import IConsole class IronPythonWrapper(IConsole): From: users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com] On Behalf Of Andrew Evans Sent: Wednesday, May 06, 2009 10:48 AM To: Discussion of IronPython Subject: Re: [IronPython] Compiling IronPythonConsole.exe Hi the ironpython_console.py comes from pyreadine in the folder pyreadline/console/ironpython_console.py the file itself in the areas where IronPythonConsole are referenced are import clr,sys clr.AddReferenceToFileAndPath(sys.executable) import IronPythonConsole class IronPythonWrapper(IronPythonConsole.IConsole): def ReadLine(self,autoIndentSize): return hook_wrap() def Write(self,text, style): System.Console.Write(text) def WriteLine(self,text, style): System.Console.WriteLine(text) IronPythonConsole.PythonCommandLine.MyConsole = IronPythonWrapper() I have not been able to find an updated pyreadline I have attached the file in question to see which module I am refering to see http://ipython.scipy.org/moin/PyReadline/Intro *cheers And thank you On Wed, May 6, 2009 at 10:39 AM, Dino Viehland di...@microsoft.commailto:di...@microsoft.com wrote: Where does ironpython_console.py come from? There is no IronPythonConsole class anymore so knowing what exactly the .py file is trying to do would be helpful. But likely you want to map existing things we're looking for into the Microsoft.Scripting.Hosting.Shell namespace which lives in Microsoft.Scripting.dll. From: users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com] On Behalf Of Andrew Evans Sent: Wednesday, May 06, 2009 10:30 AM To: users@lists.ironpython.commailto:users@lists.ironpython.com Subject: Re: [IronPython] Compiling IronPythonConsole.exe Hello maybe I should explain I have been trying to get readline to work with no success import readline fails with this message No Module Named IronPythonConsole the line in question is in the file ironpython_console.py import clr,sys clr.AddReferenceToFileAndPath(sys.executable) import IronPythonConsole Just thought at this moment do I need to add anything to my environment variables? it was mentioned on the readline web site to post here hope some one can help *cheers Andrew using readline in IronPython produces and an error no mo On Wed, May 6, 2009 at 10:19 AM, cur...@acm.orgmailto:cur...@acm.org
Re: [IronPython] How to get access to enum?
I believe you just need to import the enumeration: from module import Visibility where module is the module containing the definition of the enumeration. -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Kristian Jaksch Sent: Monday, May 04, 2009 7:25 AM To: users@lists.ironpython.com Subject: [IronPython] How to get access to enum? How can I get access to enums in IronPython? For example, when running IronPython + Silverlight I need access to the Visibility enum: btnMaxPlot.Visibility = Visibility.Collapsed This results in an error: NameError: name 'Visibility' is not defined Thanks for help! /Kristian ___ 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
[IronPython] Question on Multiple Discrete IronPython sessions in a single process
Hello, everyone! I am working on an service manager application that provides embedded python support through a small set of generalized classes: PythonService, PythonSession, and PythonClass. A client application asks the service manager for the PythonService object and then asks the PythonService object for a new PythonSession object. The PythonSession object is used to access embedded python through a small set of generalized methods. The PythonClass object is used to wrap a python class instance. The key requirement in this is each client must have its own python session, independent of any other sessions currently running. I've got this to work with CPython (specifically, python 2.5.4), by careful use of the global interpreter lock and swapping the thread state. IronPython 2.0.1 has a nicer way of implementing all of this by using the CreateEngine() to create a new python engine. However, in IronPython I've run into what appears to be a significant limitation that may prevent me from using IronPython in this particular situation as an embedded language. The limitation is when I import a python package from disk into IronPython (using IronPython.Hosting.Python.ImportModule()) in one session and then import the same package into a different session, it turns out that both sessions are pulling from the same module's scope. That is, if I make changes to the module's scope in one session (for example, changing a global variable), that change appears in the other session. After tracing execution in the IronPython 2.0.1 code, it turns out that a module is cached in the LanguageContext (PythonContext) object, which in turn is a singleton in DLR, as it is associated with the language type. This is okay if an application is embedding IronPython itself but in my scenario, this prevents multiple discrete python sessions in a single application. Ideally, I would expect to see the system state be stored in the python engine (ScriptEngine) or python runtime (ScriptRuntime) objects. Is there a way around this limitation? Can I somehow create a unique PythonContext object for each of my python sessions so I get a completely discrete python instance in each session with no crosstalk? Or do I have to resort to modifying the IronPython source to accomplish this (which I'm loathe to do since then I would have to maintain it going forward)? Thank you for your time and consideration in this matter. ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process
Dino, That example you provided produced the following output under IronPython 2.0.1: hello 42 In other words, the two sessions appear to be sharing the same module in IronPython 2.0.1. Are you using a later version of IronPython where this might be fixed? -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Dino Viehland Sent: Thursday, April 30, 2009 9:28 AM To: Discussion of IronPython Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process And this works for me: using System; using IronPython.Hosting; using Microsoft.Scripting.Hosting; class foo { static void Main(string[] args) { var engine = Python.CreateEngine(); ScriptScope scope1 = engine.ImportModule(foo); var engine2 = Python.CreateEngine(); ScriptScope scope2 = engine2.ImportModule(foo); scope1.SetVariable(foo, 42); object res; if(scope2.TryGetVariable(foo, out res)) { Console.WriteLine(res); } else { Console.WriteLine(no foo); } } } Foo.py: print 'hello' Printing out: hello hello no foo -Original Message- From: users-boun...@lists.ironpython.com [mailto:users- boun...@lists.ironpython.com] On Behalf Of Michael Foord Sent: Thursday, April 30, 2009 9:08 AM To: Discussion of IronPython Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process Dino Viehland wrote: You mention CreateEngine but are you also creating multiple runtimes? You're only allowed 1 ScriptEngine of a given type per ScriptRuntime. So you should create a new ScriptRuntime and then get the Python engine for each runtime and then be isolated. If you call Python.CreateEngine() twice it gives you two different engine objects with what *appear* to be different runtimes. If you then call Python.ImportModule for the same module but passing in the two different engines, you get two different (non-identical objects) ScriptScopes - but changes in one are reflected in the other. Is CreateEngine not the correct way to get isolated engines? Michael *From:* users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] *On Behalf Of *Lepisto, Stephen P *Sent:* Thursday, April 30, 2009 8:26 AM *To:* users@lists.ironpython.com *Subject:* [IronPython] Question on Multiple Discrete IronPython sessions in a single process Hello, everyone! I am working on an service manager application that provides embedded python support through a small set of generalized classes: PythonService, PythonSession, and PythonClass. A client application asks the service manager for the PythonService object and then asks the PythonService object for a new PythonSession object. The PythonSession object is used to access embedded python through a small set of generalized methods. The PythonClass object is used to wrap a python class instance. The key requirement in this is each client must have its own python session, independent of any other sessions currently running. I've got this to work with CPython (specifically, python 2.5.4), by careful use of the global interpreter lock and swapping the thread state. IronPython 2.0.1 has a nicer way of implementing all of this by using the CreateEngine() to create a new python engine. However, in IronPython I've run into what appears to be a significant limitation that may prevent me from using IronPython in this particular situation as an embedded language. The limitation is when I import a python package from disk into IronPython (using IronPython.Hosting.Python.ImportModule()) in one session and then import the same package into a different session, it turns out that both sessions are pulling from the same module's scope. That is, if I make changes to the module's scope in one session (for example, changing a global variable), that change appears in the other session. After tracing execution in the IronPython 2.0.1 code, it turns out that a module is cached in the LanguageContext (PythonContext) object, which in turn is a singleton in DLR, as it is associated with the language type. This is okay if an application is embedding IronPython itself but in my scenario, this prevents multiple discrete python sessions in a single application. Ideally, I would expect to see the system state be stored in the python engine (ScriptEngine) or python runtime (ScriptRuntime) objects. Is there a way around this limitation? Can I somehow create a unique PythonContext object for each of my python sessions so I get a completely discrete python instance in each session with no crosstalk? Or do I have to resort to modifying the IronPython source to accomplish this (which
Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process
I voted for fixing the bug. In the meantime, I will be putting on hold the use of IronPython as an embedded language in my project until this is fixed or IronPython 2.6 is released (if I can convince my manager and team it's a good idea to move to python 2.6 but it will affect a lot of people). Thank you for the prompt responses! -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Dino Viehland Sent: Thursday, April 30, 2009 10:23 AM To: Discussion of IronPython Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process Looks like our threads crossed. Yep, I was using the current 2.6 branch. I opened a bug to fix this in 2.0.2 (http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=22239). Thanks for reporting this - this is a very bad bug. -Original Message- From: users-boun...@lists.ironpython.com [mailto:users- boun...@lists.ironpython.com] On Behalf Of Lepisto, Stephen P Sent: Thursday, April 30, 2009 10:10 AM To: Discussion of IronPython Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process Dino, That example you provided produced the following output under IronPython 2.0.1: hello 42 In other words, the two sessions appear to be sharing the same module in IronPython 2.0.1. Are you using a later version of IronPython where this might be fixed? -Original Message- From: users-boun...@lists.ironpython.com [mailto:users- boun...@lists.ironpython.com] On Behalf Of Dino Viehland Sent: Thursday, April 30, 2009 9:28 AM To: Discussion of IronPython Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process And this works for me: using System; using IronPython.Hosting; using Microsoft.Scripting.Hosting; class foo { static void Main(string[] args) { var engine = Python.CreateEngine(); ScriptScope scope1 = engine.ImportModule(foo); var engine2 = Python.CreateEngine(); ScriptScope scope2 = engine2.ImportModule(foo); scope1.SetVariable(foo, 42); object res; if(scope2.TryGetVariable(foo, out res)) { Console.WriteLine(res); } else { Console.WriteLine(no foo); } } } Foo.py: print 'hello' Printing out: hello hello no foo -Original Message- From: users-boun...@lists.ironpython.com [mailto:users- boun...@lists.ironpython.com] On Behalf Of Michael Foord Sent: Thursday, April 30, 2009 9:08 AM To: Discussion of IronPython Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process Dino Viehland wrote: You mention CreateEngine but are you also creating multiple runtimes? You're only allowed 1 ScriptEngine of a given type per ScriptRuntime. So you should create a new ScriptRuntime and then get the Python engine for each runtime and then be isolated. If you call Python.CreateEngine() twice it gives you two different engine objects with what *appear* to be different runtimes. If you then call Python.ImportModule for the same module but passing in the two different engines, you get two different (non-identical objects) ScriptScopes - but changes in one are reflected in the other. Is CreateEngine not the correct way to get isolated engines? Michael *From:* users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] *On Behalf Of *Lepisto, Stephen P *Sent:* Thursday, April 30, 2009 8:26 AM *To:* users@lists.ironpython.com *Subject:* [IronPython] Question on Multiple Discrete IronPython sessions in a single process Hello, everyone! I am working on an service manager application that provides embedded python support through a small set of generalized classes: PythonService, PythonSession, and PythonClass. A client application asks the service manager for the PythonService object and then asks the PythonService object for a new PythonSession object. The PythonSession object is used to access embedded python through a small set of generalized methods. The PythonClass object is used to wrap a python class instance. The key requirement in this is each client must have its own python session, independent of any other sessions currently running. I've got this to work with CPython (specifically, python 2.5.4), by careful use of the global interpreter lock and swapping the thread state. IronPython 2.0.1 has a nicer way of implementing all of this by using the CreateEngine() to create a new python engine. However, in IronPython I've run into what appears to be a significant limitation that may prevent me from using IronPython in this particular situation
Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process
Curt, thanks for the tip. In my tests, I needed to use from modname import * since I needed the attributes in the module to be in the current scope. From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Curt Hagenlocher Sent: Thursday, April 30, 2009 10:31 AM To: Discussion of IronPython Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process If you import by executing the text import modname against the ScriptEngine instead of using the import API, you will avoid this particular incarnation of the bug. On Thu, Apr 30, 2009 at 10:25 AM, Lepisto, Stephen P stephen.p.lepi...@intel.commailto:stephen.p.lepi...@intel.com wrote: I voted for fixing the bug. In the meantime, I will be putting on hold the use of IronPython as an embedded language in my project until this is fixed or IronPython 2.6 is released (if I can convince my manager and team it's a good idea to move to python 2.6 but it will affect a lot of people). Thank you for the prompt responses! -Original Message- From: users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com] On Behalf Of Dino Viehland Sent: Thursday, April 30, 2009 10:23 AM To: Discussion of IronPython Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process Looks like our threads crossed. Yep, I was using the current 2.6 branch. I opened a bug to fix this in 2.0.2 (http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=22239). Thanks for reporting this - this is a very bad bug. -Original Message- From: users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com [mailto:users-mailto:users- boun...@lists.ironpython.commailto:boun...@lists.ironpython.com] On Behalf Of Lepisto, Stephen P Sent: Thursday, April 30, 2009 10:10 AM To: Discussion of IronPython Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process Dino, That example you provided produced the following output under IronPython 2.0.1: hello 42 In other words, the two sessions appear to be sharing the same module in IronPython 2.0.1. Are you using a later version of IronPython where this might be fixed? -Original Message- From: users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com [mailto:users-mailto:users- boun...@lists.ironpython.commailto:boun...@lists.ironpython.com] On Behalf Of Dino Viehland Sent: Thursday, April 30, 2009 9:28 AM To: Discussion of IronPython Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process And this works for me: using System; using IronPython.Hosting; using Microsoft.Scripting.Hosting; class foo { static void Main(string[] args) { var engine = Python.CreateEngine(); ScriptScope scope1 = engine.ImportModule(foo); var engine2 = Python.CreateEngine(); ScriptScope scope2 = engine2.ImportModule(foo); scope1.SetVariable(foo, 42); object res; if(scope2.TryGetVariable(foo, out res)) { Console.WriteLine(res); } else { Console.WriteLine(no foo); } } } Foo.py: print 'hello' Printing out: hello hello no foo -Original Message- From: users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com [mailto:users-mailto:users- boun...@lists.ironpython.commailto:boun...@lists.ironpython.com] On Behalf Of Michael Foord Sent: Thursday, April 30, 2009 9:08 AM To: Discussion of IronPython Subject: Re: [IronPython] Question on Multiple Discrete IronPython sessions in a single process Dino Viehland wrote: You mention CreateEngine but are you also creating multiple runtimes? You're only allowed 1 ScriptEngine of a given type per ScriptRuntime. So you should create a new ScriptRuntime and then get the Python engine for each runtime and then be isolated. If you call Python.CreateEngine() twice it gives you two different engine objects with what *appear* to be different runtimes. If you then call Python.ImportModule for the same module but passing in the two different engines, you get two different (non-identical objects) ScriptScopes - but changes in one are reflected in the other. Is CreateEngine not the correct way to get isolated engines? Michael *From:* users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.commailto:users-boun...@lists.ironpython.com] *On Behalf Of *Lepisto, Stephen P *Sent:* Thursday, April 30, 2009 8:26 AM *To:* users@lists.ironpython.commailto:users@lists.ironpython.com *Subject