summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorNathan Goldbaum <nathan.goldbaum@gmail.com>2023-02-23 11:39:49 -0700
committerNathan Goldbaum <nathan.goldbaum@gmail.com>2023-02-24 12:00:44 -0700
commita22e1614e5fed9596a60600ad1c5dc9562beff19 (patch)
tree82cfe6d3ced9851c99ba2475d2f363b55c063424 /numpy
parent41b5688be4ed232599f0a0ed24202892e6a049e0 (diff)
downloadnumpy-a22e1614e5fed9596a60600ad1c5dc9562beff19.tar.gz
MAINT: initialize non-legacy REFCHK arrays outside PyArray_FillObjectArray
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/ctors.c31
-rw-r--r--numpy/core/src/multiarray/refcount.c7
2 files changed, 27 insertions, 11 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 38af60427..79910ada8 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -1076,11 +1076,32 @@ PyArray_NewLikeArrayWithShape(PyArrayObject *prototype, NPY_ORDER order,
}
/* Logic shared by `empty`, `empty_like`, and `ndarray.__new__` */
- if (PyDataType_REFCHK(PyArray_DESCR((PyArrayObject *)ret))) {
- PyArray_FillObjectArray((PyArrayObject *)ret, Py_None);
- if (PyErr_Occurred()) {
- Py_DECREF(ret);
- return NULL;
+ PyArray_Descr* descr = PyArray_DESCR((PyArrayObject *)ret);
+ if (PyDataType_REFCHK(descr)) {
+ if (NPY_DT_is_legacy(NPY_DTYPE(dtype))) {
+ PyArray_FillObjectArray((PyArrayObject *)ret, Py_None);
+ if (PyErr_Occurred()) {
+ Py_DECREF(ret);
+ return NULL;
+ }
+ }
+ else {
+ // Currently we assume all non-legacy dtypes that have with the
+ // NPY_ITEM_REFCOUNT flag either represent heap-allocated dtypes or
+ // represent python objects. In the latter case the dtype is
+ // responsible for managing initialization and reference counts. In
+ // both cases initializing to NULL makes sense.
+ //
+ // In the future we might adjust this and have separate logic for
+ // new dtypes that hold heap-allocated data and new dtypes that hold
+ // python objects, particularly if we want to allow releasing the
+ // GIL in the former case.
+ char *optr = PyArray_DATA((PyArrayObject*)ret);
+ npy_intp n = PyArray_SIZE((PyArrayObject*)ret);
+ for (npy_intp i = 0; i < n; i++) {
+ optr = NULL;
+ optr += descr->elsize;
+ }
}
}
diff --git a/numpy/core/src/multiarray/refcount.c b/numpy/core/src/multiarray/refcount.c
index d78b5b7f5..e735e3b8f 100644
--- a/numpy/core/src/multiarray/refcount.c
+++ b/numpy/core/src/multiarray/refcount.c
@@ -444,12 +444,7 @@ _fillobject(char *optr, PyObject *obj, PyArray_Descr *dtype)
optr += inner_elsize;
}
}
- else if (PyDataType_FLAGCHK(dtype, NPY_ITEM_IS_POINTER))
- {
- optr = NULL;
- }
- else
- {
+ else {
/* This path should not be reachable. */
assert(0);
}