[IronPython] How can I exit from Python Script File?

Michael Foord fuzzyman at voidspace.org.uk
Mon Feb 15 22:58:26 CET 2010

On 15/02/2010 21:54, Mark Grice wrote:
> I am using IronPython within a C# .NET application.
> I have a class that the Python code uses, so I create a scope and set 
> the variable, then execute the engine like this:
>         private ScriptEngine scptEngine = null;
>         private ScriptRuntime scptRuntime = null;
>         private ScriptScope scptScope;
>         private ScriptSource scptSource;
>         public pyEngine()
>         {
>                 scptEngine = Python.CreateEngine();
>                 scptRuntime = scptEngine.Runtime;
>                 scptScope = scptEngine.CreateScope();
>                 AddAssemblies(scptRuntime);
>                 SOA SOA_screen = new SOA();                 // my Class
>                 scptScope.SetVariable("screen", SOA_screen);
>                 scptSource = 
> scptEngine.CreateScriptSourceFromFile(scriptFile);
>                 scptSource.Execute(scptScope);
>          }
> This runs fine. But within the Python code, if I detect an error, I 
> want to exit out of scriptFile.
> I check a flag and try to exit if flag is false: so, the basic python is:
>    if MessageScreenIsActive == False :
>       screen.Say("CAN NOT PROCEED: " + screen.lastResultMessage);
>       sys.exit()
> This throws an exception.
That will throw a SystemExit exception. You can have exception handling 
in your C# that catches this specific error.

> But this:
>    if MessageScreenIsActive == False :
>       screen.Say("CAN NOT PROCEED: " + screen.lastResultMessage);
>       sys.exit
You're not actually calling sys.exit here...

All the best,

> only exits the <IF block> -- not the whole script.
> Of course, I can bracket off all of my code with if/else and control 
> it this way, but there must be a simple way to simple exit the 
> script... isn't there?
> Thanks for reading!
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies ("BOGUS AGREEMENTS") that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100215/c5a9d51e/attachment.html>

More information about the Ironpython-users mailing list