b = self.type2test([Indexable(), Indexable(1), Indexable(254),
Indexable(255)])
self.assertEqual(list(b), [0, 1, 254, 255])
- self.assertRaises(ValueError, bytearray, [Indexable(-1)])
- self.assertRaises(ValueError, bytearray, [Indexable(256)])
+ self.assertRaises(ValueError, self.type2test, [Indexable(-1)])
+ self.assertRaises(ValueError, self.type2test, [Indexable(256)])
def test_from_ssize(self):
- self.assertEqual(bytearray(0), b'')
- self.assertEqual(bytearray(1), b'\x00')
- self.assertEqual(bytearray(5), b'\x00\x00\x00\x00\x00')
- self.assertRaises(ValueError, bytearray, -1)
-
- self.assertEqual(bytearray('0', 'ascii'), b'0')
- self.assertEqual(bytearray(b'0'), b'0')
+ self.assertEqual(self.type2test(0), b'')
+ self.assertEqual(self.type2test(1), b'\x00')
+ self.assertEqual(self.type2test(5), b'\x00\x00\x00\x00\x00')
+ self.assertRaises(ValueError, self.type2test, -1)
+
+ self.assertEqual(self.type2test('0', 'ascii'), b'0')
+ self.assertEqual(self.type2test(b'0'), b'0')
+ self.assertRaises(OverflowError, self.type2test, sys.maxsize + 1)
def test_constructor_type_errors(self):
self.assertRaises(TypeError, self.type2test, 0.0)
}
/* Is it an int? */
- count = PyNumber_AsSsize_t(arg, PyExc_ValueError);
- if (count == -1 && PyErr_Occurred())
+ count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
+ if (count == -1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ return -1;
PyErr_Clear();
+ }
+ else if (count < 0) {
+ PyErr_SetString(PyExc_ValueError, "negative count");
+ return -1;
+ }
else {
- if (count < 0) {
- PyErr_SetString(PyExc_ValueError, "negative count");
- return -1;
- }
if (count > 0) {
if (PyByteArray_Resize((PyObject *)self, count))
return -1;
}
/* Is it an int? */
- size = PyNumber_AsSsize_t(x, PyExc_ValueError);
+ size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
if (size == -1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ return NULL;
PyErr_Clear();
}
+ else if (size < 0) {
+ PyErr_SetString(PyExc_ValueError, "negative count");
+ return NULL;
+ }
else {
- if (size < 0) {
- PyErr_SetString(PyExc_ValueError, "negative count");
- return NULL;
- }
new = PyBytes_FromStringAndSize(NULL, size);
if (new == NULL) {
return NULL;