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

Function PyArray_DescrNewByteorder

numpy/core/src/multiarray/descriptor.c:3175–3262  ·  view source on GitHub ↗

NUMPY_API * * returns a copy of the PyArray_Descr structure with the byteorder * altered: * no arguments: The byteorder is swapped (in all subfields as well) * single argument: The byteorder is forced to the given state * (in all subfields as well) * * Valid states: ('big', '>') or ('little' or '<') * ('native', or '=') * * If a descr structure with | is encountered it's own * byte-

Source from the content-addressed store, hash-verified

3173 * *** Leaves reference count of self unchanged --- does not DECREF self ***
3174 */
3175NPY_NO_EXPORT PyArray_Descr *
3176PyArray_DescrNewByteorder(PyArray_Descr *self, char newendian)
3177{
3178 PyArray_Descr *new;
3179 char endian;
3180
3181 new = PyArray_DescrNew(self);
3182 if (new == NULL) {
3183 return NULL;
3184 }
3185 endian = new->byteorder;
3186 if (endian != NPY_IGNORE) {
3187 if (newendian == NPY_SWAP) {
3188 /* swap byteorder */
3189 if (PyArray_ISNBO(endian)) {
3190 endian = NPY_OPPBYTE;
3191 }
3192 else {
3193 endian = NPY_NATBYTE;
3194 }
3195 new->byteorder = endian;
3196 }
3197 else if (newendian != NPY_IGNORE) {
3198 new->byteorder = newendian;
3199 }
3200 }
3201 if (PyDataType_HASFIELDS(new)) {
3202 PyObject *newfields;
3203 PyObject *key, *value;
3204 PyObject *newvalue;
3205 PyObject *old;
3206 PyArray_Descr *newdescr;
3207 Py_ssize_t pos = 0;
3208 int len, i;
3209
3210 newfields = PyDict_New();
3211 if (newfields == NULL) {
3212 Py_DECREF(new);
3213 return NULL;
3214 }
3215 /* make new dictionary with replaced PyArray_Descr Objects */
3216 while (PyDict_Next(self->fields, &pos, &key, &value)) {
3217 if (NPY_TITLE_KEY(key, value)) {
3218 continue;
3219 }
3220 if (!PyUnicode_Check(key) || !PyTuple_Check(value) ||
3221 ((len=PyTuple_GET_SIZE(value)) < 2)) {
3222 continue;
3223 }
3224 old = PyTuple_GET_ITEM(value, 0);
3225 if (!PyArray_DescrCheck(old)) {
3226 continue;
3227 }
3228 newdescr = PyArray_DescrNewByteorder(
3229 (PyArray_Descr *)old, newendian);
3230 if (newdescr == NULL) {
3231 Py_DECREF(newfields); Py_DECREF(new);
3232 return NULL;

Callers 12

array_newbyteorderFunction · 0.85
PyArray_ArangeObjFunction · 0.85
ensure_native_byteorderFunction · 0.85
_new_sortlikeFunction · 0.85
_new_argsortlikeFunction · 0.85
arraydescr_newbyteorderFunction · 0.85
npyiter_get_common_dtypeFunction · 0.85
PyArray_ClipFunction · 0.85

Calls 1

PyArray_DescrNewFunction · 0.85

Tested by

no test coverage detected