please help me to use this mailing list - Jython exception trapping question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

please help me to use this mailing list - Jython exception trapping question

Mike Rodent
Hi,

I am a keen Jython user but I just don't know how this (or any) mailing list works (i.e. as a discussion forum).

I have a specific question... but for example I don't know where this email is going.  Where I might be able to see any replies, etc.? Could someone maybe send me an email briefly explaining what I do?  Thanks very much.

My question is to do with exception trapping:

The basic Pythonic exception hook seems to work like this:
def custom_hook(type_of_e, e, tb):
    logger.exception( "custom_hook called...\n" )
sys.excepthook = custom_hook

but this won't catch this (this is essentially a silent exception)
class DoIt( Runnable )
    def run( self ):
        raise Exception()
EventQueue.invokeLater( DoIt() )

so you might do this:
from java.lang import Throwable as JThrowable
class DoIt( Runnable )
    def run( self ):
        try:
            raise Exception()
        except ( BaseException, JThrowable ), e:
            # do sthg with e
 
... trouble is you have to do this with every single "Java method".  So I thought a decorator might be an idea:
class DoIt( Runnable )
    @vigil( True )
    def run( self ):
        raise Exception()

where decorator "vigil" looks like this (its param, True, False or None, checks that the function is called or not in the EDT), but vigil also traps any uncaught exception, Python or Java, raised in the function it precedes:
def vigil( req_edt ):
    if req_edt is not None and type( req_edt ) is not bool:
        msg = "required EDT status (must be None/True/False) is wrong type: %s, type %s"
        raise Exception( msg % ( req_edt, type( req_edt )) )
    def real_decorator( function ):
        if not hasattr( function, '__call__' ):
            raise Exception( "function %s does not have __call__ attr, type %s"
                % ( function, type( function )) )
        # NB info about decorator location can't be got when wrapper called, so record it at this point
        penultimate_frame = traceback.extract_stack()[ -2 ]
        decorator_file = penultimate_frame[ 0 ]   
        decorator_line_no = penultimate_frame[ 1 ]   
           
        def wrapper( *args, **kvargs ):
            try:
                if req_edt is not None and SwingUtilities.isEventDispatchThread() != req_edt:
                    raise Exception( "Wrong EDT status, should be %s, function: %s\nargs %s kvargs %s\n"
                        % ( "EDT" if req_edt else "non-EDT", function, args, kvargs ))
                return function( *args, **kvargs )
            except ( BaseException, JThrowable ), e:
                msg = "exception caught by vigil in file %s, line %d" % ( decorator_file, decorator_line_no )
                logger.exception( msg )
                frame = inspect.currentframe()
                python_stack_trace = traceback.format_stack(frame)
                python_stack_string = "Python stack trace:\n"
                for el in python_stack_trace[ : -1 ]:
                    python_stack_string += el
                logger.error( python_stack_string )
                if isinstance( e, JThrowable ):
                    java_stack_trace = e.stackTrace
                    java_stack_string = "Java stack trace:\n"
                    for el in java_stack_trace:
                        java_stack_string += "  %s\n" % unicode( el )
                    logger.error( java_stack_string )
        return wrapper
    return real_decorator

The problem with the above is that the Java stack trace will tell you a lot of fascinating information about the Java call stack in Jython... but it doesn't appear to be able to tell you at which ***line*** calling functions beyond the reach of the Python stack called the functions which eventually led to the exception.  It gives you the function, and the last line of the function... but the crucial lines in the Java trace have no line numbers.

Has anyone in the Jython community found an answer to this?

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: please help me to use this mailing list - Jython exception trapping question

Jeff Emanuel
You are using the list it exactly right.  It's just a low traffic list.

Try checking the cause from the Java exception, and print its stack trace, too.  Or maybe I'm missing the point.  An example printout might help clarify your situation.
 


 

On 7/8/2014 12:56 PM, Mike Rodent wrote:
Hi,

I am a keen Jython user but I just don't know how this (or any) mailing list works (i.e. as a discussion forum).

I have a specific question... but for example I don't know where this email is going.  Where I might be able to see any replies, etc.? Could someone maybe send me an email briefly explaining what I do?  Thanks very much.

My question is to do with exception trapping:

The basic Pythonic exception hook seems to work like this:
def custom_hook(type_of_e, e, tb):
    logger.exception( "custom_hook called...\n" )
sys.excepthook = custom_hook

but this won't catch this (this is essentially a silent exception)
class DoIt( Runnable )
    def run( self ):
        raise Exception()
EventQueue.invokeLater( DoIt() )

so you might do this:
from java.lang import Throwable as JThrowable
class DoIt( Runnable )
    def run( self ):
        try:
            raise Exception()
        except ( BaseException, JThrowable ), e:
            # do sthg with e
 
... trouble is you have to do this with every single "Java method".  So I thought a decorator might be an idea:
class DoIt( Runnable )
    @vigil( True )
    def run( self ):
        raise Exception()

where decorator "vigil" looks like this (its param, True, False or None, checks that the function is called or not in the EDT), but vigil also traps any uncaught exception, Python or Java, raised in the function it precedes:
def vigil( req_edt ):
    if req_edt is not None and type( req_edt ) is not bool:
        msg = "required EDT status (must be None/True/False) is wrong type: %s, type %s"
        raise Exception( msg % ( req_edt, type( req_edt )) )
    def real_decorator( function ):
        if not hasattr( function, '__call__' ):
            raise Exception( "function %s does not have __call__ attr, type %s"
                % ( function, type( function )) )
        # NB info about decorator location can't be got when wrapper called, so record it at this point
        penultimate_frame = traceback.extract_stack()[ -2 ]
        decorator_file = penultimate_frame[ 0 ]   
        decorator_line_no = penultimate_frame[ 1 ]   
           
        def wrapper( *args, **kvargs ):
            try:
                if req_edt is not None and SwingUtilities.isEventDispatchThread() != req_edt:
                    raise Exception( "Wrong EDT status, should be %s, function: %s\nargs %s kvargs %s\n"
                        % ( "EDT" if req_edt else "non-EDT", function, args, kvargs ))
                return function( *args, **kvargs )
            except ( BaseException, JThrowable ), e:
                msg = "exception caught by vigil in file %s, line %d" % ( decorator_file, decorator_line_no )
                logger.exception( msg )
                frame = inspect.currentframe()
                python_stack_trace = traceback.format_stack(frame)
                python_stack_string = "Python stack trace:\n"
                for el in python_stack_trace[ : -1 ]:
                    python_stack_string += el
                logger.error( python_stack_string )
                if isinstance( e, JThrowable ):
                    java_stack_trace = e.stackTrace
                    java_stack_string = "Java stack trace:\n"
                    for el in java_stack_trace:
                        java_stack_string += "  %s\n" % unicode( el )
                    logger.error( java_stack_string )
        return wrapper
    return real_decorator

The problem with the above is that the Java stack trace will tell you a lot of fascinating information about the Java call stack in Jython... but it doesn't appear to be able to tell you at which ***line*** calling functions beyond the reach of the Python stack called the functions which eventually led to the exception.  It gives you the function, and the last line of the function... but the crucial lines in the Java trace have no line numbers.

Has anyone in the Jython community found an answer to this?


------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft


_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users


------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: please help me to use this mailing list - Jython exception trapping question

Indra Talip
In reply to this post by Mike Rodent
G'day Mike,
From the Java side I've called Py#printException to get the python traceback line numbers etc from the python side of things which is what I assume you are asking for?


You could potentially call that from the python side or look at what it's doing and replicate that into the @vigil decorator.

Cheers
Indra



On 9 July 2014 04:56, Mike Rodent <[hidden email]> wrote:
Hi,

I am a keen Jython user but I just don't know how this (or any) mailing list works (i.e. as a discussion forum).

I have a specific question... but for example I don't know where this email is going.  Where I might be able to see any replies, etc.? Could someone maybe send me an email briefly explaining what I do?  Thanks very much.

My question is to do with exception trapping:

The basic Pythonic exception hook seems to work like this:
def custom_hook(type_of_e, e, tb):
    logger.exception( "custom_hook called...\n" )
sys.excepthook = custom_hook

but this won't catch this (this is essentially a silent exception)
class DoIt( Runnable )
    def run( self ):
        raise Exception()
EventQueue.invokeLater( DoIt() )

so you might do this:
from java.lang import Throwable as JThrowable
class DoIt( Runnable )
    def run( self ):
        try:
            raise Exception()
        except ( BaseException, JThrowable ), e:
            # do sthg with e
 
... trouble is you have to do this with every single "Java method".  So I thought a decorator might be an idea:
class DoIt( Runnable )
    @vigil( True )
    def run( self ):
        raise Exception()

where decorator "vigil" looks like this (its param, True, False or None, checks that the function is called or not in the EDT), but vigil also traps any uncaught exception, Python or Java, raised in the function it precedes:
def vigil( req_edt ):
    if req_edt is not None and type( req_edt ) is not bool:
        msg = "required EDT status (must be None/True/False) is wrong type: %s, type %s"
        raise Exception( msg % ( req_edt, type( req_edt )) )
    def real_decorator( function ):
        if not hasattr( function, '__call__' ):
            raise Exception( "function %s does not have __call__ attr, type %s"
                % ( function, type( function )) )
        # NB info about decorator location can't be got when wrapper called, so record it at this point
        penultimate_frame = traceback.extract_stack()[ -2 ]
        decorator_file = penultimate_frame[ 0 ]   
        decorator_line_no = penultimate_frame[ 1 ]   
           
        def wrapper( *args, **kvargs ):
            try:
                if req_edt is not None and SwingUtilities.isEventDispatchThread() != req_edt:
                    raise Exception( "Wrong EDT status, should be %s, function: %s\nargs %s kvargs %s\n"
                        % ( "EDT" if req_edt else "non-EDT", function, args, kvargs ))
                return function( *args, **kvargs )
            except ( BaseException, JThrowable ), e:
                msg = "exception caught by vigil in file %s, line %d" % ( decorator_file, decorator_line_no )
                logger.exception( msg )
                frame = inspect.currentframe()
                python_stack_trace = traceback.format_stack(frame)
                python_stack_string = "Python stack trace:\n"
                for el in python_stack_trace[ : -1 ]:
                    python_stack_string += el
                logger.error( python_stack_string )
                if isinstance( e, JThrowable ):
                    java_stack_trace = e.stackTrace
                    java_stack_string = "Java stack trace:\n"
                    for el in java_stack_trace:
                        java_stack_string += "  %s\n" % unicode( el )
                    logger.error( java_stack_string )
        return wrapper
    return real_decorator

The problem with the above is that the Java stack trace will tell you a lot of fascinating information about the Java call stack in Jython... but it doesn't appear to be able to tell you at which ***line*** calling functions beyond the reach of the Python stack called the functions which eventually led to the exception.  It gives you the function, and the last line of the function... but the crucial lines in the Java trace have no line numbers.

Has anyone in the Jython community found an answer to this?

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users




--
Indra Talip

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users