From d4d8a6b891d1488fc0b15852d6852505683ac121 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Sat, 17 Oct 2009 21:52:29 -0400 Subject: [PATCH] Properly fix problem fixed in last commit. Cleanup some stuff in the process. Update issue #132 Status: Fixed This is a very strange issue. It turns out that subprocess won't work from non-main threads while a module is being imported. Since the wmiirc entered its event loop rather than returning, it was causing problems. I suspect it was also the cause of the stack traces being printed at interperater shutdown. --- alternative_wmiircs/python/pygmi/event.py | 2 +- alternative_wmiircs/python/pygmi/menu.py | 41 ++++++++++------------- alternative_wmiircs/python/wmiirc | 5 ++- alternative_wmiircs/python/wmiirc.py | 2 -- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/alternative_wmiircs/python/pygmi/event.py b/alternative_wmiircs/python/pygmi/event.py index 6e0263e4..c56460af 100644 --- a/alternative_wmiircs/python/pygmi/event.py +++ b/alternative_wmiircs/python/pygmi/event.py @@ -108,7 +108,7 @@ class Events(): def loop(self): """ - Enters teh event loop, reading lines from wmii's '/event' + Enters the event loop, reading lines from wmii's '/event' and dispatching them, via #dispatch, to event handlers. Continues so long as #alive is True. """ diff --git a/alternative_wmiircs/python/pygmi/menu.py b/alternative_wmiircs/python/pygmi/menu.py index 07395cbb..47115762 100644 --- a/alternative_wmiircs/python/pygmi/menu.py +++ b/alternative_wmiircs/python/pygmi/menu.py @@ -1,16 +1,13 @@ +from threading import Thread from pygmi.util import call __all__ = 'Menu', 'ClickMenu' -def inthread(fn, action): - def run(): - res = fn() - if action: - return action(res) - return res - return run() - # Bug. - t = Thread(target=run) +def inthread(args, action, **kwargs): + fn = lambda: call(*args, **kwargs) + if not action: + return fn() + t = Thread(target=lambda: action(fn())) t.daemon = True t.start() @@ -27,14 +24,12 @@ class Menu(object): choices = self.choices if callable(choices): choices = choices() - def act(): - args = ['wimenu'] - if self.histfile: - args += ['-h', self.histfile] - if self.nhist: - args += ['-n', self.nhist] - return call(*map(str, args), input='\n'.join(choices)) - return inthread(act, self.action) + args = ['wimenu'] + if self.histfile: + args += ['-h', self.histfile] + if self.nhist: + args += ['-n', self.nhist] + return inthread(map(str, args), self.action, input='\n'.join(choices)) call = __call__ class ClickMenu(object): @@ -49,13 +44,11 @@ class ClickMenu(object): choices = self.choices if callable(choices): choices = choices() - def act(): - args = ['wmii9menu'] - if self.prev: - args += ['-i', self.prev] - args += ['--'] + list(choices) - return call(*map(str, args)).replace('\n', '') - return inthread(act, self.action) + args = ['wmii9menu'] + if self.prev: + args += ['-i', self.prev] + args += ['--'] + list(choices) + return inthread(map(str, args), self.action) call = __call__ # vim:se sts=4 sw=4 et: diff --git a/alternative_wmiircs/python/wmiirc b/alternative_wmiircs/python/wmiirc index ee40f8df..75a148ac 100755 --- a/alternative_wmiircs/python/wmiirc +++ b/alternative_wmiircs/python/wmiirc @@ -5,5 +5,8 @@ for p in os.environ.get("WMII_CONFPATH", "").split(':'): path += [p, p + '/python'] sys.path = path + sys.path -import pygmi +from pygmi import events import wmiirc + +events.loop() + diff --git a/alternative_wmiircs/python/wmiirc.py b/alternative_wmiircs/python/wmiirc.py index 35c4dc61..1a4c2627 100644 --- a/alternative_wmiircs/python/wmiirc.py +++ b/alternative_wmiircs/python/wmiirc.py @@ -304,6 +304,4 @@ for f in ['wmiirc_local'] + ['plugins.%s' % file[:-3] for file in files]: except Exception, e: traceback.print_exc(sys.stdout) -events.loop() - # vim:se sts=4 sw=4 et: