[IronPython] InteractiveCode and function definitions

Michael Foord fuzzyman at voidspace.org.uk
Thu Apr 16 10:36:19 PDT 2009


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>> 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>
>     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




More information about the Users mailing list