[IronPython] InteractiveCode and function definitions

Michael Foord fuzzyman at voidspace.org.uk
Thu Apr 16 19:05:58 CEST 2009


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





More information about the Ironpython-users mailing list