ironpython exception line number

Dino Viehland dinov at exchange.microsoft.com
Thu Jun 28 16:12:32 EDT 2007


Given a file foo.py:

def f():

You should get these results:

IronPython 1.0.60816 on .NET 2.0.50727.312
Copyright (c) Microsoft Corporation. All rights reserved.
>>> try:
...     execfile('foo.py')
... except IndentationError, e:
...     import sys
...     x = sys.exc_info()
...
>>> print x[1].filename, x[1].lineno, x[1].msg, x[1].offset, x[1].text, x[1].args
foo.py 2 unexpected token <eof> 1  ('unexpected token <eof>', ('foo.py', 2, 1, ''))
>>>
>>>

Which is very similar to the result you get from CPython although we seem to disagree about what we expect next.

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
...     execfile('foo.py')
... except IndentationError, e:
...     import sys
...     x = sys.exc_info()
...
>>> print x[1].filename, x[1].lineno, x[1].msg, x[1].offset, x[1].text, x[1].args
foo.py 2 expected an indented block 9  ('expected an indented block', ('foo.py', 2, 9, ''))
>>> ^Z


If you're hosting IronPython and catching this from a .NET language then you'll be catching the .NET exception.  In that case you can access the original Python exception from ex.Data["PythonExceptionInfo"].  Alternately you could catch PythonSyntaxErrorException and access its properties (Line, Column, FileName, LineText, Severity, and ErrorCode).

-----Original Message-----
From: python-list-bounces+dinov=microsoft.com at python.org [mailto:python-list-bounces+dinov=microsoft.com at python.org] On Behalf Of Troels Thomsen
Sent: Tuesday, June 26, 2007 1:33 PM
To: python-list at python.org
Subject: ironpython exception line number


Hello ,

When an exeption occurs in a IronPython executet script, and I print the
sys.exc , i get something ugly like the example below.
How can I get the fileName and line number?

Thx in advance
Troels


26-06-2007 13:19:04 : IronPython.Runtime.Exceptions.PythonIndentationError:
unexpected token def
   ved IronPython.Compiler.SimpleParserSink.AddError(String path, String
message, String lineText, CodeSpan span, Int32 errorCode, Severity severity)

   ved IronPython.Compiler.CompilerContext.AddError(String message, String
lineText, Int32 startLine, Int32 startColumn, Int32 endLine, Int32
endColumn, Int32 errorCode, Severity severity)

   ved IronPython.Compiler.Parser.ReportSyntaxError(Location start, Location
end, String message, Int32 errorCode)
   ved IronPython.Compiler.Parser.ReportSyntaxError(Token t, Int32
errorCode, Boolean allowIncomplete)
   ved IronPython.Compiler.Parser.ParseSuite()
   ved IronPython.Compiler.Parser.ParseFuncDef()
   ved IronPython.Compiler.Parser.ParseStmt()
   ved IronPython.Compiler.Parser.ParseSuite()
   ved IronPython.Compiler.Parser.ParseClassDef()
   ved IronPython.Compiler.Parser.ParseStmt()
   ved IronPython.Compiler.Parser.ParseFileInput()
   ved IronPython.Hosting.PythonEngine.Compile(Parser p, Boolean
debuggingPossible)
   ved IronPython.Hosting.PythonEngine.CompileFile(String fileName)
   ved IronPython.Hosting.PythonEngine.ExecuteFile(String fileName)



--
http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list