/head: changeset 78
Revamped thread system to handle exceptions better
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
from UIBase import UIBase
|
||||
from getpass import getpass
|
||||
import select, sys
|
||||
from threading import *
|
||||
|
||||
class TTYUI(UIBase):
|
||||
def __init__(self, verbose = 0):
|
||||
self.verbose = 0
|
||||
self.iswaiting = 0
|
||||
|
||||
def _msg(s, msg):
|
||||
print msg
|
||||
@ -28,11 +30,19 @@ class TTYUI(UIBase):
|
||||
UIBase.messagelistloaded(s, repos, folder, count)
|
||||
|
||||
def sleep(s, sleepsecs):
|
||||
s.iswaiting = 1
|
||||
try:
|
||||
UIBase.sleep(s, sleepsecs)
|
||||
except KeyboardInterrupt:
|
||||
finally:
|
||||
s.iswaiting = 0
|
||||
|
||||
def mainException(s):
|
||||
if isinstance(sys.exc_info()[1], KeyboardInterrupt) and \
|
||||
s.iswaiting:
|
||||
sys.stdout.write("Timer interrupted at user request; program terminating. \n")
|
||||
return 2
|
||||
s.terminate()
|
||||
else:
|
||||
UIBase.mainException(s)
|
||||
|
||||
def sleeping(s, sleepsecs, remainingsecs):
|
||||
if remainingsecs > 0:
|
||||
|
@ -18,7 +18,8 @@
|
||||
|
||||
from offlineimap import repository
|
||||
import offlineimap.version
|
||||
import re, time
|
||||
import re, time, sys, traceback
|
||||
from StringIO import StringIO
|
||||
|
||||
class UIBase:
|
||||
################################################## UTILS
|
||||
@ -108,6 +109,30 @@ class UIBase:
|
||||
s._msg("Deleting flags %s to message %d on %s" % \
|
||||
(", ".join(flags), uid, ds))
|
||||
|
||||
################################################## Threads
|
||||
|
||||
def threadException(s, thread):
|
||||
"""Called when a thread has terminated with an exception.
|
||||
The argument is the ExitNotifyThread that has so terminated."""
|
||||
s._msg("Thread '%s' terminated with exception:\n%s" % \
|
||||
(thread.getName(), thread.getExitStackTrace()))
|
||||
s.terminate(100)
|
||||
|
||||
def mainException(s):
|
||||
sbuf = StringIO()
|
||||
traceback.print_exc(file = sbuf)
|
||||
s._msg("Main program terminated with exception:\n" +
|
||||
sbuf.getvalue())
|
||||
|
||||
def terminate(s, exitstatus = 0):
|
||||
"""Called to terminate the application."""
|
||||
sys.exit(exitstatus)
|
||||
|
||||
def threadExited(s, thread):
|
||||
"""Called when a thread has exited normally. Many UIs will
|
||||
just ignore this."""
|
||||
pass
|
||||
|
||||
################################################## Other
|
||||
|
||||
def sleep(s, sleepsecs):
|
||||
|
Reference in New Issue
Block a user