"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 :).
On Thu, Apr 16, 2009 at 10:05 AM, Michael Foord <[email protected]>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 > [email protected] > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com >
_______________________________________________ Users mailing list [email protected] http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
