[IronPython] Problems with PythonSyntaxErrorException

J. Merrill jvm_cop at spamcop.net
Fri Oct 6 22:24:34 CEST 2006

That value of LineText may be a bug (per Martin), but it would make some sense to me if the line & column tell you where in the source the problem seems to have started (that is, where the parser was when it went looking for follolwing text to satisfy the syntax rules and couldn't find anything that qualified), while LineText tells you what text was being processed when the parser realizes\d that there was a syntax error.  That is, I don't think that it's necessarily incorrect for the "line & column of the error" to be different from "line of text being processed when the syntax error is finally known to exist."  

For example, if you accidentally fail to end a triple-quoted (multi-line) string, the error isn't going to be found until the whole program has been processed, as it will all appear to be part of the multi-line string.  But the notion that there's a not-ever-closed string that began way back up where the triple-quoted string was started could get reflected in the line & column info.

So I think you should find the line & column info, get the code of that line, and show both that and LineText.  (But then again, I'm quite anal when it comes to error reporting.)

At 06:01 PM 9/21/2006, Mike McGavin wrote
>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
>                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
>Thanks for any help.

J. Merrill / Analytical Software Corp

More information about the Ironpython-users mailing list