[IronPython] Problems with PythonSyntaxErrorException

Mike McGavin iizogii at gmail.com
Fri Sep 22 00:01:38 CEST 2006


Hello.

We've been attempting to use IronPython as a scripting engine for an
application we've been writing.  So far it's mostly been working out,
but when trying to report errors about any incoming script, I've
noticed that the PyhonSyntaxErrorException.LineText property seems to
be inconsistent with where the error is.

Here's a simplified program (output follows) which demonstrates it:

===
using System;
using IronPython.Hosting;

namespace SyntaxExceptionProblemDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string syntaxErrorCode = @"
class HasASyntaxException:
    def MethodOne(self):
        print 'hello'
        print 'world'
        print 'again'
    def MethodTwo(self)
        print 'world'
";

            try
            {
                IronPython.Hosting.PythonEngine engine1 = new
IronPython.Hosting.PythonEngine();

                Console.WriteLine("\nAbout to execute\n");
                engine1.Execute(syntaxErrorCode);
            }
            catch (IronPython.Runtime.Exceptions.PythonSyntaxErrorException ex)
            {
                Console.WriteLine("Syntax exception:");
                Console.WriteLine("Message: {0}\nLineText: {1}\nLine:
{2}\nColumn: {3}",
                    ex.Message, ex.LineText, ex.Line, ex.Column);
            }
        }
    }
}
===

Output of this program is:

===
About to execute

Syntax exception:
Message: unexpected token <newline>
LineText:         print 'hello'
Line: 7
Column: 23
===

The Line and Column properties are correct, but the LineText property
seems to be unrelated to where the error is.  Is this a bug, or am I
just misunderstanding what it's supposed to be returning?

I could probably try and rig something myself to manually locate the
correct line and column within the text I've given the engine to
parse, but it'd be nice to be able to use the LineText property
instead.


Thanks for any help.
Mike.



More information about the Ironpython-users mailing list