[Pyrex] Funny behaviour with unsigned int struct members (unwanted conversion to signed int...)

Heiko Wundram me+pyrex at modelnine.org
Mon Apr 3 17:15:43 CEST 2006


Hey all!

This is my first post to the Pyrex Mailinglist, so please be gentle with me, 
but I've found some interesting behaviour with respect to unsigned ints 
defined in unions or structs:

My union definition:

    ctypedef union value_t:
        char c
        char *s
        unsigned char uc
        unsigned char b
        int i
        int i8
        int i16
        int i32
        long long i64
        unsigned int ui
        unsigned int ui8
        unsigned int ui16
        unsigned int ui32
        unsigned long long ui64
        float r32
        double r64
        value_list_t *list

My access to the members:

cdef get_extended_info_convert_value(value_type_t type, value_t value):
    if type == EVMS_Type_String:
        return value.s
    elif type == EVMS_Type_Boolean:
        return bool(value.b)
    elif type == EVMS_Type_Char:
        return value.c
    elif type == EVMS_Type_Unsigned_Char:
        return value.uc
    elif type == EVMS_Type_Real32:
        return value.r32
    elif type == EVMS_Type_Real64:
        return value.r64
    elif type == EVMS_Type_Int:
        return value.i
    elif type == EVMS_Type_Int8:
        return value.i8
    elif type == EVMS_Type_Int16:
        return value.i16
    elif type == EVMS_Type_Int32:
        return value.i32
    elif type == EVMS_Type_Int64:
        return value.i64
    elif type == EVMS_Type_Unsigned_Int:
        return value.ui
    elif type == EVMS_Type_Unsigned_Int8:
        return value.ui8
    elif type == EVMS_Type_Unsigned_Int16:
        return value.ui16
    elif type == EVMS_Type_Unsigned_Int32:
        return value.ui32
    elif type == EVMS_Type_Unsigned_Int64:
        return value.ui64
    else:
        raise RuntimeError("Invalid value type")

And, finally, the generated code for one of them:

...
    /* "/home/modelnine/evms-python-pyrex/libevms.pyx":604 */
    __pyx_2 = PyInt_FromLong(__pyx_v_value.ui); if (!__pyx_2) {__pyx_filename 
=
__pyx_f[0]; __pyx_lineno = 604; goto __pyx_L1;}
    __pyx_r = __pyx_2;
    __pyx_2 = 0;
    goto __pyx_L0;
    goto __pyx_L2;
  }
...

This should actually create a PyLong_FromUnsignedLong() (at least on 32-bit 
platforms), as an unsigned int doesn't fit in a Python integer object there, 
AFAICT, but as you can see above, doesn't. Same thing goes for the 
member "unsigned int ui32" and it's switch case.

Thanks for any enlightenment on what I'm doing wrong, or whether this is a 
Pyrex bug...

--- Heiko.



More information about the Pyrex mailing list