Re: sys.excepthook and AppHelper.runEventLoop

Ben Smith <[email protected]> Tue, 20 Nov 2012 02:11:45 +0000 (UTC)
Newsgroups gmane.comp.python.pyobjc.devel
Message-ID <[email protected]>
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 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?

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