Commit b5dcb180 authored by Simon Feltman's avatar Simon Feltman
Browse files

Use Python error messages for marshaling integers from Python

Use Pythons default error messages where possible.
Change all explicitly raised ValueError to use OverflowError
for out of range data.

https://bugzilla.gnome.org/show_bug.cgi?id=705057
parent 3c83bfb1
......@@ -286,7 +286,7 @@ check_valid_double (double x, double min, double max)
/* we need this as PyErr_Format() does not support float types */
snprintf (buf, sizeof (buf), "%g not in range %g to %g", x, min, max);
PyErr_SetString (PyExc_ValueError, buf);
PyErr_SetString (PyExc_OverflowError, buf);
return FALSE;
}
return TRUE;
......@@ -524,8 +524,10 @@ _pygi_marshal_from_py_long (PyObject *object, /* in */
case GI_TYPE_TAG_INT8:
{
long long_value = PyLong_AsLong (number);
if (long_value < G_MININT8 || long_value > G_MAXINT8) {
PyErr_Format (PyExc_ValueError, "%ld not in range %ld to %ld",
if (PyErr_Occurred()) {
break;
} else if (long_value < G_MININT8 || long_value > G_MAXINT8) {
PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
long_value, (long)G_MININT8, (long)G_MAXINT8);
} else {
arg->v_int8 = long_value;
......@@ -536,8 +538,10 @@ _pygi_marshal_from_py_long (PyObject *object, /* in */
case GI_TYPE_TAG_UINT8:
{
long long_value = PyLong_AsLong (number);
if (long_value < 0 || long_value > G_MAXUINT8) {
PyErr_Format (PyExc_ValueError, "%ld not in range %ld to %ld",
if (PyErr_Occurred()) {
break;
} else if (long_value < 0 || long_value > G_MAXUINT8) {
PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
long_value, (long)0, (long)G_MAXUINT8);
} else {
arg->v_uint8 = long_value;
......@@ -548,8 +552,10 @@ _pygi_marshal_from_py_long (PyObject *object, /* in */
case GI_TYPE_TAG_INT16:
{
long long_value = PyLong_AsLong (number);
if (long_value < G_MININT16 || long_value > G_MAXINT16) {
PyErr_Format (PyExc_ValueError, "%ld not in range %ld to %ld",
if (PyErr_Occurred()) {
break;
} else if (long_value < G_MININT16 || long_value > G_MAXINT16) {
PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
long_value, (long)G_MININT16, (long)G_MAXINT16);
} else {
arg->v_int16 = long_value;
......@@ -560,8 +566,10 @@ _pygi_marshal_from_py_long (PyObject *object, /* in */
case GI_TYPE_TAG_UINT16:
{
long long_value = PyLong_AsLong (number);
if (long_value < 0 || long_value > G_MAXUINT16) {
PyErr_Format (PyExc_ValueError, "%ld not in range %ld to %ld",
if (PyErr_Occurred()) {
break;
} else if (long_value < 0 || long_value > G_MAXUINT16) {
PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
long_value, (long)0, (long)G_MAXUINT16);
} else {
arg->v_uint16 = long_value;
......@@ -572,8 +580,10 @@ _pygi_marshal_from_py_long (PyObject *object, /* in */
case GI_TYPE_TAG_INT32:
{
long long_value = PyLong_AsLong (number);
if (long_value < G_MININT32 || long_value > G_MAXINT32) {
PyErr_Format (PyExc_ValueError, "%ld not in range %ld to %ld",
if (PyErr_Occurred()) {
break;
} else if (long_value < G_MININT32 || long_value > G_MAXINT32) {
PyErr_Format (PyExc_OverflowError, "%ld not in range %ld to %ld",
long_value, (long)G_MININT32, (long)G_MAXINT32);
} else {
arg->v_int32 = long_value;
......@@ -584,8 +594,10 @@ _pygi_marshal_from_py_long (PyObject *object, /* in */
case GI_TYPE_TAG_UINT32:
{
PY_LONG_LONG long_value = PyLong_AsLongLong (number);
if (long_value < 0 || long_value > G_MAXUINT32) {
PyErr_Format (PyExc_ValueError, "%lld not in range %ld to %lu",
if (PyErr_Occurred()) {
break;
} else if (long_value < 0 || long_value > G_MAXUINT32) {
PyErr_Format (PyExc_OverflowError, "%lld not in range %ld to %lu",
long_value, (long)0, (unsigned long)G_MAXUINT32);
} else {
arg->v_uint32 = long_value;
......@@ -596,48 +608,14 @@ _pygi_marshal_from_py_long (PyObject *object, /* in */
case GI_TYPE_TAG_INT64:
{
/* Rely on Python overflow error and convert to ValueError for 64 bit values */
gint64 value = PyLong_AsLongLong (number);
if (PyErr_Occurred()) {
if (PyErr_ExceptionMatches (PyExc_OverflowError)) {
PyErr_Clear();
#if PY_MAJOR_VERSION >= 3
PyErr_Format (PyExc_ValueError, "%S not in range %lld to %lld",
number, PY_LLONG_MIN, PY_LLONG_MAX);
#else
/* Python 2.x does not support the %S format option and it is not
* worth the trouble to support this manually.
*/
PyErr_Format (PyExc_ValueError, "number not in range %lld to %lld",
PY_LLONG_MIN, PY_LLONG_MAX);
#endif
}
} else {
arg->v_int64 = value;
}
arg->v_int64 = PyLong_AsLongLong (number);
break;
}
case GI_TYPE_TAG_UINT64:
{
/* Rely on Python overflow error and convert to ValueError for 64 bit values */
guint64 value = PyLong_AsUnsignedLongLong (number);
if (PyErr_Occurred()) {
if (PyErr_ExceptionMatches (PyExc_OverflowError)) {
PyErr_Clear();
#if PY_MAJOR_VERSION >= 3
PyErr_Format (PyExc_ValueError, "%S not in range %ld to %llu",
number, (long)0, PY_ULLONG_MAX);
#else
/* Python 2.x does not support the %S format option and it is not
* worth the trouble to support this manually.
*/
PyErr_Format (PyExc_ValueError, "number not in range %ld to %llu",
(long)0, PY_ULLONG_MAX);
#endif
}
} else {
arg->v_uint64 = value;
}
arg->v_uint64 = PyLong_AsUnsignedLongLong (number);
break;
}
......
......@@ -94,108 +94,108 @@ class TestEverything(unittest.TestCase):
GObject.G_MAXINT8)
self.assertEqual(Everything.test_int8(GObject.G_MININT8),
GObject.G_MININT8)
self.assertRaises(ValueError, Everything.test_int8, GObject.G_MAXINT8 + 1)
self.assertRaises(OverflowError, Everything.test_int8, GObject.G_MAXINT8 + 1)
self.assertEqual(Everything.test_uint8(GObject.G_MAXUINT8),
GObject.G_MAXUINT8)
self.assertEqual(Everything.test_uint8(0), 0)
self.assertRaises(ValueError, Everything.test_uint8, -1)
self.assertRaises(ValueError, Everything.test_uint8, GObject.G_MAXUINT8 + 1)
self.assertRaises(OverflowError, Everything.test_uint8, -1)
self.assertRaises(OverflowError, Everything.test_uint8, GObject.G_MAXUINT8 + 1)
def test_int16(self):
self.assertEqual(Everything.test_int16(GObject.G_MAXINT16),
GObject.G_MAXINT16)
self.assertEqual(Everything.test_int16(GObject.G_MININT16),
GObject.G_MININT16)
self.assertRaises(ValueError, Everything.test_int16, GObject.G_MAXINT16 + 1)
self.assertRaises(OverflowError, Everything.test_int16, GObject.G_MAXINT16 + 1)
self.assertEqual(Everything.test_uint16(GObject.G_MAXUINT16),
GObject.G_MAXUINT16)
self.assertEqual(Everything.test_uint16(0), 0)
self.assertRaises(ValueError, Everything.test_uint16, -1)
self.assertRaises(ValueError, Everything.test_uint16, GObject.G_MAXUINT16 + 1)
self.assertRaises(OverflowError, Everything.test_uint16, -1)
self.assertRaises(OverflowError, Everything.test_uint16, GObject.G_MAXUINT16 + 1)
def test_int32(self):
self.assertEqual(Everything.test_int32(GObject.G_MAXINT32),
GObject.G_MAXINT32)
self.assertEqual(Everything.test_int32(GObject.G_MININT32),
GObject.G_MININT32)
self.assertRaises(ValueError, Everything.test_int32, GObject.G_MAXINT32 + 1)
self.assertRaises(OverflowError, Everything.test_int32, GObject.G_MAXINT32 + 1)
self.assertEqual(Everything.test_uint32(GObject.G_MAXUINT32),
GObject.G_MAXUINT32)
self.assertEqual(Everything.test_uint32(0), 0)
self.assertRaises(ValueError, Everything.test_uint32, -1)
self.assertRaises(ValueError, Everything.test_uint32, GObject.G_MAXUINT32 + 1)
self.assertRaises(OverflowError, Everything.test_uint32, -1)
self.assertRaises(OverflowError, Everything.test_uint32, GObject.G_MAXUINT32 + 1)
def test_int64(self):
self.assertEqual(Everything.test_int64(GObject.G_MAXINT64),
GObject.G_MAXINT64)
self.assertEqual(Everything.test_int64(GObject.G_MININT64),
GObject.G_MININT64)
self.assertRaises(ValueError, Everything.test_int64, GObject.G_MAXINT64 + 1)
self.assertRaises(OverflowError, Everything.test_int64, GObject.G_MAXINT64 + 1)
self.assertEqual(Everything.test_uint64(GObject.G_MAXUINT64),
GObject.G_MAXUINT64)
self.assertEqual(Everything.test_uint64(0), 0)
self.assertRaises(ValueError, Everything.test_uint64, -1)
self.assertRaises(ValueError, Everything.test_uint64, GObject.G_MAXUINT64 + 1)
self.assertRaises(OverflowError, Everything.test_uint64, -1)
self.assertRaises(OverflowError, Everything.test_uint64, GObject.G_MAXUINT64 + 1)
def test_int(self):
self.assertEqual(Everything.test_int(GObject.G_MAXINT),
GObject.G_MAXINT)
self.assertEqual(Everything.test_int(GObject.G_MININT),
GObject.G_MININT)
self.assertRaises(ValueError, Everything.test_int, GObject.G_MAXINT + 1)
self.assertRaises(OverflowError, Everything.test_int, GObject.G_MAXINT + 1)
self.assertEqual(Everything.test_uint(GObject.G_MAXUINT),
GObject.G_MAXUINT)
self.assertEqual(Everything.test_uint(0), 0)
self.assertRaises(ValueError, Everything.test_uint, -1)
self.assertRaises(ValueError, Everything.test_uint, GObject.G_MAXUINT + 1)
self.assertRaises(OverflowError, Everything.test_uint, -1)
self.assertRaises(OverflowError, Everything.test_uint, GObject.G_MAXUINT + 1)
def test_short(self):
self.assertEqual(Everything.test_short(GObject.G_MAXSHORT),
GObject.G_MAXSHORT)
self.assertEqual(Everything.test_short(GObject.G_MINSHORT),
GObject.G_MINSHORT)
self.assertRaises(ValueError, Everything.test_short, GObject.G_MAXSHORT + 1)
self.assertRaises(OverflowError, Everything.test_short, GObject.G_MAXSHORT + 1)
self.assertEqual(Everything.test_ushort(GObject.G_MAXUSHORT),
GObject.G_MAXUSHORT)
self.assertEqual(Everything.test_ushort(0), 0)
self.assertRaises(ValueError, Everything.test_ushort, -1)
self.assertRaises(ValueError, Everything.test_ushort, GObject.G_MAXUSHORT + 1)
self.assertRaises(OverflowError, Everything.test_ushort, -1)
self.assertRaises(OverflowError, Everything.test_ushort, GObject.G_MAXUSHORT + 1)
def test_long(self):
self.assertEqual(Everything.test_long(GObject.G_MAXLONG),
GObject.G_MAXLONG)
self.assertEqual(Everything.test_long(GObject.G_MINLONG),
GObject.G_MINLONG)
self.assertRaises(ValueError, Everything.test_long, GObject.G_MAXLONG + 1)
self.assertRaises(OverflowError, Everything.test_long, GObject.G_MAXLONG + 1)
self.assertEqual(Everything.test_ulong(GObject.G_MAXULONG),
GObject.G_MAXULONG)
self.assertEqual(Everything.test_ulong(0), 0)
self.assertRaises(ValueError, Everything.test_ulong, -1)
self.assertRaises(ValueError, Everything.test_ulong, GObject.G_MAXULONG + 1)
self.assertRaises(OverflowError, Everything.test_ulong, -1)
self.assertRaises(OverflowError, Everything.test_ulong, GObject.G_MAXULONG + 1)
def test_size(self):
self.assertEqual(Everything.test_ssize(GObject.G_MAXSSIZE),
GObject.G_MAXSSIZE)
self.assertEqual(Everything.test_ssize(GObject.G_MINSSIZE),
GObject.G_MINSSIZE)
self.assertRaises(ValueError, Everything.test_ssize, GObject.G_MAXSSIZE + 1)
self.assertRaises(OverflowError, Everything.test_ssize, GObject.G_MAXSSIZE + 1)
self.assertEqual(Everything.test_size(GObject.G_MAXSIZE),
GObject.G_MAXSIZE)
self.assertEqual(Everything.test_size(0), 0)
self.assertRaises(ValueError, Everything.test_size, -1)
self.assertRaises(ValueError, Everything.test_size, GObject.G_MAXSIZE + 1)
self.assertRaises(OverflowError, Everything.test_size, -1)
self.assertRaises(OverflowError, Everything.test_size, GObject.G_MAXSIZE + 1)
def test_timet(self):
self.assertEqual(Everything.test_timet(42), 42)
self.assertRaises(ValueError, Everything.test_timet, GObject.G_MAXUINT64 + 1)
self.assertRaises(OverflowError, Everything.test_timet, GObject.G_MAXUINT64 + 1)
def test_unichar(self):
self.assertEqual("c", Everything.test_unichar("c"))
......@@ -211,7 +211,7 @@ class TestEverything(unittest.TestCase):
GObject.G_MAXFLOAT)
self.assertEqual(Everything.test_float(GObject.G_MINFLOAT),
GObject.G_MINFLOAT)
self.assertRaises(ValueError, Everything.test_float, GObject.G_MAXFLOAT * 2)
self.assertRaises(OverflowError, Everything.test_float, GObject.G_MAXFLOAT * 2)
def test_double(self):
self.assertEqual(Everything.test_double(GObject.G_MAXDOUBLE),
......
......@@ -117,8 +117,8 @@ class TestInt8(unittest.TestCase):
max.value += 1
min.value -= 1
self.assertRaises(ValueError, GIMarshallingTests.int8_in_max, max)
self.assertRaises(ValueError, GIMarshallingTests.int8_in_min, min)
self.assertRaises(OverflowError, GIMarshallingTests.int8_in_max, max)
self.assertRaises(OverflowError, GIMarshallingTests.int8_in_min, min)
self.assertRaises(TypeError, GIMarshallingTests.int8_in_max, "self.MAX")
......@@ -145,8 +145,8 @@ class TestUInt8(unittest.TestCase):
GIMarshallingTests.uint8_in(CHAR_255)
number.value += 1
self.assertRaises(ValueError, GIMarshallingTests.uint8_in, number)
self.assertRaises(ValueError, GIMarshallingTests.uint8_in, Number(-1))
self.assertRaises(OverflowError, GIMarshallingTests.uint8_in, number)
self.assertRaises(OverflowError, GIMarshallingTests.uint8_in, Number(-1))
self.assertRaises(TypeError, GIMarshallingTests.uint8_in, "self.MAX")
......@@ -176,8 +176,8 @@ class TestInt16(unittest.TestCase):
max.value += 1
min.value -= 1
self.assertRaises(ValueError, GIMarshallingTests.int16_in_max, max)
self.assertRaises(ValueError, GIMarshallingTests.int16_in_min, min)
self.assertRaises(OverflowError, GIMarshallingTests.int16_in_max, max)
self.assertRaises(OverflowError, GIMarshallingTests.int16_in_min, min)
self.assertRaises(TypeError, GIMarshallingTests.int16_in_max, "self.MAX")
......@@ -204,8 +204,8 @@ class TestUInt16(unittest.TestCase):
number.value += 1
self.assertRaises(ValueError, GIMarshallingTests.uint16_in, number)
self.assertRaises(ValueError, GIMarshallingTests.uint16_in, Number(-1))
self.assertRaises(OverflowError, GIMarshallingTests.uint16_in, number)
self.assertRaises(OverflowError, GIMarshallingTests.uint16_in, Number(-1))
self.assertRaises(TypeError, GIMarshallingTests.uint16_in, "self.MAX")
......@@ -235,8 +235,8 @@ class TestInt32(unittest.TestCase):
max.value += 1
min.value -= 1
self.assertRaises(ValueError, GIMarshallingTests.int32_in_max, max)
self.assertRaises(ValueError, GIMarshallingTests.int32_in_min, min)
self.assertRaises(OverflowError, GIMarshallingTests.int32_in_max, max)
self.assertRaises(OverflowError, GIMarshallingTests.int32_in_min, min)
self.assertRaises(TypeError, GIMarshallingTests.int32_in_max, "self.MAX")
......@@ -263,8 +263,8 @@ class TestUInt32(unittest.TestCase):
number.value += 1
self.assertRaises(ValueError, GIMarshallingTests.uint32_in, number)
self.assertRaises(ValueError, GIMarshallingTests.uint32_in, Number(-1))
self.assertRaises(OverflowError, GIMarshallingTests.uint32_in, number)
self.assertRaises(OverflowError, GIMarshallingTests.uint32_in, Number(-1))
self.assertRaises(TypeError, GIMarshallingTests.uint32_in, "self.MAX")
......@@ -294,8 +294,8 @@ class TestInt64(unittest.TestCase):
max.value += 1
min.value -= 1
self.assertRaises(ValueError, GIMarshallingTests.int64_in_max, max)
self.assertRaises(ValueError, GIMarshallingTests.int64_in_min, min)
self.assertRaises(OverflowError, GIMarshallingTests.int64_in_max, max)
self.assertRaises(OverflowError, GIMarshallingTests.int64_in_min, min)
self.assertRaises(TypeError, GIMarshallingTests.int64_in_max, "self.MAX")
......@@ -322,8 +322,8 @@ class TestUInt64(unittest.TestCase):
number.value += 1
self.assertRaises(ValueError, GIMarshallingTests.uint64_in, number)
self.assertRaises(ValueError, GIMarshallingTests.uint64_in, Number(-1))
self.assertRaises(OverflowError, GIMarshallingTests.uint64_in, number)
self.assertRaises(OverflowError, GIMarshallingTests.uint64_in, Number(-1))
self.assertRaises(TypeError, GIMarshallingTests.uint64_in, "self.MAX")
......@@ -353,8 +353,8 @@ class TestShort(unittest.TestCase):
max.value += 1
min.value -= 1
self.assertRaises(ValueError, GIMarshallingTests.short_in_max, max)
self.assertRaises(ValueError, GIMarshallingTests.short_in_min, min)
self.assertRaises(OverflowError, GIMarshallingTests.short_in_max, max)
self.assertRaises(OverflowError, GIMarshallingTests.short_in_min, min)
self.assertRaises(TypeError, GIMarshallingTests.short_in_max, "self.MAX")
......@@ -381,8 +381,8 @@ class TestUShort(unittest.TestCase):
number.value += 1
self.assertRaises(ValueError, GIMarshallingTests.ushort_in, number)
self.assertRaises(ValueError, GIMarshallingTests.ushort_in, Number(-1))
self.assertRaises(OverflowError, GIMarshallingTests.ushort_in, number)
self.assertRaises(OverflowError, GIMarshallingTests.ushort_in, Number(-1))
self.assertRaises(TypeError, GIMarshallingTests.ushort_in, "self.MAX")
......@@ -412,8 +412,8 @@ class TestInt(unittest.TestCase):
max.value += 1
min.value -= 1
self.assertRaises(ValueError, GIMarshallingTests.int_in_max, max)
self.assertRaises(ValueError, GIMarshallingTests.int_in_min, min)
self.assertRaises(OverflowError, GIMarshallingTests.int_in_max, max)
self.assertRaises(OverflowError, GIMarshallingTests.int_in_min, min)
self.assertRaises(TypeError, GIMarshallingTests.int_in_max, "self.MAX")
......@@ -441,8 +441,8 @@ class TestUInt(unittest.TestCase):
number.value += 1
self.assertRaises(ValueError, GIMarshallingTests.uint_in, number)
self.assertRaises(ValueError, GIMarshallingTests.uint_in, Number(-1))
self.assertRaises(OverflowError, GIMarshallingTests.uint_in, number)
self.assertRaises(OverflowError, GIMarshallingTests.uint_in, Number(-1))
self.assertRaises(TypeError, GIMarshallingTests.uint_in, "self.MAX")
......@@ -472,8 +472,8 @@ class TestLong(unittest.TestCase):
max.value += 1
min.value -= 1
self.assertRaises(ValueError, GIMarshallingTests.long_in_max, max)
self.assertRaises(ValueError, GIMarshallingTests.long_in_min, min)
self.assertRaises(OverflowError, GIMarshallingTests.long_in_max, max)
self.assertRaises(OverflowError, GIMarshallingTests.long_in_min, min)
self.assertRaises(TypeError, GIMarshallingTests.long_in_max, "self.MAX")
......@@ -500,8 +500,8 @@ class TestULong(unittest.TestCase):
number.value += 1
self.assertRaises(ValueError, GIMarshallingTests.ulong_in, number)
self.assertRaises(ValueError, GIMarshallingTests.ulong_in, Number(-1))
self.assertRaises(OverflowError, GIMarshallingTests.ulong_in, number)
self.assertRaises(OverflowError, GIMarshallingTests.ulong_in, Number(-1))
self.assertRaises(TypeError, GIMarshallingTests.ulong_in, "self.MAX")
......@@ -531,8 +531,8 @@ class TestSSize(unittest.TestCase):
max.value += 1
min.value -= 1
self.assertRaises(ValueError, GIMarshallingTests.ssize_in_max, max)
self.assertRaises(ValueError, GIMarshallingTests.ssize_in_min, min)
self.assertRaises(OverflowError, GIMarshallingTests.ssize_in_max, max)
self.assertRaises(OverflowError, GIMarshallingTests.ssize_in_min, min)
self.assertRaises(TypeError, GIMarshallingTests.ssize_in_max, "self.MAX")
......@@ -559,8 +559,8 @@ class TestSize(unittest.TestCase):
number.value += 1
self.assertRaises(ValueError, GIMarshallingTests.size_in, number)
self.assertRaises(ValueError, GIMarshallingTests.size_in, Number(-1))
self.assertRaises(OverflowError, GIMarshallingTests.size_in, number)
self.assertRaises(OverflowError, GIMarshallingTests.size_in, Number(-1))
self.assertRaises(TypeError, GIMarshallingTests.size_in, "self.MAX")
......
......@@ -629,7 +629,7 @@ class TestGValue(unittest.TestCase):
value = GObject.Value(GObject.TYPE_FLOAT, 23.4)
self.assertEqual(value.g_type, GObject.TYPE_FLOAT)
self.assertRaises(TypeError, value.set_value, 'string')
self.assertRaises(ValueError, value.set_value, 1e50)
self.assertRaises(OverflowError, value.set_value, 1e50)
def test_float_inf_nan(self):
nan = float('nan')
......
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