[IronPython] Problems with PythonSyntaxErrorException

Martin Maly Martin.Maly at microsoft.com
Tue Sep 26 19:03:11 CEST 2006


This is a bug. IronPython doesn't always determine the accurate location of the syntax errors. Filed as CodePlex issue 3731.

Martin

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Mike McGavin
Sent: Thursday, September 21, 2006 3:02 PM
To: users at lists.ironpython.com
Subject: [IronPython] Problems with PythonSyntaxErrorException

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.
_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list