def ioctl_fn_ptr_r(request, datatype, return_python=None):
""" Create a helper function for invoking a ioctl() read call.
This function creates a helper function for creating a ioctl() read function.
It will call the ioctl() function with a pointer to data, and return the contents
of the data after the call.
If the datatype is a integer type (int, long, etc), it will be returned as a python int or long.
:param request: The ioctl request to call.
:param datatype: The data type of the data returned by the ioctl() call.
:param return_python: Whether we should attempt to convert the return data to a Python value. Defaults to True for fundamental ctypes data types.
:return: A function for invoking the specified ioctl().
:Example:
::
import ctypes
import os
import ioctl
import ioctl.linux
RNDGETENTCNT = ioctl.linux.IOR('R', 0x00, ctypes.c_int)
rndgetentcnt = ioctl.ioctl_fn_ptr_r(RNDGETENTCNT, ctypes.c_int)
fd = os.open('/dev/random', os.O_RDONLY)
entropy_avail = rndgetentcnt(fd)
"""
check_request(request)
check_ctypes_datatype(datatype)
if return_python is not None and not isinstance(return_python, bool):
raise TypeError('return_python must be None or a boolean, but was {}'.format(return_python.__class__.__name__))
if return_python is None:
return_python = issubclass(datatype, ctypes._SimpleCData)
def fn(fd):
check_fd(fd)
value = datatype()
ioctl(fd, request, ctypes.byref(value))
if return_python:
return value.value
else:
return value
return fn
评论列表
文章目录