|
8 | 8 | AllocatableArray, |
9 | 9 | PointerArray, |
10 | 10 | _bind_contract_native_array_handle, |
| 11 | + _native_array_actual_for_binding, |
11 | 12 | _native_array_descriptor_argument_for_binding, |
12 | 13 | _native_array_descriptor_handoff_for_binding, |
13 | 14 | ) |
@@ -111,6 +112,7 @@ def source_nullify(_handle): |
111 | 112 | assert target.associated is True |
112 | 113 | assert target.shape == (3,) |
113 | 114 | np.testing.assert_array_equal(target.to_numpy(), value) |
| 115 | + assert _native_array_actual_for_binding(target).address == value.ctypes.data |
114 | 116 |
|
115 | 117 | source.nullify() |
116 | 118 | assert source.associated is False |
@@ -232,6 +234,107 @@ def bind_default(value): |
232 | 234 | assert calls == [("destroy", owner)] |
233 | 235 |
|
234 | 236 |
|
| 237 | +@pytest.mark.parametrize( |
| 238 | + ("prepare", "descriptor_kind", "dtype", "rank", "error", "message"), |
| 239 | + [ |
| 240 | + ( |
| 241 | + lambda: AllocatableArray( |
| 242 | + dtype="float64", |
| 243 | + rank=1, |
| 244 | + ops={ |
| 245 | + "shape": lambda _handle: None, |
| 246 | + "array_actual": lambda _handle: None, |
| 247 | + "descriptor": lambda _handle: None, |
| 248 | + "allocated": lambda _handle: False, |
| 249 | + }, |
| 250 | + to_numpy_policy="unsupported", |
| 251 | + ), |
| 252 | + "allocatable", |
| 253 | + "float64", |
| 254 | + 1, |
| 255 | + TypeError, |
| 256 | + "fresh contract handle", |
| 257 | + ), |
| 258 | + ( |
| 259 | + lambda: contracts.Allocatable[contracts.Float64[:]](), |
| 260 | + "pointer", |
| 261 | + "float64", |
| 262 | + 1, |
| 263 | + TypeError, |
| 264 | + "cannot attach pointer descriptor storage", |
| 265 | + ), |
| 266 | + ( |
| 267 | + lambda: contracts.Allocatable[contracts.Float64[:]](), |
| 268 | + "allocatable", |
| 269 | + "float64", |
| 270 | + 2, |
| 271 | + ValueError, |
| 272 | + "does not match generated rank 2", |
| 273 | + ), |
| 274 | + ( |
| 275 | + lambda: contracts.Allocatable[contracts.Float64[:]](), |
| 276 | + "allocatable", |
| 277 | + "int32", |
| 278 | + 1, |
| 279 | + TypeError, |
| 280 | + "does not match generated dtype", |
| 281 | + ), |
| 282 | + ], |
| 283 | +) |
| 284 | +def test_generated_storage_rejects_incompatible_contract_handles( |
| 285 | + prepare, |
| 286 | + descriptor_kind, |
| 287 | + dtype, |
| 288 | + rank, |
| 289 | + error, |
| 290 | + message, |
| 291 | +): |
| 292 | + handle = prepare() |
| 293 | + |
| 294 | + with pytest.raises(error, match=message): |
| 295 | + _bind_contract_native_array_handle( |
| 296 | + handle, |
| 297 | + descriptor_kind, |
| 298 | + dtype, |
| 299 | + rank, |
| 300 | + {}, |
| 301 | + object(), |
| 302 | + "owned", |
| 303 | + "unsupported", |
| 304 | + ) |
| 305 | + |
| 306 | + |
| 307 | +def test_generated_storage_rejects_a_closed_contract_handle(): |
| 308 | + handle = contracts.Allocatable[contracts.Float64[:]]() |
| 309 | + handle.close() |
| 310 | + |
| 311 | + with pytest.raises(ReferenceError, match="handle is closed"): |
| 312 | + _bind_contract_native_array_handle( |
| 313 | + handle, |
| 314 | + "allocatable", |
| 315 | + "float64", |
| 316 | + 1, |
| 317 | + {}, |
| 318 | + object(), |
| 319 | + "owned", |
| 320 | + "unsupported", |
| 321 | + ) |
| 322 | + |
| 323 | + |
| 324 | +def test_pointer_association_rejects_closed_handles(): |
| 325 | + target = contracts.Pointer[contracts.Float64[:]]() |
| 326 | + source = contracts.Pointer[contracts.Float64[:]]() |
| 327 | + target.close() |
| 328 | + |
| 329 | + with pytest.raises(ReferenceError, match="pointer handle is closed"): |
| 330 | + target.associate(source) |
| 331 | + |
| 332 | + target = contracts.Pointer[contracts.Float64[:]]() |
| 333 | + source.close() |
| 334 | + with pytest.raises(ReferenceError, match="source pointer handle is closed"): |
| 335 | + target.associate(source) |
| 336 | + |
| 337 | + |
235 | 338 | def test_non_array_descriptor_and_ordinary_array_annotations_are_not_factories(): |
236 | 339 | with pytest.raises(TypeError, match="scalar allocatable contracts"): |
237 | 340 | contracts.Allocatable[contracts.Float64]() |
|
0 commit comments