有没有办法确保我所有的 ctypes 都有 argtypes?

Is there a way to ensure that all my ctypes have argtypes?

我知道我应该为我的 C/C++ 函数指定 argtypes,因为我的某些调用否则会导致堆栈损坏。

    myCfunc.argtypes = [ct.c_void_p, ct.POINTER(ct.c_void_p)]
    myCfunc.errcheck = my_error_check

事实上,我想验证我没有忘记为我大约 100 个函数调用中的任何一个指定函数原型 (argtypes/errcheck)...

现在我只是 grep 浏览我的 Python 文件,并在视觉上与包含原型定义的文件进行比较。

是否有更好的方法来验证我是否为我的所有调用定义了 argtypes/errcheck

这是一个虚拟 class 可以通过简单的检查来替换 DLL 对象的函数调用以查看属性是否已定义:

class DummyFuncPtr(object):
    restype = False
    argtypes = False
    errcheck = False

    def __call__(self, *args, **kwargs):
        assert self.restype
        assert self.argtypes
        assert self.errcheck

    def __init__(self, *args):
        pass

    def __setattr__(self, key, value):
        super(DummyFuncPtr, self).__setattr__(key, True)

要使用它替换你的 DLL 对象的 _FuncPtr class 然后调用每个函数来 运行 检查,例如:

dll = ctypes.cdll.LoadLibrary(r'path/to/dll')

# replace the DLL's function pointer
# comment out this line to disable the dummy class
dll._FuncPtr = DummyFuncPtr

some_func = dll.someFunc
some_func.restype = None
some_func.argtypes = None
some_func.errcheck = None

another_func = dll.anotherFunc
another_func.restype = None
another_func.argtypes = None

some_func()     # no error
another_func()  # Assertion error due to errcheck not defined

虚拟 class 当然会完全阻止函数被调用,所以只需注释掉替换行即可切换回正常操作。

请注意,它只会在每个函数被调用时检查该函数,因此最好将其放在保证调用该函数的某个地方的单元测试文件中。

@eryksun 提到的名称空间让我将 dll 包装在 class 中,它只公开显式注释的函数。只要 dll 没有函数名称 "annotate" 或“_error_check”(我没有),以下方法似乎对我有用:

import ctypes as ct

class MyWinDll:
    def __init__(self, dll_filename):
        self._dll = ct.WinDLL(dll_filename)
        # Specify function prototypes using the annotate function
        self.annotate(self._dll.myCfunc, [ct.POINTER(ct.c_void_p)], self._error_check)
        self.annotate(self._dll.myCfunc2, [ct.c_void_p], self._error_check)
        ...

    def annotate(self, function, argtypes, errcheck):
        # note that "annotate" may not be used as a function name in the dll...
        function.argtypes = argtypes
        function.errcheck = errcheck
        setattr(self, function.__name__, function)

    def _error_check(self, result, func, arguments):
        if result != 0:
            raise Exception

if __name__ == '__main__':
    dll = MyWinDll('myWinDll.dll')
    handle = ct.c_void_p(None)
    # Now call the dll functions using the wrapper object
    dll.myCfunc(ct.byref(handle))
    dll.myCfunc2(handle)

更新: @eryksun 的评论让我尝试通过让用户控制 WinDLL 构造函数并尝试减少重复代码来改进代码:

import ctypes as ct

DEFAULT = object()

def annotate(dll_object, function_name, argtypes, restype=DEFAULT, errcheck=DEFAULT):
    function = getattr(dll_object._dll, function_name)
    function.argtypes = argtypes
    # restype and errcheck is optional in the function_prototypes list
    if restype is DEFAULT:
        restype = dll_object.default_restype
    function.restype = restype
    if errcheck is DEFAULT:
        errcheck = dll_object.default_errcheck
    function.errcheck = errcheck
    setattr(dll_object, function_name, function)


class MyDll:
    def __init__(self, ct_dll, **function_prototypes):
        self._dll = ct_dll
        for name, prototype in function_prototypes.items():
            annotate(self, name, *prototype)


class OneDll(MyDll):
    def __init__(self, ct_dll):
        # set default values for function_prototypes
        self.default_restype = ct.c_int
        self.default_errcheck = self._error_check
        function_prototypes = {
            'myCfunc': [[ct.POINTER(ct.c_void_p)]],
            'myCfunc2': [[ct.c_void_p]],
            # ...
            'myCgetErrTxt': [[ct.c_int, ct.c_char_p, ct.c_size_t], DEFAULT, None]
        }
        super().__init__(ct_dll, **function_prototypes)

    # My error check function actually calls the dll, so I keep it here...
    def _error_check(self, result, func, arguments):
        msg = ct.create_string_buffer(255)
        if result != 0:
            raise Exception(self.myCgetErrTxt(result, msg, ct.sizeof(msg)))


if __name__ == '__main__':
    ct_dll = ct.WinDLL('myWinDll.dll')
    dll = OneDll(ct_dll)
    handle = ct.c_void_p(None)
    dll.myCfunc(ct.byref(handle))
    dll.myCfunc2(handle)

(不知道要不要删掉原码,留着参考)