Re: [Pyobjc-dev] NSApplicationMain idiom: wish to standardize
Brought to you by:
ronaldoussoren
From: Jack J. <Jac...@cw...> - 2003-03-24 16:04:01
|
On Thursday, Mar 20, 2003, at 15:23 Europe/Amsterdam, Just van Rossum wrote: > I've been using the following idiom to enter the Cocoa main event loop > for > some time now: > > > def unexpectedErrorAlert(): > exceptionInfo = > traceback.format_exception_only(*sys.exc_info()[:2])[0].strip() > return NSRunAlertPanel("An unexpected error has occurred", > "(%s)" % exceptionInfo, > "Continue", "Quit", None) > > > mainFunc = NSApplicationMain > args = (sys.argv,) > while 1: > try: > mainFunc(*args) > except: > traceback.print_exc() > if not unexpectedErrorAlert(): > break > mainFunc = NSApp().run > args = () > else: > break Just, I modified this a bit, by putting everything, including all needed imports, into a main() program. Also, in stead of printing the traceback before calling unexpectedErrorAlert() I call raise in stead of break, this keeps the console log clean if the user decides to continue and it also enables debugging with the standard trick of setting PYTHONINSPECT=1 before you run your program, and then doing "import pdb; pdb.pm()" after the program has crashed. Here it is: def main(): from AppKit import NSApplicationMain, NSRunAlertPanel, NSApp import traceback import sys def unexpectedErrorAlert(): exceptionInfo = traceback.format_exception_only(*sys.exc_info()[:2])[0].strip() return NSRunAlertPanel("An unexpected error has occurred", "(%s)" % exceptionInfo, "Continue", "Quit", None) mainFunc = NSApplicationMain args = (sys.argv,) while 1: try: mainFunc(*args) except: if not unexpectedErrorAlert(): raise mainFunc = NSApp().run args = () else: break if __name__ == '__main__': main() -- Jack Jansen, <Jac...@cw...>, http://www.cwi.nl/~jack If I can't dance I don't want to be part of your revolution -- Emma Goldman |