[Cython] Fix integer width constant names in stdint.pxd
Hello, Attached is a quick fix for some typos in stdint.pxd. Tested with Cython version 0.15.1. Mansour From a9b3caf05b98c07b779d0ecd7d0e3498de4f4f84 Mon Sep 17 00:00:00 2001 From: Mansour Moufid Date: Mon, 2 Jan 2012 19:01:42 -0500 Subject: [PATCH] Fix integer width constant names in stdint.pxd --- Cython/Includes/libc/stdint.pxd | 78 +++--- 1 files changed, 39 insertions(+), 39 deletions(-) diff --git a/Cython/Includes/libc/stdint.pxd b/Cython/Includes/libc/stdint.pxd index 21e6c33..206d468 100644 --- a/Cython/Includes/libc/stdint.pxd +++ b/Cython/Includes/libc/stdint.pxd @@ -41,52 +41,52 @@ cdef extern from "stdint.h" nogil: # 7.18.2 Limits of specified-width integer types # 7.18.2.1 Limits of exact-width integer types -enum: INT8_T_MIN -enum: INT16_T_MIN -enum: INT32_T_MIN -enum: INT64_T_MIN -enum: INT8_T_MAX -enum: INT16_T_MAX -enum: INT32_T_MAX -enum: INT64_T_MAX -enum: UINT8_T_MAX -enum: UINT16_T_MAX -enum: UINT32_T_MAX -enum: UINT64_T_MAX +enum: INT8_MIN +enum: INT16_MIN +enum: INT32_MIN +enum: INT64_MIN +enum: INT8_MAX +enum: INT16_MAX +enum: INT32_MAX +enum: INT64_MAX +enum: UINT8_MAX +enum: UINT16_MAX +enum: UINT32_MAX +enum: UINT64_MAX #7.18.2.2 Limits of minimum-width integer types -enum: INT_LEAST8_T_MIN -enum: INT_LEAST16_T_MIN -enum: INT_LEAST32_T_MIN -enum: INT_LEAST64_T_MIN -enum: INT_LEAST8_T_MAX -enum: INT_LEAST16_T_MAX -enum: INT_LEAST32_T_MAX -enum: INT_LEAST64_T_MAX -enum: UINT_LEAST8_T_MAX -enum: UINT_LEAST16_T_MAX -enum: UINT_LEAST32_T_MAX -enum: UINT_LEAST64_T_MAX +enum: INT_LEAST8_MIN +enum: INT_LEAST16_MIN +enum: INT_LEAST32_MIN +enum: INT_LEAST64_MIN +enum: INT_LEAST8_MAX +enum: INT_LEAST16_MAX +enum: INT_LEAST32_MAX +enum: INT_LEAST64_MAX +enum: UINT_LEAST8_MAX +enum: UINT_LEAST16_MAX +enum: UINT_LEAST32_MAX +enum: UINT_LEAST64_MAX #7.18.2.3 Limits of fastest minimum-width integer types -enum: INT_FAST8_T_MIN -enum: INT_FAST16_T_MIN -enum: INT_FAST32_T_MIN -enum: INT_FAST64_T_MIN -enum: INT_FAST8_T_MAX -enum: INT_FAST16_T_MAX -enum: INT_FAST32_T_MAX -enum: INT_FAST64_T_MAX -enum: UINT_FAST8_T_MAX -enum: UINT_FAST16_T_MAX -enum: UINT_FAST32_T_MAX -enum: UINT_FAST64_T_MAX +enum: INT_FAST8_MIN +enum: INT_FAST16_MIN +enum: INT_FAST32_MIN +enum: INT_FAST64_MIN +enum: INT_FAST8_MAX +enum: INT_FAST16_MAX +enum: INT_FAST32_MAX +enum: INT_FAST64_MAX +enum: UINT_FAST8_MAX +enum: UINT_FAST16_MAX +enum: UINT_FAST32_MAX +enum: UINT_FAST64_MAX #7.18.2.4 Limits of integer types capable of holding object pointers enum: INTPTR_MIN enum: INTPTR_MAX enum: UINTPTR_MAX # 7.18.2.5 Limits of greatest-width integer types -enum: INTMAX_T_MAX -enum: INTMAX_T_MIN -enum: UINTMAX_T_MAX +enum: INTMAX_MAX +enum: INTMAX_MIN +enum: UINTMAX_MAX # 7.18.3 Limits of other integer types # ptrdiff_t -- 1.7.5.4 ___ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel
Re: [Cython] Fix integer width constant names in stdint.pxd
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? Thanks, Mansour ___ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel
Re: [Cython] Fix integer width constant names in stdint.pxd
On Mon, Jan 2, 2012 at 8:48 PM, Lisandro Dalcin wrote: > On 2 January 2012 22:37, Mansour Moufid 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_MAX)" This works great. Exactly what I was looking for, thanks. Mansour ___ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel
[Cython] More typed constants (was: Fix integer width constant names in stdint.pxd)
Hello again, Attached is a patch that continues with the idea of declaring constants using their corresponding type. Great work on Cython, by the way. It's very useful. Mansour From 4f537f477d845468f36ac7b9370185124250520a Mon Sep 17 00:00:00 2001 From: Mansour Moufid Date: Fri, 20 Jan 2012 13:48:41 -0500 Subject: [PATCH] Continue defining constants using corresponding types. Mimic glibc when uncertain. For the new wchar.pxd: - wint_t is defined as typedef unsigned int in glibc. For stdint.pxd: - ptrdiff_t, wchar_t, and size_t are defined in stddef.pxd; - wint_t is defined in wchar.pxd; and - sig_atomic_t is defined in signal.pxd. For limits.pxd: - CHAR_BIT is always 8; and - MB_LEN_MAX is 16 in glibc. --- Cython/Includes/libc/limits.pxd | 36 ++-- Cython/Includes/libc/stdint.pxd | 32 ++-- Cython/Includes/libc/wchar.pxd |5 + 3 files changed, 41 insertions(+), 32 deletions(-) create mode 100644 Cython/Includes/libc/wchar.pxd diff --git a/Cython/Includes/libc/limits.pxd b/Cython/Includes/libc/limits.pxd index 3951dde..17cb640 100644 --- a/Cython/Includes/libc/limits.pxd +++ b/Cython/Includes/libc/limits.pxd @@ -1,29 +1,29 @@ # 5.2.4.2.1 Sizes of integer types -cdef extern from "limits.h": +cdef extern from "limits.h" nogil: enum: CHAR_BIT enum: MB_LEN_MAX -enum: CHAR_MIN -enum: CHAR_MAX +char CHAR_MIN +char CHAR_MAX -enum: SCHAR_MIN -enum: SCHAR_MAX -enum: UCHAR_MAX +signed char SCHAR_MIN +signed char SCHAR_MAX +unsigned char UCHAR_MAX -enum: SHRT_MIN -enum: SHRT_MAX -enum: USHRT_MAX +short SHRT_MIN +short SHRT_MAX +unsigned short USHRT_MAX -enum:INT_MIN -enum:INT_MAX -enum: UINT_MAX +int INT_MIN +int INT_MAX +unsigned int UINT_MAX -enum: LONG_MIN -enum: LONG_MAX -enum: ULONG_MAX +long int LONG_MIN +long int LONG_MAX +unsigned long int ULONG_MAX -enum: LLONG_MIN -enum: LLONG_MAX -enum: ULLONG_MAX +long long int LLONG_MIN +long long int LLONG_MAX +unsigned long long int ULLONG_MAX diff --git a/Cython/Includes/libc/stdint.pxd b/Cython/Includes/libc/stdint.pxd index 4d791a1..1237eb8 100644 --- a/Cython/Includes/libc/stdint.pxd +++ b/Cython/Includes/libc/stdint.pxd @@ -1,6 +1,10 @@ # Longness only used for type promotion. # Actual compile time size used for conversions. +from libc.stddef cimport ptrdiff_t, wchar_t, size_t +from libc.wchar cimport wint_t +from libc.signal cimport sig_atomic_t + # 7.18 Integer types cdef extern from "stdint.h" nogil: @@ -80,26 +84,26 @@ cdef extern from "stdint.h" nogil: uint_fast32_t UINT_FAST32_MAX uint_fast64_t UINT_FAST64_MAX #7.18.2.4 Limits of integer types capable of holding object pointers -enum: INTPTR_MIN -enum: INTPTR_MAX -enum: UINTPTR_MAX +intptr_t INTPTR_MIN +intptr_t INTPTR_MAX +uintptr_t UINTPTR_MAX # 7.18.2.5 Limits of greatest-width integer types -enum: INTMAX_MAX -enum: INTMAX_MIN -enum: UINTMAX_MAX +intmax_t INTMAX_MAX +intmax_t INTMAX_MIN +uintmax_t UINTMAX_MAX # 7.18.3 Limits of other integer types # ptrdiff_t -enum: PTRDIFF_MIN -enum: PTRDIFF_MAX +ptrdiff_t PTRDIFF_MIN +ptrdiff_t PTRDIFF_MAX # sig_atomic_t -enum: SIG_ATOMIC_MIN -enum: SIG_ATOMIC_MAX +sig_atomic_t SIG_ATOMIC_MIN +sig_atomic_t SIG_ATOMIC_MAX # size_t size_t SIZE_MAX # wchar_t -enum: WCHAR_MIN -enum: WCHAR_MAX +wchar_t WCHAR_MIN +wchar_t WCHAR_MAX # wint_t -enum: WINT_MIN -enum: WINT_MAX +wint_t WINT_MIN +wint_t WINT_MAX diff --git a/Cython/Includes/libc/wchar.pxd b/Cython/Includes/libc/wchar.pxd new file mode 100644 index 000..4c01390 --- /dev/null +++ b/Cython/Includes/libc/wchar.pxd @@ -0,0 +1,5 @@ +cdef extern from "wchar.h" nogil: + +ctypedef unsigned int wint_t + +wint_t WEOF -- 1.7.5.4 ___ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel