[IronPython] InteractiveCode and function definitions
Curt Hagenlocher
curt at hagenlocher.org
Thu Apr 16 20:00:23 CEST 2009
What I've done is to test explicitly for the blank line at the end in
conjuction with the test for ScriptCodeParseResult.IncompleteStatement:
completeThought = (result == ScriptCodeParseResult.Complete or
result == ScriptCodeParseResult.Invalid or
(result == ScriptCodeParseResult.IncompleteStatement and
text.endswith('\n\n')))
On Thu, Apr 16, 2009 at 10:54 AM, Michael Foord
<fuzzyman at voidspace.org.uk>wrote:
> Curt Hagenlocher wrote:
>
>> 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.
>>
> 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.
>
> 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".
>> My point is that it's not correct for IronPython to dictate the semantics
>> of your interpreter.
>>
>
> 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?
>
> Michael
>
> [*] Okay, "arbitrary" is a bit strong in that it's what python.exe and
>> ipy.exe defines. :)
>>
>> On Thu, Apr 16, 2009 at 10:36 AM, Michael Foord <
>> fuzzyman at voidspace.org.uk <mailto:fuzzyman at voidspace.org.uk>> wrote:
>>
>> Curt Hagenlocher wrote:
>>
>> "IncompleteStatement" means that the user is allowed to type
>> more code. If you want to know whether or not it's a valid
>> (complete) string, just check for it not being Invalid. A
>> function definition is never "complete" in Python because
>> there's never a terminating curly brace :).
>>
>>
>> But that isn't sufficient to implement an interactive interpreter
>> on top of. This code conceptually is complete as far as an
>> interactive interpreter is concerned:
>>
>>
>> 'def f():\n print 1\n\n'
>>
>> It also means you can't distinguish between the previous kind of
>> incomplete (which is incomplete because the user *could* type more
>> code) and this kind of incomplete:
>>
>> 'a = """'
>>
>> or:
>>
>> 'a = (1 + 2 +'
>>
>> Which are both incomplete because the user *must* type more code.
>> (Although the latter two give IncompleteToken - I wonder if that
>> would be enough.)
>>
>> Because of the other IronPython bugs we can't use the code module
>> and ScriptSource / ScriptParseResult doesn't give sufficient
>> information. Any other ideas?
>>
>> Michael
>>
>>
>> On Thu, Apr 16, 2009 at 10:05 AM, Michael Foord
>> <fuzzyman at voidspace.org.uk <mailto:fuzzyman at voidspace.org.uk>
>> <mailto:fuzzyman at voidspace.org.uk
>> <mailto:fuzzyman at voidspace.org.uk>>> wrote:
>>
>> Hello guys,
>>
>> We're trying to detect whether a section of code is
>> complete (to
>> mimic the behaviour of the interactive interpreter).
>>
>> First of all we tried using the Python standard library code
>> module which provides interactive console classes. There
>> are two
>> outstanding bugs on codeplex (one reported by me today) which
>> prevent this being an ideal solution:
>>
>>
>> http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=22064
>>
>> http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21881
>>
>> The second approach was to create a ScriptSource and looking at
>> the code properties to tell if the statement is complete or not
>> (using IronPython 2.0.1). However we can never get it to
>> return a
>> ScriptParseResult.Complete for function definitions. Code below
>> shows using \n for newlines but we have also tried with \r\n.
>>
>> >>> import clr
>> >>> clr.AddReference('IronPython')
>> >>> clr.AddReference('Microsoft.Scripting')
>> >>> from IronPython.Hosting import Python
>> >>> from Microsoft.Scripting import SourceCodeKind,
>> ScriptCodeParseResult
>> >>>
>> >>> engine = Python.CreateEngine()
>> >>> s = engine.CreateScriptSourceFromString('def f():\n print
>> 1\n', 'foo', SourceCodeKind.InteractiveCode)
>> >>> s.GetCodeProperties()
>> <Microsoft.Scripting.ScriptCodeParseResult object at
>> 0x000000000000003F [IncompleteStatement]>
>> >>> s = engine.CreateScriptSourceFromString('def f():\n print
>> 1\n\n', 'foo', SourceCodeKind.InteractiveCode)
>> >>> s.GetCodeProperties()
>> <Microsoft.Scripting.ScriptCodeParseResult object at
>> 0x0000000000000040 [IncompleteStatement]>
>> >>>
>>
>> The DLR hosting spec has little helpful to say on the matter as
>> far as I can tell.
>>
>> Looking at an example from Tomas it doesn't seem very different
>> from what we're doing:
>>
>> http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/
>>
>> Any clues as to what we are doing wrong or how to procede?
>>
>> Thanks
>>
>> Michael
>>
>> -- http://www.ironpythoninaction.com/
>> http://www.voidspace.org.uk/blog
>>
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> <mailto:Users at lists.ironpython.com>
>> <mailto:Users at lists.ironpython.com
>> <mailto:Users at lists.ironpython.com>>
>>
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>>
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>>
>> -- http://www.ironpythoninaction.com/
>> http://www.voidspace.org.uk/blog
>>
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>
>
> --
> http://www.ironpythoninaction.com/
> http://www.voidspace.org.uk/blog
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090416/cf5ae4ce/attachment.html>
More information about the Ironpython-users
mailing list