Re: sys.excepthook and AppHelper.runEventLoop

Ronald Oussoren <[email protected]> Tue, 20 Nov 2012 09:07:27 +0100
Newsgroups gmane.comp.python.pyobjc.devel
Message-ID <[email protected]>
On 20 Nov, 2012, at 3:11, Ben Smith <[email protected]> wrote:

> Benjamin Smith <bcsmith05 <at> gmail.com> writes:
> 
>> 
>> Hi all,
>> 
>> I have a python script that I'm bundling with py2app and run using
> AppHelper.runEventLoop.  In my script, I
>> set sys.excepthook to a custom function in order to do some cleanup when there
> is an unhandled exception. 
>> However, because runEventLoop runs the entire runloop inside a try, except
> block, sys.excepthook is
>> never called and the application simply terminates.  It looks like the only
> way to mimic this behavior is to
>> pass in my cleanup code within the 'unexpectedErrorAlert' parameter to
> runEventLoop, but that seemed to
>> not be the best approach.  Is there another way to mimic sys.excepthook that
> I'm missing?
>> 
>> Thanks!
>> Ben
>> ------------------------------------------------------------------------------
>> Monitor your physical, virtual and cloud infrastructure from a single
>> web console. Get in-depth insight into apps, servers, databases, vmware,
>> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
>> Pricing starts from $795 for 25 servers or applications!
>> http://p.sf.net/sfu/zoho_dev2dev_nov
>> 
> 
> Hi again,
> 
> It looks like my understanding earlier was not correct.  The problem was
> unrelated to runEventLoop's try/catch blocks, but rather seemed to be related to
> the fact that my exception was being raised in code that was fired off from
> applicationDidFinishLaunching.  It seems that when python code raises an
> exception within a method called from the run loop, the exception is handled by
> the run loop (generating an objective C exception) and so never reaches the
> python unhandled exception handler.

The Objective-C exception should be converted to a Python one by the bridge,
unless the runloop swallows the exception (and it does that at least for some
exceptions).

> 
> The first thing we tried was to use NSSetUncaughtExceptionHandler to catch the
> objective-C exceptions, but it seems like that function is not implemented.  So
> now what we're going with is to write a decorator to wrap methods that could be
> called from the run loop in try, except, and then on the except, route the
> exception to our sys.excepthook function.  Does that sound reasonable?

That's the best solution. In general Apple's code doesn't like exceptions at all,
throwing exceptions through an Objective-C callstack could result in resource
leaks and other misbehavior. That's because Apple tends to use exceptions only
to signal programmer errors, "normal" errors are usually signalled using return
values and/or NSError values.

You could also call "objc.setVerbose(1)". After that call PyObjC will print exceptions
when converting them to/from Objective-C. 

Ronald
> 
> Thanks a bunch!
> Ben
> 
> 
> ------------------------------------------------------------------------------
> Monitor your physical, virtual and cloud infrastructure from a single
> web console. Get in-depth insight into apps, servers, databases, vmware,
> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
> Pricing starts from $795 for 25 servers or applications!
> http://p.sf.net/sfu/zoho_dev2dev_nov
> _______________________________________________
> Pyobjc-dev mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/pyobjc-dev


------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov