ironpython exception line number

T

Troels Thomsen

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)
 
D

Dino Viehland

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..... 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

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..... 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: [email protected] [mailto:p[email protected]] On Behalf Of Troels Thomsen
Sent: Tuesday, June 26, 2007 1:33 PM
To: (e-mail address removed)
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)
 
T

Troels Thomsen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top