[Pyrex] enum regression 0.9.5?

Lenard Lindstrom len-l at telus.net
Mon Jan 29 17:42:27 UTC 2007


Anders Gustafsson wrote:
> Greg Ewing wrote:
>   
>> That looks like it'll make any two enum types compatible,
>> which doesn't sound right. I'll look into this.
>>     
>
> I'm not a C++ expert. But I think the enums are implicitly converted to
> ints making it legal C++. Maybe it might be a good idea to allow pyrex
> to be stricter in this case, as comparing variables of two different
> enums is most probably is a bug.
>
>
> But I think that it might be a good idea to allow comparation of ints
> and enums.
>
> With:
> cdef myenum_t y
>
> I think these should be valid:
> if y == 10
> if y < 10
>
>
> And I would definitly expect this to work:
>
> cdef extern from "whatever.h":
>         ctypedef enum enum_t:
>                 ENUM_VAL1
>                 ENUM_VAL2
>                 ENUM_END
>
> def x(int i):
>         if i >= ENUM_END:
>                 raise
>         do_stuff(i)
>
>   
C++ is more strict that C in regards to enums. There is no guaranty that 
a C++ enum is an int or even that different enum types have the same 
size. So to allow int/enum comparisons in Pyrex means adding a C cast to 
the generated code. Though C++ allows it, the behaviour is likely 
undefined. Maybe an explicit type cast in the Pyrex code will work when 
the conversion is known to be safe.

Try:

        if i >= <int> ENUM_END:
            raise
        do_stuff(<enum_t> i)

-- 
Lenard Lindstrom
<len-l at telus.net>





More information about the Pyrex mailing list