|
2 | 2 | import os |
3 | 3 | import tempfile |
4 | 4 | import unittest |
| 5 | +from collections import UserList |
5 | 6 | from typing import Any |
6 | 7 |
|
7 | 8 | import numpy as np |
@@ -261,13 +262,56 @@ def test_one_dimensional_empty_broadcast(self): |
261 | 262 | ds.createDimension('x', 3) |
262 | 263 | variable = ds.createVariable('data', 'i4', ('x',)) |
263 | 264 | variable[:] = [10, 20, 30] |
264 | | - for empty in (EMPTY, slice(0), np.zeros(3, dtype=bool)): |
| 265 | + cases = ( |
| 266 | + (EMPTY, ValueError), |
| 267 | + (slice(0), IndexError), |
| 268 | + (np.zeros(3, dtype=bool), ValueError), |
| 269 | + ) |
| 270 | + for empty, error in cases: |
265 | 271 | with self.subTest(empty=repr(empty)): |
266 | 272 | variable[empty] = np.array([123]) |
267 | 273 | assert_array_equal(variable[:], [10, 20, 30]) |
268 | | - with self.assertRaises((ValueError, IndexError)): |
| 274 | + with self.assertRaises(error): |
269 | 275 | variable[empty] = np.array([123, 456]) |
270 | 276 |
|
| 277 | + def test_oversized_empty_slice_write(self): |
| 278 | + path = os.path.join(self.directory.name, 'slice-write.nc') |
| 279 | + cases = ( |
| 280 | + (None, slice(-3, None)), |
| 281 | + (None, (Ellipsis, slice(-2, -2))), |
| 282 | + (None, [slice(-3, None)]), |
| 283 | + (2, slice(1, 1)), |
| 284 | + (2, UserList([slice(1, 1)])), |
| 285 | + ) |
| 286 | + for length, key in cases: |
| 287 | + with self.subTest(length=length, key=key): |
| 288 | + with Dataset(path, 'w') as ds: |
| 289 | + ds.createDimension('time', length) |
| 290 | + variable = ds.createVariable('sensor', 'i4', ('time',)) |
| 291 | + variable[:] = [0, 1] |
| 292 | + with self.assertRaisesRegex(IndexError, 'size of data array does not conform to slice'): |
| 293 | + variable[key] = [9, 8, 7] |
| 294 | + variable[:0] = [9] |
| 295 | + self.assertEqual(len(ds.dimensions['time']), 2) |
| 296 | + self.assertEqual(variable.shape, (2,)) |
| 297 | + assert_array_equal(variable[:], [0, 1]) |
| 298 | + |
| 299 | + def test_oversized_empty_strided_slice_write(self): |
| 300 | + path = os.path.join(self.directory.name, 'strided-write.nc') |
| 301 | + for use_get_vars in (False, True): |
| 302 | + with self.subTest(use_get_vars=use_get_vars): |
| 303 | + with Dataset(path, 'w') as ds: |
| 304 | + ds.createDimension('time', 2) |
| 305 | + variable = ds.createVariable('sensor', 'i4', ('time',)) |
| 306 | + variable[:] = [0, 1] |
| 307 | + variable.use_nc_get_vars(use_get_vars) |
| 308 | + with self.assertRaisesRegex(IndexError, 'size of data array does not conform to slice'): |
| 309 | + variable[4:5:2] = [9, 8, 7] |
| 310 | + variable[4:5:2] = [9] |
| 311 | + self.assertEqual(len(ds.dimensions['time']), 2) |
| 312 | + self.assertEqual(variable.shape, (2,)) |
| 313 | + assert_array_equal(variable[:], [0, 1]) |
| 314 | + |
271 | 315 | def test_empty_encoded_characters(self): |
272 | 316 | raw = np.array(['alpha', 'bravo', 'delta', 'gamma'], dtype='S5').view('S1').reshape(4, 5) |
273 | 317 | paths = [] |
|
0 commit comments