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

Function _array_from_array_like

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

* Attempts to extract an array from an array-like object. * * array-like is defined as either * * * an object implementing the PEP 3118 buffer interface; * * an object with __array_struct__ or __array_interface__ attributes; * * an object with an __array__ function. * * @param op The object to convert to an array * @param requested_type a requested dtype instance, may be NULL; The result

Source from the content-addressed store, hash-verified

1394 * is returned.)
1395 */
1396NPY_NO_EXPORT PyObject *
1397_array_from_array_like(PyObject *op,
1398 PyArray_Descr *requested_dtype, npy_bool writeable, PyObject *context,
1399 int never_copy) {
1400 PyObject* tmp;
1401
1402 /*
1403 * If op supports the PEP 3118 buffer interface.
1404 * We skip bytes and unicode since they are considered scalars. Unicode
1405 * would fail but bytes would be incorrectly converted to a uint8 array.
1406 */
1407 if (PyObject_CheckBuffer(op) && !PyBytes_Check(op) && !PyUnicode_Check(op)) {
1408 PyObject *memoryview = PyMemoryView_FromObject(op);
1409 if (memoryview == NULL) {
1410 /* TODO: Should probably not blanket ignore errors. */
1411 PyErr_Clear();
1412 }
1413 else {
1414 tmp = _array_from_buffer_3118(memoryview);
1415 Py_DECREF(memoryview);
1416 if (tmp == NULL) {
1417 return NULL;
1418 }
1419
1420 if (writeable
1421 && PyArray_FailUnlessWriteable(
1422 (PyArrayObject *)tmp, "PEP 3118 buffer") < 0) {
1423 Py_DECREF(tmp);
1424 return NULL;
1425 }
1426
1427 return tmp;
1428 }
1429 }
1430
1431 /*
1432 * If op supports the __array_struct__ or __array_interface__ interface.
1433 */
1434 tmp = PyArray_FromStructInterface(op);
1435 if (tmp == NULL) {
1436 return NULL;
1437 }
1438 if (tmp == Py_NotImplemented) {
1439 /* Until the return, NotImplemented is always a borrowed reference*/
1440 tmp = PyArray_FromInterface(op);
1441 if (tmp == NULL) {
1442 return NULL;
1443 }
1444 }
1445
1446 /*
1447 * If op supplies the __array__ function.
1448 * The documentation says this should produce a copy, so
1449 * we skip this method if writeable is true, because the intent
1450 * of writeable is to modify the operand.
1451 * XXX: If the implementation is wrong, and/or if actual
1452 * usage requires this behave differently,
1453 * this should be changed!

Calls 5

_array_from_buffer_3118Function · 0.85
PyArray_FromInterfaceFunction · 0.85

Tested by

no test coverage detected