From a24e785ead6dbd80050cb157326a6a23b279d4e4 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Fri, 10 Mar 2023 08:21:19 -0700 Subject: ENH: allow using dtype classes in array creation functions This enables writing np.array(some_object, dtype=type(np.dtype('i'))). This is a follow-on from https://github.com/numpy/numpy/pull/23154, see that PR for more details. I had to add a new include to `ctors.h` to bring in the definition of the `npy_dtype_info` struct. Since `ctors.h` is included in many other files inside numpy, I found that I needed to modify fewer includes across numpy if I moved the definition of `npy_dtype_info` to `common.h` from `descriptor.h`. The new includes of `common.h` are needed to support later includes of `ctors.h` in those files. If anyone has an alternate place to put `npy_dtype_info` that would cause less churn of includes I'd love to hear about it. I spent a bunch of time tweaking the reference counts. I'm reasonably confident this is correct but not 100%, an additional careful pass over the reference count logic from a reviewer would be very appreciated. I could have made `_PyArray_FromAny` and `_PyArray_CheckFromAny` take just a `npy_dtype_info` struct, but I found it made the reference count logic more complicated, since `PyArray_FromAny` and `PyArray_CheckFromAny` steal the reference to the descriptor they are passed and I needed to conserve that behavior. Also both functions support passing in a `NULL` pointer for the descriptor and I needed to maintain that behavior as well. The change to `ucsnarrow.h` fixes a preexisting conflict with the prototype in `ucsnarrow.c` that triggered a compiler error while I was working on this. --- doc/source/reference/c-api/array.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'doc/source/reference/c-api') diff --git a/doc/source/reference/c-api/array.rst b/doc/source/reference/c-api/array.rst index 6651a4760..65244f69e 100644 --- a/doc/source/reference/c-api/array.rst +++ b/doc/source/reference/c-api/array.rst @@ -423,7 +423,7 @@ From other objects :c:data:`NPY_ARRAY_FORCECAST` is present in ``flags``, this call will generate an error if the data type cannot be safely obtained from the object. If you want to use - ``NULL`` for the *dtype* and ensure the array is notswapped then + ``NULL`` for the *dtype* and ensure the array is not swapped then use :c:func:`PyArray_CheckFromAny`. A value of 0 for either of the depth parameters causes the parameter to be ignored. Any of the following array flags can be added (*e.g.* using \|) to get the -- cgit v1.2.1