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


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 
 >>> s = engine.CreateScriptSourceFromString('def f():\n  print 1\n\n', 
'foo', SourceCodeKind.InteractiveCode)
 >>> s.GetCodeProperties()
<Microsoft.Scripting.ScriptCodeParseResult object at 0x0000000000000040 

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:


Any clues as to what we are doing wrong or how to procede?




More information about the Ironpython-users mailing list