Getting python exceptions printed the normal way with PyObjC
Asked Answered
L

2

5

I'm getting errors like this:

2010-07-13 20:43:15.131 Python[1527:60f] main: Caught OC_PythonException: : LoginMenuSet instance has no attribute 'play_sound'

That's with this code:

@try {
    [section loop]; //Loop through section
} @catch (NSException *exception) {
    NSLog(@"Caught %@: %@", [exception name], [exception reason]);
}

I want the python exception to be printed normally with the traceback and everything else.

Thank you.

Lindemann answered 13/7, 2010 at 19:46 Comment(0)
L
0

Here's my own solution:

In Objective-C class:

@try {
        [section loop]; //Loop through section
    } @catch (NSException *exception) {
        NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
        [self exception: [[exception userInfo] valueForKey: @"__pyobjc_exc_traceback__"]];
    }

In python pyobjc subclass:

def exception_(self,trace):
        traceback.print_tb(trace)
        NSApplication.sharedApplication().terminate_(None) #Accept no errors

I, of-course, imported the traceback module.

Lindemann answered 1/9, 2010 at 12:24 Comment(0)
E
9

One trick to see Python exceptions is to call objc.setVerbose(1). This makes PyObjC slightly more verbose and causes it to print Python stack traces when converting exceptions from Python to Objective-C.

Egidius answered 31/8, 2010 at 14:1 Comment(2)
Thank you for this answer but since asking this question I did find my own solution eventually. I forgot to answer my own question but I'll do that below.Lindemann
For completeness, you must first import objc.Kolodgie
L
0

Here's my own solution:

In Objective-C class:

@try {
        [section loop]; //Loop through section
    } @catch (NSException *exception) {
        NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
        [self exception: [[exception userInfo] valueForKey: @"__pyobjc_exc_traceback__"]];
    }

In python pyobjc subclass:

def exception_(self,trace):
        traceback.print_tb(trace)
        NSApplication.sharedApplication().terminate_(None) #Accept no errors

I, of-course, imported the traceback module.

Lindemann answered 1/9, 2010 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.