MCPcopy Create free account
hub / github.com/numpy/numpy / _array_from_buffer_3118

Function _array_from_buffer_3118

numpy/core/src/multiarray/ctors.c:1236–1373  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1234
1235
1236NPY_NO_EXPORT PyObject *
1237_array_from_buffer_3118(PyObject *memoryview)
1238{
1239 /* PEP 3118 */
1240 Py_buffer *view;
1241 PyArray_Descr *descr = NULL;
1242 PyObject *r = NULL;
1243 int nd, flags;
1244 Py_ssize_t d;
1245 npy_intp shape[NPY_MAXDIMS], strides[NPY_MAXDIMS];
1246
1247 view = PyMemoryView_GET_BUFFER(memoryview);
1248
1249 if (view->suboffsets != NULL) {
1250 PyErr_SetString(PyExc_BufferError,
1251 "NumPy currently does not support importing buffers which "
1252 "include suboffsets as they are not compatible with the NumPy"
1253 "memory layout without a copy. Consider copying the original "
1254 "before trying to convert it to a NumPy array.");
1255 return NULL;
1256 }
1257
1258 nd = view->ndim;
1259 descr = _dtype_from_buffer_3118(memoryview);
1260
1261 if (descr == NULL) {
1262 return NULL;
1263 }
1264
1265 /* Sanity check */
1266 if (descr->elsize != view->itemsize) {
1267 /* Ctypes has bugs in its PEP3118 implementation, which we need to
1268 * work around.
1269 *
1270 * bpo-10746
1271 * bpo-32780
1272 * bpo-32782
1273 *
1274 * Note that even if the above are fixed in main, we have to drop the
1275 * early patch versions of python to actually make use of the fixes.
1276 */
1277 if (!npy_ctypes_check(Py_TYPE(view->obj))) {
1278 /* This object has no excuse for a broken PEP3118 buffer */
1279 PyErr_Format(
1280 PyExc_RuntimeError,
1281 "Item size %zd for PEP 3118 buffer format "
1282 "string %s does not match the dtype %c item size %d.",
1283 view->itemsize, view->format, descr->type,
1284 descr->elsize);
1285 Py_DECREF(descr);
1286 return NULL;
1287 }
1288
1289 if (PyErr_Warn(
1290 PyExc_RuntimeWarning,
1291 "A builtin ctypes object gave a PEP3118 format "
1292 "string that does not match its itemsize, so a "
1293 "best-guess will be made of the data type. "

Callers 1

_array_from_array_likeFunction · 0.85

Calls 3

_dtype_from_buffer_3118Function · 0.85
npy_ctypes_checkFunction · 0.50

Tested by

no test coverage detected