[Python-Dev] a small C style question

Tim Peters tim.one@home.com
Sun, 4 Mar 2001 01:50:58 -0500


[Fredrik Lundh]
> DEC's OpenVMS compiler are a bit pickier than most other compilers.
> among other things, it correctly notices that the "code" variable in
> this statement is an unsigned variable:
>
>     UNICODEDATA:
>
>         if (code < 0 || code >= 65536)
>     ........^
>     %CC-I-QUESTCOMPARE, In this statement, the unsigned
>     expression "code" is being compared with a relational
>     operator to a constant whose value is not greater than
>     zero.  This might not be what you intended.
>     at line number 285 in file UNICODEDATA.C
>
> the easiest solution would of course be to remove the "code < 0"
> part, but code is a Py_UCS4 variable.  what if someone some day
> changes Py_UCS4 to a 64-bit signed integer, for example?
>
> what's the preferred style?
>
> 1) leave it as is, and let OpenVMS folks live with the
> compiler complaint
>
> 2) get rid of "code < 0" and hope that nobody messes
> up the Py_UCS4 declaration
>
> 3) cast "code" to a known unsigned type, e.g:
>
>         if ((unsigned int) code >= 65536)

#2.  The comment at the declaration of Py_UCS4 insists that an unsigned type
be used:

/*
 * Use this typedef when you need to represent a UTF-16 surrogate pair
 * as single unsigned integer.
             ^^^^^^^^
 */
#if SIZEOF_INT >= 4
typedef unsigned int Py_UCS4;
#elif SIZEOF_LONG >= 4
typedef unsigned long Py_UCS4;
#endif

If someone needs to boost that to a 64-bit int someday (hard to imagine ...),
they can boost it to an unsigned 64-bit int just as well.

If you really need to cater to impossibilities <0.5 wink>, #define a
Py_UCS4_IN_RANGE macro next to the typedef, and use the macro instead.