ironpython exception line number

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Troels Thomsen

    #1

    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.Runt ime.Exceptions. PythonIndentati onError:
    unexpected token def
    ved IronPython.Comp iler.SimplePars erSink.AddError (String path, String
    message, String lineText, CodeSpan span, Int32 errorCode, Severity severity)

    ved IronPython.Comp iler.CompilerCo ntext.AddError( String message, String
    lineText, Int32 startLine, Int32 startColumn, Int32 endLine, Int32
    endColumn, Int32 errorCode, Severity severity)

    ved IronPython.Comp iler.Parser.Rep ortSyntaxError( Location start, Location
    end, String message, Int32 errorCode)
    ved IronPython.Comp iler.Parser.Rep ortSyntaxError( Token t, Int32
    errorCode, Boolean allowIncomplete )
    ved IronPython.Comp iler.Parser.Par seSuite()
    ved IronPython.Comp iler.Parser.Par seFuncDef()
    ved IronPython.Comp iler.Parser.Par seStmt()
    ved IronPython.Comp iler.Parser.Par seSuite()
    ved IronPython.Comp iler.Parser.Par seClassDef()
    ved IronPython.Comp iler.Parser.Par seStmt()
    ved IronPython.Comp iler.Parser.Par seFileInput()
    ved IronPython.Host ing.PythonEngin e.Compile(Parse r p, Boolean
    debuggingPossib le)
    ved IronPython.Host ing.PythonEngin e.CompileFile(S tring fileName)
    ved IronPython.Host ing.PythonEngin e.ExecuteFile(S tring fileName)



  • Dino Viehland

    #2
    RE: ironpython exception line number

    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.p y')
    .... except IndentationErro r, 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.p y')
    .... except IndentationErro r, 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["PythonExceptio nInfo"]. Alternately you could catch PythonSyntaxErr orException and access its properties (Line, Column, FileName, LineText, Severity, and ErrorCode).

    -----Original Message-----
    From: python-list-bounces+dinov=m icrosoft.com@py thon.org [mailto:python-list-bounces+dinov=m icrosoft.com@py thon.org] On Behalf Of Troels Thomsen
    Sent: Tuesday, June 26, 2007 1:33 PM
    To: python-list@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.Runt ime.Exceptions. PythonIndentati onError:
    unexpected token def
    ved IronPython.Comp iler.SimplePars erSink.AddError (String path, String
    message, String lineText, CodeSpan span, Int32 errorCode, Severity severity)

    ved IronPython.Comp iler.CompilerCo ntext.AddError( String message, String
    lineText, Int32 startLine, Int32 startColumn, Int32 endLine, Int32
    endColumn, Int32 errorCode, Severity severity)

    ved IronPython.Comp iler.Parser.Rep ortSyntaxError( Location start, Location
    end, String message, Int32 errorCode)
    ved IronPython.Comp iler.Parser.Rep ortSyntaxError( Token t, Int32
    errorCode, Boolean allowIncomplete )
    ved IronPython.Comp iler.Parser.Par seSuite()
    ved IronPython.Comp iler.Parser.Par seFuncDef()
    ved IronPython.Comp iler.Parser.Par seStmt()
    ved IronPython.Comp iler.Parser.Par seSuite()
    ved IronPython.Comp iler.Parser.Par seClassDef()
    ved IronPython.Comp iler.Parser.Par seStmt()
    ved IronPython.Comp iler.Parser.Par seFileInput()
    ved IronPython.Host ing.PythonEngin e.Compile(Parse r p, Boolean
    debuggingPossib le)
    ved IronPython.Host ing.PythonEngin e.CompileFile(S tring fileName)
    ved IronPython.Host ing.PythonEngin e.ExecuteFile(S tring fileName)



    --

    Comment

    • Troels Thomsen

      #3
      Re: ironpython exception line number

      >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["PythonExceptio nInfo"].
      Yes ! YES !
      Thx

      regards tpt


      Comment

      Working...