10000 MAINT: Fix pickling/unpickling for unsigned flags · numpy/numpy@237524d · GitHub
[go: up one dir, main page]

Skip to content

Commit 237524d

Browse files
committed
MAINT: Fix pickling/unpickling for unsigned flags
Note that the (rare) aligned struct case will not allow unpickling NumPy 2.x files with NumPy 1.x. This could be added if necessary. Unpickling 1.x files in 2.x is unproblematic
1 parent 3b418c6 commit 237524d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

numpy/_core/src/multiarray/descriptor.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2785,7 +2785,7 @@ arraydescr_reduce(PyArray_Descr *self, PyObject *NPY_UNUSED(args))
27852785
}
27862786
PyTuple_SET_ITEM(state, 5, PyLong_FromLong(elsize));
27872787
PyTuple_SET_ITEM(state, 6, PyLong_FromLong(alignment));
2788-
PyTuple_SET_ITEM(state, 7, PyLong_FromLong(self->flags));
2788+
PyTuple_SET_ITEM(state, 7, PyLong_FromUnsignedLongLong(self->flags));
27892789

27902790
PyTuple_SET_ITEM(ret, 2, state);
27912791
return ret;
@@ -2840,7 +2840,7 @@ arraydescr_setstate(_PyArray_LegacyDescr *self, PyObject *args)
28402840
PyObject *subarray, *fields, *names = NULL, *metadata=NULL;
28412841
int incref_names = 1;
28422842
int int_dtypeflags = 0;
2843-
char dtypeflags;
2843+
npy_uint64 dtypeflags;
28442844

28452845
if (!PyDataType_ISLEGACY(self)) {
28462846
PyErr_SetString(PyExc_RuntimeError,
@@ -3140,6 +3140,10 @@ arraydescr_setstate(_PyArray_LegacyDescr *self, PyObject *args)
31403140
* flags as an int even though it actually was a char in the PyArray_Descr
31413141
* structure
31423142
*/
3143+
if (int_dtypeflags < 0 && int_dtypeflags >= -128) {
3144+
/* NumPy used to use a char. So normalize if signed. */
3145+
int_dtypeflags += 128;
3146+
}
31433147
dtypeflags = int_dtypeflags;
31443148
if (dtypeflags != int_dtypeflags) {
31453149
PyErr_Format(PyExc_ValueError,

0 commit comments

Comments
 (0)
0