From 383ae9e64786bbc96b929a625c02617b4f03b18f Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Wed, 12 Jan 2011 11:15:08 +0100 Subject: [PATCH] Catch SystemExit and KeyboardInterrupt exceptions Previously we did not catch KeyboardInterrupts explicitly as all of the code was executed in forked child threads which would never receive Ctrl-c exceptions. With the upcoming single threaded modus, this code can be run in the main thread however, so we need to take care of KeyboardInterrupts explicitly. As this is pretty highlevel code, we also protect against receiving a SystemExit exception which is raised e.g. in the ui.terminate() code by calling sys.exit(). Signed-off-by: Sebastian Spaeth Signed-off-by: Nicolas Sebrecht --- offlineimap/accounts.py | 6 ++++++ offlineimap/init.py | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py index 0fc1edf..cd09646 100644 --- a/offlineimap/accounts.py +++ b/offlineimap/accounts.py @@ -187,6 +187,8 @@ class AccountSynchronizationMixin: try: try: self.sync(siglistener) + except (KeyboardInterrupt, SystemExit): + raise except: self.ui.warn("Error occured attempting to sync account " + self.name \ + ": " + str(sys.exc_info()[1])) @@ -201,6 +203,8 @@ class AccountSynchronizationMixin: try: try: self.sync(siglistener) + except (KeyboardInterrupt, SystemExit): + raise except: self.ui.warn("Error occured attempting to sync account " + self.name \ + ": " + str(sys.exc_info()[1])) @@ -370,6 +374,8 @@ def syncfolder(accountname, remoterepos, remotefolder, localrepos, localfolder.syncmessagesto(statusfolder) statusfolder.save() localrepos.restore_atime() + except (KeyboardInterrupt, SystemExit): + raise except: ui.warn("ERROR in syncfolder for %s folder %s: %s" % \ (accountname,remotefolder.getvisiblename(),sys.exc_info()[1])) diff --git a/offlineimap/init.py b/offlineimap/init.py index d0676bc..f652328 100644 --- a/offlineimap/init.py +++ b/offlineimap/init.py @@ -334,14 +334,14 @@ class OfflineImap: 'siglisteners': siglisteners}) t.setDaemon(1) t.start() - except: - ui.mainException() - - try: threadutil.exitnotifymonitorloop(threadutil.threadexited) - except SystemExit: + + except KeyboardInterrupt: + ui.terminate(1, errormsg = 'CTRL-C pressed, aborting...') + return + except (SystemExit): raise except: - ui.mainException() # Also expected to terminate. + ui.mainException()