NUMPY_API Get a subset of bytes from each element of the array steals reference to typed, must not be NULL */
| 374 | steals reference to typed, must not be NULL |
| 375 | */ |
| 376 | NPY_NO_EXPORT PyObject * |
| 377 | PyArray_GetField(PyArrayObject *self, PyArray_Descr *typed, int offset) |
| 378 | { |
| 379 | PyObject *ret = NULL; |
| 380 | PyObject *safe; |
| 381 | static PyObject *checkfunc = NULL; |
| 382 | int self_elsize, typed_elsize; |
| 383 | |
| 384 | if (self == NULL) { |
| 385 | PyErr_SetString(PyExc_ValueError, |
| 386 | "self is NULL in PyArray_GetField"); |
| 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | if (typed == NULL) { |
| 391 | PyErr_SetString(PyExc_ValueError, |
| 392 | "typed is NULL in PyArray_GetField"); |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | /* check that we are not reinterpreting memory containing Objects. */ |
| 397 | if (_may_have_objects(PyArray_DESCR(self)) || _may_have_objects(typed)) { |
| 398 | npy_cache_import("numpy.core._internal", "_getfield_is_safe", |
| 399 | &checkfunc); |
| 400 | if (checkfunc == NULL) { |
| 401 | Py_DECREF(typed); |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | /* only returns True or raises */ |
| 406 | safe = PyObject_CallFunction(checkfunc, "OOi", PyArray_DESCR(self), |
| 407 | typed, offset); |
| 408 | if (safe == NULL) { |
| 409 | Py_DECREF(typed); |
| 410 | return NULL; |
| 411 | } |
| 412 | Py_DECREF(safe); |
| 413 | } |
| 414 | self_elsize = PyArray_ITEMSIZE(self); |
| 415 | typed_elsize = typed->elsize; |
| 416 | |
| 417 | /* check that values are valid */ |
| 418 | if (typed_elsize > self_elsize) { |
| 419 | PyErr_SetString(PyExc_ValueError, "new type is larger than original type"); |
| 420 | Py_DECREF(typed); |
| 421 | return NULL; |
| 422 | } |
| 423 | if (offset < 0) { |
| 424 | PyErr_SetString(PyExc_ValueError, "offset is negative"); |
| 425 | Py_DECREF(typed); |
| 426 | return NULL; |
| 427 | } |
| 428 | if (offset > self_elsize - typed_elsize) { |
| 429 | PyErr_SetString(PyExc_ValueError, "new type plus offset is larger than original type"); |
| 430 | Py_DECREF(typed); |
| 431 | return NULL; |
| 432 | } |
| 433 |
no test coverage detected