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

Function PyArray_FromString

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

NUMPY_API * * Given a pointer to a string ``data``, a string length ``slen``, and * a ``PyArray_Descr``, return an array corresponding to the data * encoded in that string. * * If the dtype is NULL, the default array type is used (double). * If non-null, the reference is stolen. * * If ``slen`` is < 0, then the end of string is used for text data. * It is an error for ``slen`` to be < 0

Source from the content-addressed store, hash-verified

3701 * for whitespace around the separator is added.
3702 */
3703NPY_NO_EXPORT PyObject *
3704PyArray_FromString(char *data, npy_intp slen, PyArray_Descr *dtype,
3705 npy_intp num, char *sep)
3706{
3707 int itemsize;
3708 PyArrayObject *ret;
3709 npy_bool binary;
3710
3711 if (dtype == NULL) {
3712 dtype=PyArray_DescrFromType(NPY_DEFAULT_TYPE);
3713 if (dtype == NULL) {
3714 return NULL;
3715 }
3716 }
3717 if (PyDataType_FLAGCHK(dtype, NPY_ITEM_IS_POINTER) ||
3718 PyDataType_REFCHK(dtype)) {
3719 PyErr_SetString(PyExc_ValueError,
3720 "Cannot create an object array from" \
3721 " a string");
3722 Py_DECREF(dtype);
3723 return NULL;
3724 }
3725 itemsize = dtype->elsize;
3726 if (itemsize == 0) {
3727 PyErr_SetString(PyExc_ValueError, "zero-valued itemsize");
3728 Py_DECREF(dtype);
3729 return NULL;
3730 }
3731
3732 binary = ((sep == NULL) || (strlen(sep) == 0));
3733 if (binary) {
3734 if (num < 0 ) {
3735 if (slen % itemsize != 0) {
3736 PyErr_SetString(PyExc_ValueError,
3737 "string size must be a "\
3738 "multiple of element size");
3739 Py_DECREF(dtype);
3740 return NULL;
3741 }
3742 num = slen/itemsize;
3743 }
3744 else {
3745 if (slen < num*itemsize) {
3746 PyErr_SetString(PyExc_ValueError,
3747 "string is smaller than " \
3748 "requested size");
3749 Py_DECREF(dtype);
3750 return NULL;
3751 }
3752 }
3753 /*
3754 * NewFromDescr may replace dtype to absorb subarray shape
3755 * into the array, so get size beforehand.
3756 */
3757 npy_intp size_to_copy = num*dtype->elsize;
3758 ret = (PyArrayObject *)
3759 PyArray_NewFromDescr(&PyArray_Type, dtype,
3760 1, &num, NULL, NULL,

Callers 1

array_fromstringFunction · 0.85

Calls 3

PyArray_NewFromDescrFunction · 0.85
PyArray_DATAFunction · 0.85
array_from_textFunction · 0.85

Tested by

no test coverage detected