[IronPython] SourceCodeKind.Statements versus SourceCodeKind.InteractiveCode

Jeff Slutter jslutter at reactorzero.com
Sun Dec 7 00:33:09 CET 2008

I'm working on getting an interactive script console into my
application. Information on how to do this is a bit hard to find but I
came across an app.py by Jim Hugunin showing how to do it with Silverlight.

I based my C# app around that and have something working pretty well.
I'm always using SourceCodeKind.InteractiveCode when compiling the
source snippets and it works great. But there is a situation where it
doesn't do what I expect.

If I pass in the following string:

a = 10 + 3;

(note: two statements, separated with a newline)

It will give me an error saying that the 'print' is an unexpected token.

If I pass in the following string:

a = 10 + 3; print(a);

It works as expected (printing out 13).

It works fine if there are multiple lines for a statement like:

if( a > 10 ):

If I use SourceCodeKind.Statements then all of the above works just
fine, but I don't get the nice things like the automatic print of the
returned value of the statement or the "_" variable.

Is there a reason why InteractiveCode does things different like that? I
want to give a consistent interface to my users and I think they would
expect that they can give two statements at once (especially if they can
do it on the same line).

I can work around this to get what I want by building up my buffer one
line at a time and testing to see if it is a "complete" statement and
executing that, then continuing to feed in the next line, etc. But, if I
don't have to, I don't want to do that.

Any ideas?


More information about the Ironpython-users mailing list