On 2 January 2012 23:00, Robert Bradshaw <rober...@math.washington.edu> wrote: > On Mon, Jan 2, 2012 at 5:48 PM, Lisandro Dalcin <dalc...@gmail.com> wrote: >> On 2 January 2012 22:37, Mansour Moufid <mansourmou...@gmail.com> wrote: >>> Now my issue is as follows. >>> >>> (I CCed the cython-users list if this question is more appropriate there.) >>> >>> I have a simple file, int.pyx: >>> >>> from libc.stdint cimport * >>> print long(UINT8_MAX) >>> print long(UINT16_MAX) >>> print long(UINT32_MAX) >>> print long(UINT64_MAX) >>> >>> with the usual setup.py stuff. Compiling and running: >>> >>> $ python setup.py build_ext --inplace >>> ... >>> int.c:566:3: warning: overflow in implicit constant conversion [-Woverflow] >>> ... >>> $ python -c 'import int' >>> 255 >>> 65535 >>> -1 >>> -1 >>> >>> So obviously there are overflows here. Checking int.c, I see: >>> >>> /* "int.pyx":2 >>> * from libc.stdint cimport * >>> * print long(UINT8_MAX) # <<<<<<<<<<<<<< >>> * print long(UINT16_MAX) >>> * print long(UINT32_MAX) >>> */ >>> __pyx_t_1 = PyInt_FromLong(UINT8_MAX); >>> >>> and so on... >>> >>> PyInt_FromLong is used for all these constants, regardless of >>> signedness or width, so any argument larger than LONG_MAX overflows, >>> *before* being converted to the arbitrary-size Python integer type. >>> >>> I don't know if this is a bug, or if I'm overlooking something. Is >>> there a way for me to use these constants with Python's arbitrary-size >>> integers? >>> >> >> All these constants are declared as "enum", so Cython promotes them to >> "int". Once again, Cython should have something like a "const" type >> qualifier to poperly declare these compile-time constants. >> >> As workaround, you could explicitly cast the constants like this >> "print long(<uint8_t>UINT8_MAX)" > > I'm leaning towards declaring them as being the proper type to begin > with; what's to be gained by declaring these extern values as enums > (=const)? At least with the larger types we should do this to avoid > patently incorrect behavior, and this way they would be consistant > with the actual C for arithmetic promotion, etc. >
Not sure about recent Cython releases, but in older ones you cannot do: cdef char buf[UINT8_MAX] unless UINT8_MAX was declared in Cython as a compile time constant. However, I do agree that for the case of stdint.h, using matching types instead of "enum" is way better. -- Lisandro Dalcin --------------- CIMEC (INTEC/CONICET-UNL) Predio CONICET-Santa Fe Colectora RN 168 Km 472, Paraje El Pozo 3000 Santa Fe, Argentina Tel: +54-342-4511594 (ext 1011) Tel/Fax: +54-342-4511169 _______________________________________________ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel