Skip to content

Commit

Permalink
Update machine state on unhandled exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Sep 1, 2022
1 parent 3066a9a commit 2c44a27
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
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')

0 comments on commit 2c44a27

Please sign in to comment.