Skip to content

Commit 529fd40

Browse files
authored
Merge pull request numpy#26063 from seberg/const-qualify-new-api
MAINT,API: Const qualify some new API (mostly new DType API)
2 parents c9fc389 + 550f7d7 commit 529fd40

24 files changed

+171
-160
lines changed

doc/source/reference/c-api/array.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,8 +1682,8 @@ the functions that must be implemented for each slot.
16821682
16831683
.. c:type:: NPY_CASTING (PyArrayMethod_ResolveDescriptors)( \
16841684
struct PyArrayMethodObject_tag *method, \
1685-
PyArray_DTypeMeta **dtypes, \
1686-
PyArray_Descr **given_descrs, \
1685+
PyArray_DTypeMeta *const *dtypes, \
1686+
PyArray_Descr *const *given_descrs, \
16871687
PyArray_Descr **loop_descrs, \
16881688
npy_intp *view_offset)
16891689
@@ -1857,7 +1857,7 @@ Typedefs for functions that users of the ArrayMethod API can implement are
18571857
described below.
18581858
18591859
.. c:type:: int (PyArrayMethod_TraverseLoop)( \
1860-
void *traverse_context, PyArray_Descr *descr, char *data, \
1860+
void *traverse_context, const PyArray_Descr *descr, char *data, \
18611861
npy_intp size, npy_intp stride, NpyAuxData *auxdata)
18621862
18631863
A traverse loop working on a single array. This is similar to the general
@@ -1880,7 +1880,7 @@ described below.
18801880
passed through in the future (for structured dtypes).
18811881
18821882
.. c:type:: int (PyArrayMethod_GetTraverseLoop)( \
1883-
void *traverse_context, PyArray_Descr *descr, \
1883+
void *traverse_context, const PyArray_Descr *descr, \
18841884
int aligned, npy_intp fixed_stride, \
18851885
PyArrayMethod_TraverseLoop **out_loop, NpyAuxData **out_auxdata, \
18861886
NPY_ARRAYMETHOD_FLAGS *flags)
@@ -1920,7 +1920,8 @@ with the rest of the ArrayMethod API.
19201920
attempt a new search for a matching loop/promoter.
19211921
19221922
.. c:type:: int (PyArrayMethod_PromoterFunction)(PyObject *ufunc, \
1923-
PyArray_DTypeMeta *op_dtypes[], PyArray_DTypeMeta *signature[], \
1923+
PyArray_DTypeMeta *const op_dtypes[], \
1924+
PyArray_DTypeMeta *const signature[], \
19241925
PyArray_DTypeMeta *new_op_dtypes[])
19251926
19261927
Type of the promoter function, which must be wrapped into a
@@ -3386,7 +3387,7 @@ Data Type Promotion and Inspection
33863387
----------------------------------
33873388
33883389
.. c:function:: PyArray_DTypeMeta *PyArray_CommonDType( \
3389-
PyArray_DTypeMeta *dtype1, PyArray_DTypeMeta *dtype2)
3390+
const PyArray_DTypeMeta *dtype1, const PyArray_DTypeMeta *dtype2)
33903391
33913392
This function defines the common DType operator. Note that the common DType
33923393
will not be ``object`` (unless one of the DTypes is ``object``). Similar to
@@ -3413,7 +3414,7 @@ Data Type Promotion and Inspection
34133414
For example promoting ``float16`` with any other float, integer, or unsigned
34143415
integer again gives a floating point number.
34153416
3416-
.. c:function:: PyArray_Descr *PyArray_GetDefaultDescr(PyArray_DTypeMeta *DType)
3417+
.. c:function:: PyArray_Descr *PyArray_GetDefaultDescr(const PyArray_DTypeMeta *DType)
34173418
34183419
Given a DType class, returns the default instance (descriptor). This checks
34193420
for a ``singleton`` first and only calls the ``default_descr`` function if

doc/source/reference/c-api/types-and-structures.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ PyArrayMethod_Context and PyArrayMethod_Spec
728728
typedef struct {
729729
PyObject *caller;
730730
struct PyArrayMethodObject_tag *method;
731-
PyArray_Descr **descriptors;
731+
PyArray_Descr *const *descriptors;
732732
} PyArrayMethod_Context
733733
734734
.. c:member:: PyObject *caller

numpy/_core/include/numpy/dtype_api.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ typedef struct PyArrayMethod_Context_tag {
106106
struct PyArrayMethodObject_tag *method;
107107

108108
/* Operand descriptors, filled in by resolve_descriptors */
109-
PyArray_Descr **descriptors;
109+
PyArray_Descr *const *descriptors;
110110
/* Structure may grow (this is harmless for DType authors) */
111111
} PyArrayMethod_Context;
112112

@@ -159,9 +159,9 @@ typedef NPY_CASTING (PyArrayMethod_ResolveDescriptors)(
159159
/* "method" is currently opaque (necessary e.g. to wrap Python) */
160160
struct PyArrayMethodObject_tag *method,
161161
/* DTypes the method was created for */
162-
PyArray_DTypeMeta **dtypes,
162+
PyArray_DTypeMeta *const *dtypes,
163163
/* Input descriptors (instances). Outputs may be NULL. */
164-
PyArray_Descr **given_descrs,
164+
PyArray_Descr *const *given_descrs,
165165
/* Exact loop descriptors to use, must not hold references on error */
166166
PyArray_Descr **loop_descrs,
167167
npy_intp *view_offset);
@@ -177,9 +177,9 @@ typedef NPY_CASTING (PyArrayMethod_ResolveDescriptors)(
177177
*/
178178
typedef NPY_CASTING (PyArrayMethod_ResolveDescriptorsWithScalar)(
179179
struct PyArrayMethodObject_tag *method,
180-
PyArray_DTypeMeta **dtypes,
180+
PyArray_DTypeMeta *const *dtypes,
181181
/* Unlike above, these can have any DType and we may allow NULL. */
182-
PyArray_Descr **given_descrs,
182+
PyArray_Descr *const *given_descrs,
183183
/*
184184
* Input scalars or NULL. Only ever passed for python scalars.
185185
* WARNING: In some cases, a loop may be explicitly selected and the
@@ -227,7 +227,7 @@ typedef int (PyArrayMethod_GetLoop)(
227227
*/
228228
typedef int (PyArrayMethod_GetReductionInitial)(
229229
PyArrayMethod_Context *context, npy_bool reduction_is_empty,
230-
char *initial);
230+
void *initial);
231231

232232
/*
233233
* The following functions are only used by the wrapping array method defined
@@ -256,8 +256,8 @@ typedef int (PyArrayMethod_GetReductionInitial)(
256256
* `resolve_descriptors`, so that it can be filled there if not NULL.)
257257
*/
258258
typedef int (PyArrayMethod_TranslateGivenDescriptors)(int nin, int nout,
259-
PyArray_DTypeMeta *wrapped_dtypes[],
260-
PyArray_Descr *given_descrs[], PyArray_Descr *new_descrs[]);
259+
PyArray_DTypeMeta *const wrapped_dtypes[],
260+
PyArray_Descr *const given_descrs[], PyArray_Descr *new_descrs[]);
261261

262262
/**
263263
* The function to convert the actual loop descriptors (as returned by the
@@ -278,7 +278,7 @@ typedef int (PyArrayMethod_TranslateGivenDescriptors)(int nin, int nout,
278278
* @returns 0 on success, -1 on failure.
279279
*/
280280
typedef int (PyArrayMethod_TranslateLoopDescriptors)(int nin, int nout,
281-
PyArray_DTypeMeta *new_dtypes[], PyArray_Descr *given_descrs[],
281+
PyArray_DTypeMeta *const new_dtypes[], PyArray_Descr *const given_descrs[],
282282
PyArray_Descr *original_descrs[], PyArray_Descr *loop_descrs[]);
283283

284284

@@ -303,7 +303,7 @@ typedef int (PyArrayMethod_TranslateLoopDescriptors)(int nin, int nout,
303303
*
304304
*/
305305
typedef int (PyArrayMethod_TraverseLoop)(
306-
void *traverse_context, PyArray_Descr *descr, char *data,
306+
void *traverse_context, const PyArray_Descr *descr, char *data,
307307
npy_intp size, npy_intp stride, NpyAuxData *auxdata);
308308

309309

@@ -317,7 +317,7 @@ typedef int (PyArrayMethod_TraverseLoop)(
317317
*
318318
*/
319319
typedef int (PyArrayMethod_GetTraverseLoop)(
320-
void *traverse_context, PyArray_Descr *descr,
320+
void *traverse_context, const PyArray_Descr *descr,
321321
int aligned, npy_intp fixed_stride,
322322
PyArrayMethod_TraverseLoop **out_loop, NpyAuxData **out_auxdata,
323323
NPY_ARRAYMETHOD_FLAGS *flags);
@@ -334,7 +334,7 @@ typedef int (PyArrayMethod_GetTraverseLoop)(
334334
* (There are potential use-cases, these are currently unsupported.)
335335
*/
336336
typedef int (PyArrayMethod_PromoterFunction)(PyObject *ufunc,
337-
PyArray_DTypeMeta *op_dtypes[], PyArray_DTypeMeta *signature[],
337+
PyArray_DTypeMeta *const op_dtypes[], PyArray_DTypeMeta *const signature[],
338338
PyArray_DTypeMeta *new_op_dtypes[]);
339339

340340
/*

numpy/_core/include/numpy/npy_2_compat.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,19 @@ DESCR_ACCESSOR(C_METADATA, c_metadata, NpyAuxData *, 1)
220220
#if !(defined(NPY_INTERNAL_BUILD) && NPY_INTERNAL_BUILD)
221221
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
222222
static inline PyArray_ArrFuncs *
223-
PyDataType_GetArrFuncs(PyArray_Descr *descr)
223+
PyDataType_GetArrFuncs(const PyArray_Descr *descr)
224224
{
225225
return _PyDataType_GetArrFuncs(descr);
226226
}
227227
#elif NPY_ABI_VERSION < 0x02000000
228228
static inline PyArray_ArrFuncs *
229-
PyDataType_GetArrFuncs(PyArray_Descr *descr)
229+
PyDataType_GetArrFuncs(const PyArray_Descr *descr)
230230
{
231231
return descr->f;
232232
}
233233
#else
234234
static inline PyArray_ArrFuncs *
235-
PyDataType_GetArrFuncs(PyArray_Descr *descr)
235+
PyDataType_GetArrFuncs(const PyArray_Descr *descr)
236236
{
237237
if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) {
238238
return _PyDataType_GetArrFuncs(descr);

numpy/_core/src/multiarray/array_method.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
static NPY_CASTING
6060
default_resolve_descriptors(
6161
PyArrayMethodObject *method,
62-
PyArray_DTypeMeta **dtypes,
63-
PyArray_Descr **input_descrs,
62+
PyArray_DTypeMeta *const *dtypes,
63+
PyArray_Descr *const *input_descrs,
6464
PyArray_Descr **output_descrs,
6565
npy_intp *view_offset)
6666
{
@@ -139,7 +139,7 @@ npy_default_get_strided_loop(
139139
PyArrayMethod_StridedLoop **out_loop, NpyAuxData **out_transferdata,
140140
NPY_ARRAYMETHOD_FLAGS *flags)
141141
{
142-
PyArray_Descr **descrs = context->descriptors;
142+
PyArray_Descr *const *descrs = context->descriptors;
143143
PyArrayMethodObject *meth = context->method;
144144
*flags = meth->flags & NPY_METH_RUNTIME_FLAGS;
145145
*out_transferdata = NULL;

numpy/_core/src/multiarray/convert_datatype.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,8 +2440,8 @@ PyArray_AddCastingImplementation_FromSpec(PyArrayMethod_Spec *spec, int private)
24402440
NPY_NO_EXPORT NPY_CASTING
24412441
legacy_same_dtype_resolve_descriptors(
24422442
PyArrayMethodObject *NPY_UNUSED(self),
2443-
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]),
2444-
PyArray_Descr *given_descrs[2],
2443+
PyArray_DTypeMeta *const NPY_UNUSED(dtypes[2]),
2444+
PyArray_Descr *const given_descrs[2],
24452445
PyArray_Descr *loop_descrs[2],
24462446
npy_intp *view_offset)
24472447
{
@@ -2483,7 +2483,7 @@ legacy_cast_get_strided_loop(
24832483
PyArrayMethod_StridedLoop **out_loop, NpyAuxData **out_transferdata,
24842484
NPY_ARRAYMETHOD_FLAGS *flags)
24852485
{
2486-
PyArray_Descr **descrs = context->descriptors;
2486+
PyArray_Descr *const *descrs = context->descriptors;
24872487
int out_needs_api = 0;
24882488

24892489
*flags = context->method->flags & NPY_METH_RUNTIME_FLAGS;
@@ -2507,8 +2507,8 @@ legacy_cast_get_strided_loop(
25072507
NPY_NO_EXPORT NPY_CASTING
25082508
simple_cast_resolve_descriptors(
25092509
PyArrayMethodObject *self,
2510-
PyArray_DTypeMeta *dtypes[2],
2511-
PyArray_Descr *given_descrs[2],
2510+
PyArray_DTypeMeta *const dtypes[2],
2511+
PyArray_Descr *const given_descrs[2],
25122512
PyArray_Descr *loop_descrs[2],
25132513
npy_intp *view_offset)
25142514
{
@@ -2548,7 +2548,7 @@ get_byteswap_loop(
25482548
PyArrayMethod_StridedLoop **out_loop, NpyAuxData **out_transferdata,
25492549
NPY_ARRAYMETHOD_FLAGS *flags)
25502550
{
2551-
PyArray_Descr **descrs = context->descriptors;
2551+
PyArray_Descr *const *descrs = context->descriptors;
25522552
assert(descrs[0]->kind == descrs[1]->kind);
25532553
assert(descrs[0]->elsize == descrs[1]->elsize);
25542554
int itemsize = descrs[0]->elsize;
@@ -2727,8 +2727,8 @@ PyArray_InitializeNumericCasts(void)
27272727
static int
27282728
cast_to_string_resolve_descriptors(
27292729
PyArrayMethodObject *self,
2730-
PyArray_DTypeMeta *dtypes[2],
2731-
PyArray_Descr *given_descrs[2],
2730+
PyArray_DTypeMeta *const dtypes[2],
2731+
PyArray_Descr *const given_descrs[2],
27322732
PyArray_Descr *loop_descrs[2],
27332733
npy_intp *NPY_UNUSED(view_offset))
27342734
{
@@ -2879,8 +2879,8 @@ add_other_to_and_from_string_cast(
28792879
NPY_NO_EXPORT NPY_CASTING
28802880
string_to_string_resolve_descriptors(
28812881
PyArrayMethodObject *NPY_UNUSED(self),
2882-
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]),
2883-
PyArray_Descr *given_descrs[2],
2882+
PyArray_DTypeMeta *const NPY_UNUSED(dtypes[2]),
2883+
PyArray_Descr *const given_descrs[2],
28842884
PyArray_Descr *loop_descrs[2],
28852885
npy_intp *view_offset)
28862886
{
@@ -2932,7 +2932,7 @@ string_to_string_get_loop(
29322932
NPY_ARRAYMETHOD_FLAGS *flags)
29332933
{
29342934
int unicode_swap = 0;
2935-
PyArray_Descr **descrs = context->descriptors;
2935+
PyArray_Descr *const *descrs = context->descriptors;
29362936

29372937
assert(NPY_DTYPE(descrs[0]) == NPY_DTYPE(descrs[1]));
29382938
*flags = context->method->flags & NPY_METH_RUNTIME_FLAGS;
@@ -3033,7 +3033,7 @@ PyArray_InitializeStringCasts(void)
30333033
*/
30343034
static NPY_CASTING
30353035
cast_to_void_dtype_class(
3036-
PyArray_Descr **given_descrs, PyArray_Descr **loop_descrs,
3036+
PyArray_Descr *const *given_descrs, PyArray_Descr **loop_descrs,
30373037
npy_intp *view_offset)
30383038
{
30393039
/* `dtype="V"` means unstructured currently (compare final path) */
@@ -3058,8 +3058,8 @@ cast_to_void_dtype_class(
30583058
static NPY_CASTING
30593059
nonstructured_to_structured_resolve_descriptors(
30603060
PyArrayMethodObject *NPY_UNUSED(self),
3061-
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]),
3062-
PyArray_Descr *given_descrs[2],
3061+
PyArray_DTypeMeta *const NPY_UNUSED(dtypes[2]),
3062+
PyArray_Descr *const given_descrs[2],
30633063
PyArray_Descr *loop_descrs[2],
30643064
npy_intp *view_offset)
30653065
{
@@ -3251,8 +3251,8 @@ PyArray_GetGenericToVoidCastingImpl(void)
32513251
static NPY_CASTING
32523252
structured_to_nonstructured_resolve_descriptors(
32533253
PyArrayMethodObject *NPY_UNUSED(self),
3254-
PyArray_DTypeMeta *dtypes[2],
3255-
PyArray_Descr *given_descrs[2],
3254+
PyArray_DTypeMeta *const dtypes[2],
3255+
PyArray_Descr *const given_descrs[2],
32563256
PyArray_Descr *loop_descrs[2],
32573257
npy_intp *view_offset)
32583258
{
@@ -3521,8 +3521,8 @@ can_cast_fields_safety(
35213521
static NPY_CASTING
35223522
void_to_void_resolve_descriptors(
35233523
PyArrayMethodObject *self,
3524-
PyArray_DTypeMeta *dtypes[2],
3525-
PyArray_Descr *given_descrs[2],
3524+
PyArray_DTypeMeta *const dtypes[2],
3525+
PyArray_Descr *const given_descrs[2],
35263526
PyArray_Descr *loop_descrs[2],
35273527
npy_intp *view_offset)
35283528
{
@@ -3720,8 +3720,8 @@ PyArray_InitializeVoidToVoidCast(void)
37203720
static NPY_CASTING
37213721
object_to_any_resolve_descriptors(
37223722
PyArrayMethodObject *NPY_UNUSED(self),
3723-
PyArray_DTypeMeta *dtypes[2],
3724-
PyArray_Descr *given_descrs[2],
3723+
PyArray_DTypeMeta *const dtypes[2],
3724+
PyArray_Descr *const given_descrs[2],
37253725
PyArray_Descr *loop_descrs[2],
37263726
npy_intp *NPY_UNUSED(view_offset))
37273727
{
@@ -3794,8 +3794,8 @@ PyArray_GetObjectToGenericCastingImpl(void)
37943794
static NPY_CASTING
37953795
any_to_object_resolve_descriptors(
37963796
PyArrayMethodObject *NPY_UNUSED(self),
3797-
PyArray_DTypeMeta *dtypes[2],
3798-
PyArray_Descr *given_descrs[2],
3797+
PyArray_DTypeMeta *const dtypes[2],
3798+
PyArray_Descr *const given_descrs[2],
37993799
PyArray_Descr *loop_descrs[2],
38003800
npy_intp *NPY_UNUSED(view_offset))
38013801
{

numpy/_core/src/multiarray/convert_datatype.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ PyArray_CheckCastSafety(NPY_CASTING casting,
109109
NPY_NO_EXPORT NPY_CASTING
110110
legacy_same_dtype_resolve_descriptors(
111111
PyArrayMethodObject *self,
112-
PyArray_DTypeMeta *dtypes[2],
113-
PyArray_Descr *given_descrs[2],
112+
PyArray_DTypeMeta *const dtypes[2],
113+
PyArray_Descr *const given_descrs[2],
114114
PyArray_Descr *loop_descrs[2],
115115
npy_intp *view_offset);
116116

@@ -124,8 +124,8 @@ legacy_cast_get_strided_loop(
124124
NPY_NO_EXPORT NPY_CASTING
125125
simple_cast_resolve_descriptors(
126126
PyArrayMethodObject *self,
127-
PyArray_DTypeMeta *dtypes[2],
128-
PyArray_Descr *input_descrs[2],
127+
PyArray_DTypeMeta *const dtypes[2],
128+
PyArray_Descr *const input_descrs[2],
129129
PyArray_Descr *loop_descrs[2],
130130
npy_intp *view_offset);
131131

numpy/_core/src/multiarray/datetime.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3780,7 +3780,7 @@ time_to_time_get_loop(
37803780
{
37813781
int requires_wrap = 0;
37823782
int inner_aligned = aligned;
3783-
PyArray_Descr **descrs = context->descriptors;
3783+
PyArray_Descr *const *descrs = context->descriptors;
37843784
*flags = NPY_METH_NO_FLOATINGPOINT_ERRORS;
37853785

37863786
PyArray_DatetimeMetaData *meta1 = get_datetime_metadata_from_dtype(descrs[0]);
@@ -3929,7 +3929,7 @@ datetime_to_string_get_loop(
39293929
PyArrayMethod_StridedLoop **out_loop, NpyAuxData **out_transferdata,
39303930
NPY_ARRAYMETHOD_FLAGS *flags)
39313931
{
3932-
PyArray_Descr **descrs = context->descriptors;
3932+
PyArray_Descr *const *descrs = context->descriptors;
39333933
*flags = context->method->flags & NPY_METH_RUNTIME_FLAGS;
39343934

39353935
if (descrs[1]->type_num == NPY_STRING) {
@@ -3989,7 +3989,7 @@ string_to_datetime_cast_get_loop(
39893989
PyArrayMethod_StridedLoop **out_loop, NpyAuxData **out_transferdata,
39903990
NPY_ARRAYMETHOD_FLAGS *flags)
39913991
{
3992-
PyArray_Descr **descrs = context->descriptors;
3992+
PyArray_Descr *const *descrs = context->descriptors;
39933993
*flags = context->method->flags & NPY_METH_RUNTIME_FLAGS;
39943994

39953995
if (descrs[0]->type_num == NPY_STRING) {

0 commit comments

Comments
 (0)