Changed nature of threaded implementation to give more descriptive debug messages and to catch and report uncaught exceptions.

This commit is contained in:
Jeremy Fincher 2003-03-26 02:30:05 +00:00
parent 1808366dde
commit 4d43fe11db
1 changed files with 22 additions and 5 deletions

View File

@ -234,7 +234,7 @@ class IrcObjectProxy:
if callback is None: if callback is None:
self.reply(msg, '[%s]' % ' '.join(args)) self.reply(msg, '[%s]' % ' '.join(args))
callback.callCommand(getattr(callback, name), callback.callCommand(getattr(callback, name),
(self, self.msg, self.args)) self, self.msg, self.args)
except Error, e: except Error, e:
self.reply(self.msg, debug.exnToString(e)) self.reply(self.msg, debug.exnToString(e))
except Exception, e: except Exception, e:
@ -273,6 +273,23 @@ class IrcObjectProxyThreaded(IrcObjectProxy):
def error(self, msg, s): def error(self, msg, s):
self.reply(msg, 'Error: ' + s) self.reply(msg, 'Error: ' + s)
class CommandThread(threading.Thread):
def __init__(self, command, irc, msg, args):
name = '%s.%s with args %r' % (command.im_class.__name__,
command.im_func.func_name, args)
threading.Thread.__init__(self, target=command, name=name,
args=(irc, msg, args))
self.irc = irc
self.msg = msg
def run(self):
try:
threading.Thread.run(self)
except Error, e:
self.irc.reply(self.msg, debug.exnToString(e))
except Exception, e:
debug.recoverableException()
self.irc.error(self.msg, debug.exnToString(e))
class Privmsg(irclib.IrcCallback): class Privmsg(irclib.IrcCallback):
"""Base class for all Privmsg handlers.""" """Base class for all Privmsg handlers."""
@ -307,14 +324,14 @@ class Privmsg(irclib.IrcCallback):
else: else:
return False return False
def callCommand(self, f, args): def callCommand(self, f, irc, msg, args):
if self.threaded: if self.threaded:
thread = threading.Thread(target=f, args=args) thread = CommandThread(f, irc, msg, args)
thread.setDaemon(True) thread.setDaemon(True)
thread.start() thread.start()
debug.printf('Spawned new thread: %s' % thread) debug.printf('Spawned new thread: %s' % thread)
else: else:
f(*args) f(irc, msg, args)
def _getCommands(self, args): def _getCommands(self, args):
commands = [] commands = []
@ -423,5 +440,5 @@ class PrivmsgRegexp(Privmsg):
msg = self.rateLimiter.get() msg = self.rateLimiter.get()
if msg: if msg:
irc = IrcObjectProxyRegexp(irc) irc = IrcObjectProxyRegexp(irc)
self.callCommand(method, (irc, msg, m)) self.callCommand(method, irc, msg, m)
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: