Commit 57242de5 authored by Karl Wette's avatar Karl Wette
Browse files

Fixes to "Fix Numpy 1.20.0 by initializing PyArray_Descr portably"

- 'pdescr' must be derived from NPY_VOID. The C arrays being viewed contain
  arbitrary C data (which are wrapped by Python objects on the fly), not the
  Python objects themselves (for which NPY_OBJECT would be used).
- Restore flags and other settings from the static PyArray_Descr
  initialisation. Some (e.g. 'byteorder') likely already have sensible defaults,
  but better safe than sorry.
- Remove 'NPY_ITEM_IS_POINTER' from 'flags'. It is essentially deprecated by
  Numpy 1.20.0, and in any case appears not to be needed, as the logic of what
  elements are "pointers" is already handled by SWIG. Since we don't need this
  flag, we also don't need the hard-coded 'names' and 'fields'. (I imagine this
  would break a number of things by forcing access to SWIG objects through the
  'swigobj' field, but never got this working so couldn't test it out.)
- Restore the 'Py_INCREF(*pdescr)' before returning 'pdescr'. This is CRITICAL
  otherwise, ...
parent e60ee429
Pipeline #187926 failed with stages
in 215 minutes and 44 seconds
......@@ -665,26 +665,30 @@ SWIGINTERN bool swiglal_release_parent(void *ptr) {
// Create NumPy array descriptor if none yet exists.
if (*pdescr == NULL) {
*pdescr = PyArray_DescrNewFromType(NPY_OBJECT);
*pdescr = PyArray_DescrNewFromType(NPY_VOID);
if (*pdescr == NULL) {
return NULL;
}
(*pdescr)->typeobj = SwigPyObject_type();
(*pdescr)->byteorder = '=';
(*pdescr)->flags = NPY_LIST_PICKLE | NPY_NEEDS_INIT | NPY_NEEDS_PYAPI | NPY_USE_GETITEM | NPY_USE_SETITEM;
(*pdescr)->type_num = 0;
(*pdescr)->elsize = esize;
(*pdescr)->alignment = 1;
(*pdescr)->subarray = NULL;
(*pdescr)->names = NULL;
(*pdescr)->fields = NULL;
(*pdescr)->f = &swiglal_py_array_objview_##ACFTYPE##_arrfuncs;
// Numpy workaround for registering user-defined object types. See
// https://github.com/numpy/numpy/pull/18303,
// https://github.com/numpy/numpy/pull/17320
(*pdescr)->names = Py_BuildValue("(s)", "swigobj");
(*pdescr)->fields = Py_BuildValue("{s:(s,i)}", "swigobj", "O", 0);
if (PyArray_RegisterDataType(*pdescr) < 0) {
return NULL;
}
}
// PyArray_NewFromDescr appears to steal a reference to the descriptor passed to it, so a
// reference count increment is needed here.
Py_INCREF(*pdescr);
return *pdescr;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment