471,354 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

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.PythonIndentationErr or:
unexpected token def
ved IronPython.Compiler.SimpleParserSink.AddError(Stri ng path, String
message, String lineText, CodeSpan span, Int32 errorCode, Severity severity)

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

ved IronPython.Compiler.Parser.ReportSyntaxError(Locat ion 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)

Jun 26 '07 #1
2 5361
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 <eof1 ('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: py*************************************@python.org [mailto:py*************************************@pyt hon.org] On Behalf Of Troels Thomsen
Sent: Tuesday, June 26, 2007 1:33 PM
To: py*********@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.PythonIndentationErr or:
unexpected token def
ved IronPython.Compiler.SimpleParserSink.AddError(Stri ng path, String
message, String lineText, CodeSpan span, Int32 errorCode, Severity severity)

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

ved IronPython.Compiler.Parser.ReportSyntaxError(Locat ion 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
Jun 28 '07 #2
>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
Jun 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Carl Johan Rehn | last post: by
3 posts views Thread by Sanghyeon Seo | last post: by
7 posts views Thread by sanxiyn | last post: by
9 posts views Thread by Claudio Grondi | last post: by
1 post views Thread by edfialk | last post: by
1 post views Thread by Divya | last post: by
3 posts views Thread by Jack | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.