<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
On 15/02/2010 21:54, Mark Grice wrote:
<blockquote
 cite="mid:294f1ba41002151354k42155624med8ba3d713767a3b@mail.gmail.com"
 type="cite">I am using IronPython within a C# .NET application.&nbsp;
  <div><br>
  </div>
  <div>I have a class that the Python code uses, so I create a scope
and set the variable, then execute the engine like this:</div>
  <div><br>
  </div>
  <div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;private ScriptEngine scptEngine = null;</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;private ScriptRuntime scptRuntime = null;</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;private ScriptScope scptScope;</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;private ScriptSource scptSource;</div>
  <div><br>
  </div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;public pyEngine()</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scptEngine = Python.CreateEngine();</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scptRuntime = scptEngine.Runtime;</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scptScope = scptEngine.CreateScope();</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AddAssemblies(scptRuntime);&nbsp;</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; SOA SOA_screen = new SOA(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //
my Class</div>
  <div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scptScope.SetVariable("screen", SOA_screen); &nbsp;&nbsp;</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scptSource =
scptEngine.CreateScriptSourceFromFile(scriptFile);</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scptSource.Execute(scptScope);</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }</div>
  <div><br>
  </div>
  <div>This runs fine. But within the Python code, if I detect an
error, I want to exit out of scriptFile.</div>
  <div><br>
  </div>
  <div>I check a flag and try to exit if flag is false: so, the basic
python is:</div>
  <div><br>
  </div>
  <div>
  <div>&nbsp;&nbsp; if MessageScreenIsActive == False : &nbsp;</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp;screen.Say("CAN NOT PROCEED: " +
screen.lastResultMessage); &nbsp; &nbsp;</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp;sys.exit()</div>
  <div><br>
  </div>
  <div>This throws an exception. </div>
  </div>
  </div>
  </div>
</blockquote>
That will throw a SystemExit exception. You can have exception handling
in your C# that catches this specific error.<br>
<br>
<blockquote
 cite="mid:294f1ba41002151354k42155624med8ba3d713767a3b@mail.gmail.com"
 type="cite">
  <div>
  <div>
  <div>
  <div>But this:</div>
  <div><br>
  </div>
  <div>
  <div>&nbsp;&nbsp; if MessageScreenIsActive == False : &nbsp;</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp;screen.Say("CAN NOT PROCEED: " +
screen.lastResultMessage); &nbsp; &nbsp;</div>
  <div>&nbsp;&nbsp; &nbsp; &nbsp;sys.exit</div>
  </div>
  </div>
  </div>
  </div>
</blockquote>
You're not actually calling sys.exit here...<br>
<br>
<br>
All the best,<br>
<br>
Michael<br>
<blockquote
 cite="mid:294f1ba41002151354k42155624med8ba3d713767a3b@mail.gmail.com"
 type="cite">
  <div>
  <div>
  <div>
  <div>
  <div><br>
  </div>
  <div>only exits the &lt;IF block&gt; -- not the whole script.</div>
  <div><br>
  </div>
  <div>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?</div>
  <div><br>
  </div>
  <div>Thanks for reading!</div>
  <div><br>
  </div>
  <div><br>
  </div>
  </div>
  </div>
  </div>
  </div>
  <pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
Users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a>
<a class="moz-txt-link-freetext" href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a>
  </pre>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">-- 
<a class="moz-txt-link-freetext" href="http://www.ironpythoninaction.com/">http://www.ironpythoninaction.com/</a>
<a class="moz-txt-link-freetext" href="http://www.voidspace.org.uk/blog">http://www.voidspace.org.uk/blog</a>

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 (&#8221;BOGUS AGREEMENTS&#8221;) 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.

</pre>
</body>
</html>