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

Function convert_shape_to_string

numpy/core/src/multiarray/common.c:197–245  ·  view source on GitHub ↗

* Convert an array shape to a string such as "(1, 2)". * * @param Dimensionality of the shape * @param npy_intp pointer to shape array * @param String to append after the shape `(1, 2)%s`. * * @return Python unicode string */

Source from the content-addressed store, hash-verified

195 * @return Python unicode string
196 */
197NPY_NO_EXPORT PyObject *
198convert_shape_to_string(npy_intp n, npy_intp const *vals, char *ending)
199{
200 npy_intp i;
201
202 /*
203 * Negative dimension indicates "newaxis", which can
204 * be discarded for printing if it's a leading dimension.
205 * Find the first non-"newaxis" dimension.
206 */
207 for (i = 0; i < n && vals[i] < 0; i++);
208
209 if (i == n) {
210 return PyUnicode_FromFormat("()%s", ending);
211 }
212
213 PyObject *ret = PyUnicode_FromFormat("%" NPY_INTP_FMT, vals[i++]);
214 if (ret == NULL) {
215 return NULL;
216 }
217 for (; i < n; ++i) {
218 PyObject *tmp;
219
220 if (vals[i] < 0) {
221 tmp = PyUnicode_FromString(",newaxis");
222 }
223 else {
224 tmp = PyUnicode_FromFormat(",%" NPY_INTP_FMT, vals[i]);
225 }
226 if (tmp == NULL) {
227 Py_DECREF(ret);
228 return NULL;
229 }
230
231 Py_SETREF(ret, PyUnicode_Concat(ret, tmp));
232 Py_DECREF(tmp);
233 if (ret == NULL) {
234 return NULL;
235 }
236 }
237
238 if (i == 1) {
239 Py_SETREF(ret, PyUnicode_FromFormat("(%S,)%s", ret, ending));
240 }
241 else {
242 Py_SETREF(ret, PyUnicode_FromFormat("(%S)%s", ret, ending));
243 }
244 return ret;
245}
246
247
248NPY_NO_EXPORT void

Callers 6

dot_alignment_errorFunction · 0.85
npyiter_fill_axisdataFunction · 0.85
mapiter_fill_infoFunction · 0.85
PyArray_MapIterNewFunction · 0.85
broadcast_stridesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected