diff --git a/myo/__init__.py b/myo/__init__.py index f44b213..252fe81 100644 --- a/myo/__init__.py +++ b/myo/__init__.py @@ -159,10 +159,9 @@ def set_locking_policy(self, locking_policy): self._hub.set_locking_policy(locking_policy) self._locking_policy = locking_policy - def _run(self, duration_ms, listener): + def run_once(self, duration_ms, listener): """ - Private version of the :meth:`run` method. Does not - re-set the :attr:`running` attribute. Used by :meth:`run`. + Run *listener* for *duration_ms* seconds. """ if not isinstance(listener, DeviceListener): @@ -177,20 +176,14 @@ def _run(self, duration_ms, listener): def callback(listener, event): try: - # Stop immediately if the Hub was stopped via the - # stop() method. with self._lock: if self._stopped: return False - - # Invoke the listener but catch the event. return _invoke_listener(listener, event) - except Exception as exc: + except BaseException: with self._lock: - self._exception = exc - traceback.print_exc() - - return False + self._exception = sys.exc_info() + raise return self._hub.run(duration_ms, callback, listener) @@ -227,7 +220,7 @@ def run(self, interval_ms, listener, lil_sleep=0.01): def worker(): try: while not self.stop_requested: - if not self._run(interval_ms, listener): + if not self.run_once(interval_ms, listener): self.stop() finally: with self._lock: diff --git a/myo/lowlevel/ctyping.py b/myo/lowlevel/ctyping.py index 1925eec..723914a 100644 --- a/myo/lowlevel/ctyping.py +++ b/myo/lowlevel/ctyping.py @@ -280,8 +280,8 @@ def wrapper(ud, event): # warn the user. try: result = callback(ud, event) - except Exception: - traceback.print_exc() + except BaseException: + wrapper.exc_info = sys.exc_info() result = False # Warn the user if the callback did not return a @@ -308,8 +308,11 @@ def wrapper(ud, event): ud, byref(error)) error.raise_on_error() - # Return True if the run was complete, meaning the callback - # did not request to halt the Hub. + # Did an exception occur in the callback? Propagate it. + exc_info = getattr(wrapper, 'exc_info', None) + if exc_info: + six.reraise(*exc_info) + return not getattr(wrapper, 'stopped', False) def shutdown(self):