Skip to content

Commit

Permalink
Small character device verification cleanup (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb authored Jan 21, 2025
1 parent abd286e commit 83f9360
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
8 changes: 3 additions & 5 deletions evdev/uinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,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):
Expand Down
17 changes: 15 additions & 2 deletions tests/test_uinput.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# encoding: utf-8
import os
import stat
from select import select
from unittest.mock import patch
Expand Down Expand Up @@ -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)

0 comments on commit 83f9360

Please sign in to comment.