Skip to content

Commit 83f9360

Browse files
authored
Small character device verification cleanup (#229)
1 parent abd286e commit 83f9360

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

evdev/uinput.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,11 @@ def _verify(self):
266266
Verify that an uinput device exists and is readable and writable
267267
by the current process.
268268
"""
269-
270269
try:
271270
m = os.stat(self.devnode)[stat.ST_MODE]
272-
if not stat.S_ISCHR(m):
273-
raise OSError
274-
except (IndexError, OSError):
275-
msg = '"{}" does not exist or is not a character device file ' "- verify that the uinput module is loaded"
271+
assert stat.S_ISCHR(m)
272+
except (IndexError, OSError, AssertionError):
273+
msg = '"{}" does not exist or is not a character device file - verify that the uinput module is loaded'
276274
raise UInputError(msg.format(self.devnode))
277275

278276
if not os.access(self.devnode, os.W_OK):

tests/test_uinput.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# encoding: utf-8
2+
import os
23
import stat
34
from select import select
45
from unittest.mock import patch
@@ -119,6 +120,18 @@ def test_write(c):
119120

120121

121122
@patch.object(stat, 'S_ISCHR', return_value=False)
122-
def test_not_a_character_device(c):
123-
with pytest.raises(UInputError):
123+
def test_not_a_character_device(ischr_mock, c):
124+
with pytest.raises(UInputError, match='not a character device file'):
125+
uinput.UInput(**c)
126+
127+
@patch.object(stat, 'S_ISCHR', return_value=True)
128+
@patch.object(os, 'stat', side_effect=OSError())
129+
def test_not_a_character_device_2(stat_mock, ischr_mock, c):
130+
with pytest.raises(UInputError, match='not a character device file'):
131+
uinput.UInput(**c)
132+
133+
@patch.object(stat, 'S_ISCHR', return_value=True)
134+
@patch.object(os, 'stat', return_value=[])
135+
def test_not_a_character_device_3(stat_mock, ischr_mock, c):
136+
with pytest.raises(UInputError, match='not a character device file'):
124137
uinput.UInput(**c)

0 commit comments

Comments
 (0)