Commit 1f07ff33 authored by scoder's avatar scoder

Merge pull request #377 from cython/neg-overflow

PyPy: Improve negative overflow error messages in int type conversion
parents 60f0e2f9 0407171e
......@@ -560,6 +560,15 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
if (unlikely(Py_SIZE(x) < 0)) {
goto raise_neg_overflow;
}
#endif
#if CYTHON_COMPILING_IN_PYPY
{
int result = PyObject_RichCompareBool(x, Py_False, Py_LT);
if (unlikely(result < 0))
return -1;
if (unlikely(result == 1))
goto raise_neg_overflow;
}
#endif
if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
__PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, PyLong_AsUnsignedLong(x))
......
......@@ -22,7 +22,7 @@ def test_schar(signed char x):
>>> test_schar(128) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to signed char
"""
return x
......@@ -43,7 +43,7 @@ def test_add_schar(x, y):
>>> test_add_schar(SCHAR_MAX, 1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to signed char
"""
cdef signed char r = x + y
return r
......@@ -55,7 +55,7 @@ def test_uchar(unsigned char x):
>>> test_uchar(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to unsigned char
>>> test_uchar(0)
0
>>> test_uchar(1)
......@@ -65,7 +65,7 @@ def test_uchar(unsigned char x):
>>> test_uchar(UCHAR_MAX+1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to unsigned char
"""
return x
......@@ -76,7 +76,7 @@ def test_add_uchar(x, y):
>>> test_add_uchar(UCHAR_MAX, 1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to unsigned char
"""
cdef unsigned char r = x + y
return r
......@@ -95,7 +95,7 @@ def test_char(char x):
Traceback (most recent call last):
...
OverflowError: ...
>>> if CHAR_MIN < 0:
>>> if CHAR_MIN < 0:
... assert test_char(-1) == -1
>>> test_char(CHAR_MIN) == CHAR_MIN
True
......@@ -108,7 +108,7 @@ def test_char(char x):
>>> test_char(CHAR_MAX+1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to char
"""
return x
......@@ -129,7 +129,7 @@ def test_add_char(x, y):
>>> test_add_char(CHAR_MAX, 1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to char
"""
cdef char r = x + y
return r
......@@ -144,7 +144,7 @@ def test_short(short x):
>>> test_short(SHORT_MIN-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to short
>>> test_short(SHORT_MIN) == SHORT_MIN
True
>>> test_short(-1)
......@@ -158,7 +158,7 @@ def test_short(short x):
>>> test_short(SHORT_MAX+1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to short
"""
return x
......@@ -167,7 +167,7 @@ def test_add_short(x, y):
>>> test_add_short(SHORT_MIN, -1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to short
>>> test_add_short(SHORT_MIN, 0) == SHORT_MIN
True
>>> test_add_short(SHORT_MIN, 1) == SHORT_MIN+1
......@@ -179,7 +179,7 @@ def test_add_short(x, y):
>>> test_add_short(SHORT_MAX, 1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to short
"""
cdef short r = x + y
return r
......@@ -192,7 +192,7 @@ def test_sshort(short x):
>>> test_sshort(SSHORT_MIN-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to short
>>> test_sshort(SSHORT_MIN) == SSHORT_MIN
True
>>> test_sshort(-1)
......@@ -206,7 +206,7 @@ def test_sshort(short x):
>>> test_short(SSHORT_MAX+1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to short
"""
return x
......@@ -215,7 +215,7 @@ def test_add_sshort(x, y):
>>> test_add_sshort(SSHORT_MIN, -1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to signed short
>>> test_add_sshort(SSHORT_MIN, 0) == SSHORT_MIN
True
>>> test_add_sshort(SSHORT_MIN, 1) == SSHORT_MIN+1
......@@ -227,7 +227,7 @@ def test_add_sshort(x, y):
>>> test_add_sshort(SSHORT_MAX, 1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to signed short
"""
cdef signed short r = x + y
return r
......@@ -239,7 +239,7 @@ def test_ushort(unsigned short x):
>>> test_ushort(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to unsigned short
>>> test_ushort(0)
0
>>> test_ushort(1)
......@@ -260,7 +260,7 @@ def test_add_ushort(x, y):
>>> test_add_ushort(USHORT_MAX, 1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to unsigned short
"""
cdef unsigned short r = x + y
return r
......@@ -318,7 +318,7 @@ def test_add_int(x, y):
SINT_MAX = <signed int>((<unsigned int>-1)>>1)
SINT_MIN = (-SINT_MAX-1)
def test_sint(int x):
def test_sint(signed int x):
u"""
>>> test_sint(SINT_MIN-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
......@@ -370,7 +370,7 @@ def test_uint(unsigned int x):
>>> test_uint(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to unsigned int
>>> print(test_uint(0))
0
>>> print(test_uint(1))
......@@ -449,7 +449,7 @@ def test_add_long(x, y):
SLONG_MAX = <signed long>((<unsigned long>-1)>>1)
SLONG_MIN = (-SLONG_MAX-1)
def test_slong(long x):
def test_slong(signed long x):
u"""
>>> test_slong(SLONG_MIN-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
......@@ -501,7 +501,7 @@ def test_ulong(unsigned long x):
>>> test_ulong(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to unsigned long
>>> print(test_ulong(0))
0
>>> print(test_ulong(1))
......@@ -632,7 +632,7 @@ def test_ulonglong(unsigned long long x):
>>> test_ulonglong(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to unsigned PY_LONG_LONG
>>> print(test_ulonglong(0))
0
>>> print(test_ulonglong(1))
......
......@@ -25,7 +25,7 @@ def test_schar(SChar x):
>>> test_schar(-129) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to SChar
>>> test_schar(-128)
-128
>>> test_schar(0)
......@@ -35,7 +35,7 @@ def test_schar(SChar x):
>>> test_schar(128) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to SChar
"""
return x
......@@ -44,7 +44,7 @@ def test_add_schar(x, y):
>>> test_add_schar(SCHAR_MIN, -1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to SChar
>>> test_add_schar(SCHAR_MIN, 0) == SCHAR_MIN
True
>>> test_add_schar(SCHAR_MIN, 1) == SCHAR_MIN+1
......@@ -56,7 +56,7 @@ def test_add_schar(x, y):
>>> test_add_schar(SCHAR_MAX, 1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to SChar
"""
cdef SChar r = x + y
return r
......@@ -68,7 +68,7 @@ def test_uchar(UChar x):
>>> test_uchar(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to UChar
>>> test_uchar(0)
0
>>> test_uchar(1)
......@@ -78,7 +78,7 @@ def test_uchar(UChar x):
>>> test_uchar(UCHAR_MAX+1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to UChar
"""
return x
......@@ -89,7 +89,7 @@ def test_add_uchar(x, y):
>>> test_add_uchar(UCHAR_MAX, 1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to UChar
"""
cdef UChar r = x + y
return r
......@@ -99,12 +99,12 @@ def test_add_uchar(x, y):
SSHORT_MAX = <SShort>((<UShort>-1)>>1)
SSHORT_MIN = (-SSHORT_MAX-1)
def test_sshort(short x):
def test_sshort(SShort x):
u"""
>>> test_sshort(SSHORT_MIN-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to SShort
>>> test_sshort(SSHORT_MIN) == SSHORT_MIN
True
>>> test_sshort(-1)
......@@ -127,7 +127,7 @@ def test_add_sshort(x, y):
>>> test_add_sshort(SSHORT_MIN, -1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to SShort
>>> test_add_sshort(SSHORT_MIN, 0) == SSHORT_MIN
True
>>> test_add_sshort(SSHORT_MIN, 1) == SSHORT_MIN+1
......@@ -139,7 +139,7 @@ def test_add_sshort(x, y):
>>> test_add_sshort(SSHORT_MAX, 1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to SShort
"""
cdef SShort r = x + y
return r
......@@ -151,7 +151,7 @@ def test_ushort(UShort x):
>>> test_ushort(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to UShort
>>> test_ushort(0)
0
>>> test_ushort(1)
......@@ -161,7 +161,7 @@ def test_ushort(UShort x):
>>> test_ushort(USHORT_MAX+1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to UShort
"""
return x
......@@ -172,7 +172,7 @@ def test_add_ushort(x, y):
>>> test_add_ushort(USHORT_MAX, 1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: value too large to convert to UShort
"""
cdef UShort r = x + y
return r
......@@ -182,7 +182,7 @@ def test_add_ushort(x, y):
SINT_MAX = <SInt>((<UInt>-1)>>1)
SINT_MIN = (-SINT_MAX-1)
def test_sint(int x):
def test_sint(SInt x):
u"""
>>> test_sint(SINT_MIN-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
......@@ -234,7 +234,7 @@ def test_uint(UInt x):
>>> test_uint(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to UInt
>>> print(test_uint(0))
0
>>> print(test_uint(1))
......@@ -317,7 +317,7 @@ def test_ulong(ULong x):
>>> test_ulong(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to ULong
>>> print(test_ulong(0))
0
>>> print(test_ulong(1))
......@@ -400,7 +400,7 @@ def test_ulonglong(ULongLong x):
>>> test_ulonglong(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to ULongLong
>>> print(test_ulonglong(0))
0
>>> print(test_ulonglong(1))
......@@ -554,7 +554,7 @@ def test_MyUInt2(MyUInt2 x):
>>> test_MyUInt2(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to ...
>>> test_MyUInt2(0)
0
>>> test_MyUInt2(1)
......@@ -582,7 +582,7 @@ def test_DefUChar(defs.UChar x):
>>> test_DefUChar(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to ...
>>> test_DefUChar(0)
0
>>> test_DefUChar(1)
......@@ -606,7 +606,7 @@ def test_ExtUInt(defs.ExtUInt x):
>>> test_ExtUInt(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to ...
>>> test_ExtUInt(0)
0
>>> test_ExtUInt(1)
......@@ -634,7 +634,7 @@ def test_LocUInt(LocUInt x):
>>> test_LocUInt(-1) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
OverflowError: can't convert negative value to ...
>>> test_LocUInt(0)
0
>>> test_LocUInt(1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment