如何使用ctypes和基于ctypes的枚举正确调用一个以 "自定义枚举 "为参数的函数?

问题描述 投票:1回答:1

我真的希望有PythonCtypesC专家能帮我解决这个问题,可能是我在使用Python与C库交互时,缺乏正确使用Ctypes的类型结构知识。

目的是。 我需要使用 ctypes 来访问一些加载 DLL 的库函数,并与之交互。这个想法在大多数时候都能正常工作,但有少数函数是以枚举作为参数的,而这些枚举在涉及到int类型时非常敏感。下面是一个假的例子。

typedef enum led_property : uint8_t {
  LED_OFF = 0
  LED_POWER
}

int32_t configure_led(const led_property, const int32_t value)

这就是函数所接收的枚举类型,不仅有uint8_t,还有int32_t,int_64t等等。

根据我在网上找到的一个python配方,我设法用ctypes类型来 "适应 "python枚举。

class EnumerationTypeUInt8(type(c_uint8)):
def __new__(metacls, name, bases, dict):
    if not "_members_" in dict:
        _members_ = {}
        for key, value in dict.items():
            if not key.startswith("_"):
                _members_[key] = value
        dict["_members_"] = _members_
    cls = type(c_uint8).__new__(metacls, name, bases, dict)
    for key, value in cls._members_.items():
        globals()[key] = value
    return cls

def __contains__(self, value):
    return value in self._members_.values()

def __repr__(self):
    return "<Enumeration {}>".format(self.__name__)

def EnumerationUInt8(c_uint8):
__metaclass__ = EnumerationTypeUInt8
_members_ = {}

def __init__(self, value):
    for k, v in self._members_.items():
        if v == value:
            self.name = k
            break
    else:
        raise ValueError("No enumeration member with value {}".format(value))
    c_uint8.__init__(self, value)

@classmethod
def from_param(cls, param):
    if isinstance(param, EnumerationUInt8):
        if param.__class__ != cls:
            raise ValueError("Can not mix enumeration members")
        else:
            return param
    else:
        return cls(param)

def __repr__(self):
    return "<member {}={} of {}".format(self.name, self.value, self.__class__)

我加载了这个库,并对它的函数进行了如下装饰。

class LedProperty(EnumerationUInt8):
   LED_OFF = c_uint8(0)
   LED_POWER = c_uint8(1)

lib = "library.dll"
self._lib = CDLL(lib)
configure_led = self._lib.configure_led
configure_led.argtypes = [LedProperty, c_int32]
configre_led.restype = c_int32

问题是我试过了所有能试的方法 但我从来没有正确调用过那个configure_led python函数 大多数时候我都会得到以下错误信息:

ctypes.ArgumentError class 'ValueError' No enumeration member with value c_ubyte(1)
or
ctypes.ArgumentError class 'ValueError' No enumeration member with value 1

发生这种情况是因为我在调试时可以看到 "EnumerationUInt8" "self.成员. items() "总是一个空的dict。所以可能是这个带有ctypes的自定义枚举没有正确加载它们的成员。我总是以 "else: return cls(param) "结束。

试过了......等等!

configure_led(LedProperty.LED_POWER, 5)
configure_led(LedProperty.LED_POWER.value, 5)
configure_led(c_uint8(LedProperty.LED_POWER), 5)

...等等!似乎没有什么是正确的。

有谁知道如何使用cytpes类型正确声明Enums,并在之后使用这些enums作为函数的参数?

观察..: 我现在用的是 Python 3.8.3先谢谢你了!

python types enums ctypes
1个回答
2
投票

假设这样实现,test.cpp。

#include <stdint.h>

enum led_property : uint8_t {
    LED_OFF = 0,
    LED_POWER
};

extern "C" __declspec(dllexport) int32_t configure_led(enum led_property prop, int32_t value) {
    return prop * value;
}

这将使第一个参数只允许有LED值。

from ctypes import *
from enum import Enum,auto

class LED(Enum):

    OFF = 0
    POWER = auto()  # autoincrement from last value

    @classmethod
    def from_param(cls,obj):
        if not isinstance(obj,LED):
            raise TypeError('not an LED enumeration')
        return c_int8(obj.value)

dll = CDLL('./test')
dll.configure_led.argtypes = LED,c_int32
dll.configure_led.restype = c_int32

print(dll.configure_led(LED.OFF,5))   # prints 0
print(dll.configure_led(LED.POWER,5)) # prints 5
print(dll.configure_led(0,5))         # not an LED enumeration
© www.soinside.com 2019 - 2024. All rights reserved.