MCPcopy Index your code
hub / github.com/python/cpython / test_ndarray_exceptions

Method test_ndarray_exceptions

Lib/test/test_buffer.py:1150–1306  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1148 self.assertTrue(is_contiguous(nd, 'C'))
1149
1150 def test_ndarray_exceptions(self):
1151 nd = ndarray([9], [1])
1152 ndm = ndarray([9], [1], flags=ND_VAREXPORT)
1153
1154 # Initialization of a new ndarray or mutation of an existing array.
1155 for c in (ndarray, nd.push, ndm.push):
1156 # Invalid types.
1157 self.assertRaises(TypeError, c, {1,2,3})
1158 self.assertRaises(TypeError, c, [1,2,'3'])
1159 self.assertRaises(TypeError, c, [1,2,(3,4)])
1160 self.assertRaises(TypeError, c, [1,2,3], shape={3})
1161 self.assertRaises(TypeError, c, [1,2,3], shape=[3], strides={1})
1162 self.assertRaises(TypeError, c, [1,2,3], shape=[3], offset=[])
1163 self.assertRaises(TypeError, c, [1], shape=[1], format={})
1164 self.assertRaises(TypeError, c, [1], shape=[1], flags={})
1165 self.assertRaises(TypeError, c, [1], shape=[1], getbuf={})
1166
1167 # ND_FORTRAN flag is only valid without strides.
1168 self.assertRaises(TypeError, c, [1], shape=[1], strides=[1],
1169 flags=ND_FORTRAN)
1170
1171 # ND_PIL flag is only valid with ndim > 0.
1172 self.assertRaises(TypeError, c, [1], shape=[], flags=ND_PIL)
1173
1174 # Invalid items.
1175 self.assertRaises(ValueError, c, [], shape=[1])
1176 self.assertRaises(ValueError, c, ['XXX'], shape=[1], format="L")
1177 # Invalid combination of items and format.
1178 self.assertRaises(struct.error, c, [1000], shape=[1], format="B")
1179 self.assertRaises(ValueError, c, [1,(2,3)], shape=[2], format="B")
1180 self.assertRaises(ValueError, c, [1,2,3], shape=[3], format="QL")
1181
1182 # Invalid ndim.
1183 n = ND_MAX_NDIM+1
1184 self.assertRaises(ValueError, c, [1]*n, shape=[1]*n)
1185
1186 # Invalid shape.
1187 self.assertRaises(ValueError, c, [1], shape=[-1])
1188 self.assertRaises(ValueError, c, [1,2,3], shape=['3'])
1189 self.assertRaises(OverflowError, c, [1], shape=[2**128])
1190 # prod(shape) * itemsize != len(items)
1191 self.assertRaises(ValueError, c, [1,2,3,4,5], shape=[2,2], offset=3)
1192
1193 # Invalid strides.
1194 self.assertRaises(ValueError, c, [1,2,3], shape=[3], strides=['1'])
1195 self.assertRaises(OverflowError, c, [1], shape=[1],
1196 strides=[2**128])
1197
1198 # Invalid combination of strides and shape.
1199 self.assertRaises(ValueError, c, [1,2], shape=[2,1], strides=[1])
1200 # Invalid combination of strides and format.
1201 self.assertRaises(ValueError, c, [1,2,3,4], shape=[2], strides=[3],
1202 format="L")
1203
1204 # Invalid offset.
1205 self.assertRaises(ValueError, c, [1,2,3], shape=[3], offset=4)
1206 self.assertRaises(ValueError, c, [1,2,3], shape=[1], offset=3,
1207 format="L")

Callers

nothing calls this directly

Calls 5

listClass · 0.85
assertRaisesMethod · 0.45
pushMethod · 0.45
releaseMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected