Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update machine state on unhandled exceptions #1560

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news.d/bugfix/1560.ui.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update the tray icon to "disconnected" when a serial-over-USB machine is unplugged.
8 changes: 8 additions & 0 deletions plover/machine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,18 @@ class ThreadedStenotypeBase(StenotypeBase, threading.Thread):
"""
def __init__(self):
threading.Thread.__init__(self)
self._on_unhandled_exception(self._error)
self.name += '-machine'
StenotypeBase.__init__(self)
self.finished = threading.Event()

def _on_unhandled_exception(self, action):
super_invoke_excepthook = self._invoke_excepthook
def invoke_excepthook(self):
action()
super_invoke_excepthook(self)
self._invoke_excepthook = invoke_excepthook

def run(self):
"""This method should be overridden by a subclass."""
pass
Expand Down
14 changes: 14 additions & 0 deletions test/test_machine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from unittest.mock import Mock
from plover.machine.base import ThreadedStenotypeBase

class MyMachine(ThreadedStenotypeBase):
def run(self):
raise "some unexpected error"

def test_update_machine_staten_on_unhandled_exception():
machine = MyMachine()
callback = Mock()
machine.add_state_callback(callback)
machine.start_capture()
machine.join()
callback.assert_called_with('disconnected')