* Fill in str with an appropriate PEP 3118 format string, based on * descr. For structured dtypes, calls itself recursively. Each call extends * str at offset then updates offset, and uses descr->byteorder, (and * possibly the byte order in obj) to determine the byte-order char. * * Returns 0 for success, -1 for failure */
| 188 | * Returns 0 for success, -1 for failure |
| 189 | */ |
| 190 | static int |
| 191 | _buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str, |
| 192 | PyObject* obj, Py_ssize_t *offset, |
| 193 | char *active_byteorder) |
| 194 | { |
| 195 | int k; |
| 196 | char _active_byteorder = '@'; |
| 197 | Py_ssize_t _offset = 0; |
| 198 | |
| 199 | if (active_byteorder == NULL) { |
| 200 | active_byteorder = &_active_byteorder; |
| 201 | } |
| 202 | if (offset == NULL) { |
| 203 | offset = &_offset; |
| 204 | } |
| 205 | |
| 206 | if (descr->subarray) { |
| 207 | PyObject *item, *subarray_tuple; |
| 208 | Py_ssize_t total_count = 1; |
| 209 | Py_ssize_t dim_size; |
| 210 | Py_ssize_t old_offset; |
| 211 | char buf[128]; |
| 212 | int ret; |
| 213 | |
| 214 | if (PyTuple_Check(descr->subarray->shape)) { |
| 215 | subarray_tuple = descr->subarray->shape; |
| 216 | Py_INCREF(subarray_tuple); |
| 217 | } |
| 218 | else { |
| 219 | subarray_tuple = Py_BuildValue("(O)", descr->subarray->shape); |
| 220 | } |
| 221 | |
| 222 | if (_append_char(str, '(') < 0) { |
| 223 | ret = -1; |
| 224 | goto subarray_fail; |
| 225 | } |
| 226 | for (k = 0; k < PyTuple_GET_SIZE(subarray_tuple); ++k) { |
| 227 | if (k > 0) { |
| 228 | if (_append_char(str, ',') < 0) { |
| 229 | ret = -1; |
| 230 | goto subarray_fail; |
| 231 | } |
| 232 | } |
| 233 | item = PyTuple_GET_ITEM(subarray_tuple, k); |
| 234 | dim_size = PyNumber_AsSsize_t(item, NULL); |
| 235 | |
| 236 | PyOS_snprintf(buf, sizeof(buf), "%ld", (long)dim_size); |
| 237 | if (_append_str(str, buf) < 0) { |
| 238 | ret = -1; |
| 239 | goto subarray_fail; |
| 240 | } |
| 241 | total_count *= dim_size; |
| 242 | } |
| 243 | if (_append_char(str, ')') < 0) { |
| 244 | ret = -1; |
| 245 | goto subarray_fail; |
| 246 | } |
| 247 |
no test coverage detected