I think this may work pretty well. Something worth thinking through would be to what degree can user make your life complicated by catching exceptions. Once you throw your exception, the user can actually catch it (or you can modify codegen to filter your special exceptions out so user cannot catch them), but at the same time, to do that, user has to execute a statement (or in this case the except clause of the try statement) which would end up throwing the exception again as the time quantum for the script expired. The only danger I can see is if there is an exception handler on the stack which you don't control (such as a call into .NET) User can catch anything there and never give you control back. I can only think of explicit monitor thread and forced termination of the runaway thread as a protection against such misbehavior...
Martin From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Markus Hajek Sent: Monday, April 16, 2007 11:55 PM To: 'Discussion of IronPython' Subject: Re: [IronPython] Callback per statement One more question: I want to use this callback to abort python execution in certain cases. I'm currently thinking the best way to go about this would be to throw an exception in the callback I hook up. About like: public static class PythonCallback { public static void Callback() { If (pythonAbortionDesired) Throw new PythonAbortionException(); } } //... try { engine.ExecuteFile("SomePythonFile.py"); } catch (PythonAbortionException) { // do whatever I wanna do when Python execution has aborted } What do you think of this? Is there any better alternative? Many thanks again, Markus Hajek Team Vienna - Kazemi, Hajek & Pisarik OG Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im Auftrag von Markus Hajek Gesendet: Dienstag, 17. April 2007 08:38 An: 'Discussion of IronPython' Betreff: Re: [IronPython] Callback per statement Thanks a million, I'll start experimenting with that right away. Looks just exactly like what I need, thanks again. Cheers, Markus Hajek Team Vienna - Kazemi, Hajek & Pisarik OG Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im Auftrag von Martin Maly Gesendet: Montag, 16. April 2007 20:13 An: Discussion of IronPython Betreff: Re: [IronPython] Callback per statement It is not possible to do this without change to code generation at this point. Essentially you could do something like this: public class MyCallbackClass { public void MyCallback() { // } } And then emit call to this utility wherever you like: cg.EmitCall(typeof(MyCallbackClass).GetMethod("MyCallback")) You can then control the granularity at which you call this. Per statement, or even per expression (I am a bit confused about your mention of operators below, whether you want to do this for each expression, but it is certainly possible as well). For starters, what you could easily do to get feel for things would be to modify SuiteStatement's Emit method (which only loops through enclosed statements and emits each of them) and add the above "cg.EmitCall" for each statement, run your repro, say: X = 1 X = 2 X = 3 And put breakpoint in the "MyCallback", or better yet, run the repro: Ipy.exe -X:SaveAssemblies x.py And examine the x.exe we'll generate with ildasm or reflector. Then you can add calls to your callbacks wherever desired. Hope this helps Martin From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Markus Hajek Sent: Monday, April 16, 2007 3:37 AM To: 'Discussion of IronPython' Subject: [IronPython] Callback per statement Hi, is there a way to execute a callback whenever a Python statement is about to be executed (or just has executed)? With Python statements, I mean method calls, operators, assignments. And if there's no such way, how would I have to go about changing code generation to facilitate that? Many thanks, Markus Hajek Team Vienna - Kazemi, Hajek & Pisarik OG
_______________________________________________ users mailing list [email protected] http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
