<div>What I&#39;ve done is to test explicitly for the blank line at the end in conjuction with the test for ScriptCodeParseResult.IncompleteStatement:</div>
<div> </div>
<div>completeThought = (result == ScriptCodeParseResult.Complete or</div>
<div>    result == ScriptCodeParseResult.Invalid or<br>    (result == ScriptCodeParseResult.IncompleteStatement and text.endswith(&#39;\n\n&#39;)))<br><br></div>
<div class="gmail_quote">On Thu, Apr 16, 2009 at 10:54 AM, Michael Foord <span dir="ltr">&lt;<a href="mailto:fuzzyman@voidspace.org.uk">fuzzyman@voidspace.org.uk</a>&gt;</span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div class="im">Curt Hagenlocher wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">That&#39;s only because you&#39;ve decided to arbitrarily[*] define &quot;\n\n&quot; as being a signal to mean &quot;complete&quot;.  That&#39;s not part of the actual language specification.<br>
</blockquote></div>It&#39;s the behaviour of the interactive interpreter though - which specifies something. It&#39;s also the specification adhered to by the code module for implementing interactive interpreters. 
<div class="im"><br><br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">In fact, if I append &quot;\n\n  print 2\n\n&quot; to that string, it&#39;s still a valid Python program.  The key here is that &quot;the user has entered a complete thought&quot; is a property of the interpreter and not of the language.  I might well decide that the &quot;commit&quot; key sequence is Control+E (as it is in SQL Server Management Studio) instead of &quot;enter enter&quot;.<br>
 My point is that it&#39;s not correct for IronPython to dictate the semantics of your interpreter.<br></blockquote><br></div>Fine, so do you have any suggestions as to how to replicate the behaviour of the interactive interpreter - whether or not it counts as a specification?<br>
<font color="#888888"><br>Michael<br><br></font>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div class="im"> [*] Okay, &quot;arbitrary&quot; is a bit strong in that it&#39;s what python.exe and ipy.exe defines. :)<br><br></div>
<div>
<div></div>
<div class="h5">On Thu, Apr 16, 2009 at 10:36 AM, Michael Foord &lt;<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a> &lt;mailto:<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a>&gt;&gt; wrote:<br>
<br>   Curt Hagenlocher wrote:<br><br>       &quot;IncompleteStatement&quot; means that the user is allowed to type<br>       more code.  If you want to know whether or not it&#39;s a valid<br>       (complete) string, just check for it not being Invalid.  A<br>
       function definition is never &quot;complete&quot; in Python because<br>       there&#39;s never a terminating curly brace :).<br><br><br>   But that isn&#39;t sufficient to implement an interactive interpreter<br>   on top of. This code conceptually is complete as far as an<br>
   interactive interpreter is concerned:<br><br><br>     &#39;def f():\n  print 1\n\n&#39;<br><br>   It also means you can&#39;t distinguish between the previous kind of<br>   incomplete (which is incomplete because the user *could* type more<br>
   code) and this kind of incomplete:<br><br>     &#39;a = &quot;&quot;&quot;&#39;<br><br>   or:<br><br>     &#39;a = (1 + 2 +&#39;<br><br>   Which are both incomplete because the user *must* type more code.<br>   (Although the latter two give IncompleteToken - I wonder if that<br>
   would be enough.)<br><br>   Because of the other IronPython bugs we can&#39;t use the code module<br>   and ScriptSource / ScriptParseResult doesn&#39;t give sufficient<br>   information. Any other ideas?<br><br>   Michael<br>
<br><br>       On Thu, Apr 16, 2009 at 10:05 AM, Michael Foord<br>       &lt;<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a> &lt;mailto:<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a>&gt;<br>
       &lt;mailto:<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a><br>       &lt;mailto:<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a>&gt;&gt;&gt; wrote:<br>
<br>          Hello guys,<br><br>          We&#39;re trying to detect whether a section of code is<br>       complete (to<br>          mimic the behaviour of the interactive interpreter).<br><br>          First of all we tried using the Python standard library code<br>
          module which provides interactive console classes. There<br>       are two<br>          outstanding bugs on codeplex (one reported by me today) which<br>          prevent this being an ideal solution:<br><br>                <a href="http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=22064" target="_blank">http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=22064</a><br>
                <a href="http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21881" target="_blank">http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21881</a><br><br>          The second approach was to create a ScriptSource and looking at<br>
          the code properties to tell if the statement is complete or not<br>          (using IronPython 2.0.1). However we can never get it to<br>       return a<br>          ScriptParseResult.Complete for function definitions. Code below<br>
          shows using \n for newlines but we have also tried with \r\n.<br><br>          &gt;&gt;&gt; import clr<br>          &gt;&gt;&gt; clr.AddReference(&#39;IronPython&#39;)<br>          &gt;&gt;&gt; clr.AddReference(&#39;Microsoft.Scripting&#39;)<br>
          &gt;&gt;&gt; from IronPython.Hosting import Python<br>          &gt;&gt;&gt; from Microsoft.Scripting import SourceCodeKind,<br>          ScriptCodeParseResult<br>          &gt;&gt;&gt;<br>          &gt;&gt;&gt; engine = Python.CreateEngine()<br>
          &gt;&gt;&gt; s = engine.CreateScriptSourceFromString(&#39;def f():\n  print<br>          1\n&#39;, &#39;foo&#39;, SourceCodeKind.InteractiveCode)<br>          &gt;&gt;&gt; s.GetCodeProperties()<br>          &lt;Microsoft.Scripting.ScriptCodeParseResult object at<br>
          0x000000000000003F [IncompleteStatement]&gt;<br>          &gt;&gt;&gt; s = engine.CreateScriptSourceFromString(&#39;def f():\n  print<br>          1\n\n&#39;, &#39;foo&#39;, SourceCodeKind.InteractiveCode)<br>          &gt;&gt;&gt; s.GetCodeProperties()<br>
          &lt;Microsoft.Scripting.ScriptCodeParseResult object at<br>          0x0000000000000040 [IncompleteStatement]&gt;<br>          &gt;&gt;&gt;<br><br>          The DLR hosting spec has little helpful to say on the matter as<br>
          far as I can tell.<br><br>          Looking at an example from Tomas it doesn&#39;t seem very different<br>          from what we&#39;re doing:<br><br>          <a href="http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/" target="_blank">http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/</a><br>
<br>          Any clues as to what we are doing wrong or how to procede?<br><br>          Thanks<br><br>          Michael<br><br>          --    <a href="http://www.ironpythoninaction.com/" target="_blank">http://www.ironpythoninaction.com/</a><br>
          <a href="http://www.voidspace.org.uk/blog" target="_blank">http://www.voidspace.org.uk/blog</a><br><br><br>          _______________________________________________<br>          Users mailing list<br>          <a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br>
       &lt;mailto:<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a>&gt;<br>       &lt;mailto:<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br>
       &lt;mailto:<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a>&gt;&gt;<br><br>          <a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
<br><br>       ------------------------------------------------------------------------<br><br><br><br>       _______________________________________________<br>       Users mailing list<br>       <a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a> &lt;mailto:<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a>&gt;<br>
       <a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>        <br><br><br>   --    <a href="http://www.ironpythoninaction.com/" target="_blank">http://www.ironpythoninaction.com/</a><br>
   <a href="http://www.voidspace.org.uk/blog" target="_blank">http://www.voidspace.org.uk/blog</a><br><br><br>   _______________________________________________<br>   Users mailing list<br>   <a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a> &lt;mailto:<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a>&gt;<br>
   <a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br><br><br>------------------------------------------------------------------------<br>
<br>_______________________________________________<br>Users mailing list<br><a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br><a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
 <br></div></div></blockquote>
<div>
<div></div>
<div class="h5"><br><br>-- <br><a href="http://www.ironpythoninaction.com/" target="_blank">http://www.ironpythoninaction.com/</a><br><a href="http://www.voidspace.org.uk/blog" target="_blank">http://www.voidspace.org.uk/blog</a><br>
<br><br>_______________________________________________<br>Users mailing list<br><a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br><a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
</div></div></blockquote></div><br>