Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ironpython exception line number

21 views
Skip to first unread message

Troels Thomsen

unread,
Jun 26, 2007, 8:33:07 PM6/26/07
to

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)

Dino Viehland

unread,
Jun 28, 2007, 8:12:32 PM6/28/07
to Troels Thomsen, pytho...@python.org
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).


Hello ,

Thx in advance
Troels

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

Troels Thomsen

unread,
Jun 30, 2007, 8:54:43 AM6/30/07
to

>If you're hosting IronPython and catching this from a .NET language
>then you'll be catching the .NET exception.

Yes

>In that case you can access the original Python exception
>from ex.Data["PythonExceptionInfo"].

Yes ! YES !
Thx

regards tpt


0 new messages