<div>What I'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('\n\n')))<br><br></div>
<div class="gmail_quote">On Thu, Apr 16, 2009 at 10:54 AM, Michael Foord <span dir="ltr"><<a href="mailto:fuzzyman@voidspace.org.uk">fuzzyman@voidspace.org.uk</a>></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's only because you've decided to arbitrarily[*] define "\n\n" as being a signal to mean "complete". That's not part of the actual language specification.<br>
</blockquote></div>It's the behaviour of the interactive interpreter though - which specifies something. It'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 "\n\n print 2\n\n" to that string, it's still a valid Python program. The key here is that "the user has entered a complete thought" is a property of the interpreter and not of the language. I might well decide that the "commit" key sequence is Control+E (as it is in SQL Server Management Studio) instead of "enter enter".<br>
My point is that it'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, "arbitrary" is a bit strong in that it'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 <<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a> <mailto:<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a>>> wrote:<br>
<br> Curt Hagenlocher wrote:<br><br> "IncompleteStatement" means that the user is allowed to type<br> more code. If you want to know whether or not it's a valid<br> (complete) string, just check for it not being Invalid. A<br>
function definition is never "complete" in Python because<br> there's never a terminating curly brace :).<br><br><br> But that isn'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> 'def f():\n print 1\n\n'<br><br> It also means you can'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> 'a = """'<br><br> or:<br><br> 'a = (1 + 2 +'<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't use the code module<br> and ScriptSource / ScriptParseResult doesn'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> <<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a> <mailto:<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a>><br>
<mailto:<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a><br> <mailto:<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a>>>> wrote:<br>
<br> Hello guys,<br><br> We'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> >>> import clr<br> >>> clr.AddReference('IronPython')<br> >>> clr.AddReference('Microsoft.Scripting')<br>
>>> from IronPython.Hosting import Python<br> >>> from Microsoft.Scripting import SourceCodeKind,<br> ScriptCodeParseResult<br> >>><br> >>> engine = Python.CreateEngine()<br>
>>> s = engine.CreateScriptSourceFromString('def f():\n print<br> 1\n', 'foo', SourceCodeKind.InteractiveCode)<br> >>> s.GetCodeProperties()<br> <Microsoft.Scripting.ScriptCodeParseResult object at<br>
0x000000000000003F [IncompleteStatement]><br> >>> s = engine.CreateScriptSourceFromString('def f():\n print<br> 1\n\n', 'foo', SourceCodeKind.InteractiveCode)<br> >>> s.GetCodeProperties()<br>
<Microsoft.Scripting.ScriptCodeParseResult object at<br> 0x0000000000000040 [IncompleteStatement]><br> >>><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't seem very different<br> from what we'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>
<mailto:<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a>><br> <mailto:<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br>
<mailto:<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a>>><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> <mailto:<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><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> <mailto:<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><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>