How to use C enum in Python CTypes?

Jim Langston tazmaster at rocketmail.com
Tue Aug 7 07:28:22 EDT 2007


"rozniy" <rozniy at gmail.com> wrote in message 
news:1186478018.409193.188130 at e9g2000prf.googlegroups.com...
> On Aug 7, 2:11 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
>> On Tue, 07 Aug 2007 04:57:19 +0000, rozniy wrote:
>> > typedef enum olss_tag
>> >    {
>> >    OLSS_AD,
>> >    OLSS_DA,
>> >    OLSS_DIN,
>> >    OLSS_DOUT,
>> >    OLSS_SRL,
>> >    OLSS_CT
>> >    }
>> > OLSS;
>>
>> > I managed to fudge the HDEV, UINT and PHDASS types as CTypes c_long(),
>> > but I am not sure how translate a C enum into Python...
>>
>> > This site
>> >http://python.net/crew/theller/ctypes/tutorial.html#bugs-todo-and-non...
>>
>> > says that enumeration types is not implemented,
>> > "Enumeration types are not implemented. You can do it easily yourself,
>> > using c_int as the base class."
>>
>> I would just define constants:
>>
>> (OLSS_AD,
>>  OLSS_DA,
>>  OLSS_DIN,
>>  OLSS_DOUT,
>>  OLSS_SRL,
>>  OLSS_CT) = map(ctypes.c_int, xrange(6))
>>
>> Ciao,
>>         Marc 'BlackJack' Rintsch- Hide quoted text -
>>
>> - Show quoted text -
>
>
> Wouldn't that assign integer values 0 to 5 to the things? I don't know
> if it'll give me the correct results.

Yes, that's how C's and C++'s enums work, unless an override is given (which 
I know you can do in C++ for sure).  Otherwise, they are just numbers 
starting at 0.  The size of the intergers (byte, 2 bytes, 4 bytes, etc..) is 
implemenation defined I believe, but an int normally works. 





More information about the Python-list mailing list