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

Function PyArray_ToString

numpy/core/src/multiarray/convert.c:323–382  ·  view source on GitHub ↗

NUMPY_API*/

Source from the content-addressed store, hash-verified

321
322/*NUMPY_API*/
323NPY_NO_EXPORT PyObject *
324PyArray_ToString(PyArrayObject *self, NPY_ORDER order)
325{
326 npy_intp numbytes;
327 npy_intp i;
328 char *dptr;
329 int elsize;
330 PyObject *ret;
331 PyArrayIterObject *it;
332
333 if (order == NPY_ANYORDER)
334 order = PyArray_ISFORTRAN(self) ? NPY_FORTRANORDER : NPY_CORDER;
335
336 /* if (PyArray_TYPE(self) == NPY_OBJECT) {
337 PyErr_SetString(PyExc_ValueError, "a string for the data" \
338 "in an object array is not appropriate");
339 return NULL;
340 }
341 */
342
343 numbytes = PyArray_NBYTES(self);
344 if ((PyArray_IS_C_CONTIGUOUS(self) && (order == NPY_CORDER))
345 || (PyArray_IS_F_CONTIGUOUS(self) && (order == NPY_FORTRANORDER))) {
346 ret = PyBytes_FromStringAndSize(PyArray_DATA(self), (Py_ssize_t) numbytes);
347 }
348 else {
349 PyObject *new;
350 if (order == NPY_FORTRANORDER) {
351 /* iterators are always in C-order */
352 new = PyArray_Transpose(self, NULL);
353 if (new == NULL) {
354 return NULL;
355 }
356 }
357 else {
358 Py_INCREF(self);
359 new = (PyObject *)self;
360 }
361 it = (PyArrayIterObject *)PyArray_IterNew(new);
362 Py_DECREF(new);
363 if (it == NULL) {
364 return NULL;
365 }
366 ret = PyBytes_FromStringAndSize(NULL, (Py_ssize_t) numbytes);
367 if (ret == NULL) {
368 Py_DECREF(it);
369 return NULL;
370 }
371 dptr = PyBytes_AS_STRING(ret);
372 i = it->size;
373 elsize = PyArray_DESCR(self)->elsize;
374 while (i--) {
375 memcpy(dptr, it->dataptr, elsize);
376 dptr += elsize;
377 PyArray_ITER_NEXT(it);
378 }
379 Py_DECREF(it);
380 }

Callers 3

array_tobytesFunction · 0.85
array_tostringFunction · 0.85
array_reduceFunction · 0.85

Calls 4

PyArray_DATAFunction · 0.85
PyArray_TransposeFunction · 0.85
PyArray_IterNewFunction · 0.85
PyArray_DESCRFunction · 0.85

Tested by

no test coverage detected