Skip to content

Commit

Permalink
rename Hub._run to Hub.run_once(), reraising exception in ctyping.Hub…
Browse files Browse the repository at this point in the history
….run() from wrapper(). Fixes NiklasRosenstein#31
  • Loading branch information
NiklasRosenstein committed Dec 14, 2015
1 parent b07426a commit 290551e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
19 changes: 6 additions & 13 deletions myo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions myo/lowlevel/ctyping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 290551e

Please sign in to comment.