How Do I Correctly Call A Function That Takes A "custom Enum" As Argument Using Ctypes And Ctypes Based Enums?
I really hope some Python/Ctypes/C expert can help me with this one, it is probably my lack of knowledge on the typing structure to correctly use Ctypes when using Python to intera
Solution 1:
Assuming this implementation, test.cpp:
#include<stdint.h>enumled_property : uint8_t {
LED_OFF = 0,
LED_POWER
};
extern"C" __declspec(dllexport) int32_tconfigure_led(enum led_property prop, int32_t value){
return prop * value;
}
This will allow only LED values for the first parameter:
from ctypes import *
from enum import Enum,auto
classLED(Enum):
OFF = 0
POWER = auto() # autoincrement from last value @classmethoddeffrom_param(cls,obj):
ifnotisinstance(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 0print(dll.configure_led(LED.POWER,5)) # prints 5print(dll.configure_led(0,5)) # not an LED enumeration
Post a Comment for "How Do I Correctly Call A Function That Takes A "custom Enum" As Argument Using Ctypes And Ctypes Based Enums?"