diff --git a/evdev/uinput.py b/evdev/uinput.py index 756f83c..2c6b980 100644 --- a/evdev/uinput.py +++ b/evdev/uinput.py @@ -268,13 +268,11 @@ def _verify(self): Verify that an uinput device exists and is readable and writable by the current process. """ - try: m = os.stat(self.devnode)[stat.ST_MODE] - if not stat.S_ISCHR(m): - raise OSError - except (IndexError, OSError): - msg = '"{}" does not exist or is not a character device file ' "- verify that the uinput module is loaded" + assert stat.S_ISCHR(m) + except (IndexError, OSError, AssertionError): + msg = '"{}" does not exist or is not a character device file - verify that the uinput module is loaded' raise UInputError(msg.format(self.devnode)) if not os.access(self.devnode, os.W_OK): diff --git a/tests/test_uinput.py b/tests/test_uinput.py index dcd09e0..666361f 100644 --- a/tests/test_uinput.py +++ b/tests/test_uinput.py @@ -1,4 +1,5 @@ # encoding: utf-8 +import os import stat from select import select from unittest.mock import patch @@ -119,6 +120,18 @@ def test_write(c): @patch.object(stat, 'S_ISCHR', return_value=False) -def test_not_a_character_device(c): - with pytest.raises(UInputError): +def test_not_a_character_device(ischr_mock, c): + with pytest.raises(UInputError, match='not a character device file'): + uinput.UInput(**c) + +@patch.object(stat, 'S_ISCHR', return_value=True) +@patch.object(os, 'stat', side_effect=OSError()) +def test_not_a_character_device_2(stat_mock, ischr_mock, c): + with pytest.raises(UInputError, match='not a character device file'): + uinput.UInput(**c) + +@patch.object(stat, 'S_ISCHR', return_value=True) +@patch.object(os, 'stat', return_value=[]) +def test_not_a_character_device_3(stat_mock, ischr_mock, c): + with pytest.raises(UInputError, match='not a character device file'): uinput.UInput(**c)