Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bb7a31a

Browse filesBrowse files
authored
Merge pull request #18398 from seberg/casting-loop-signature
MAINT: Fix casting signatures to align with NEP 43 signature
2 parents 6668669 + b5de1ce commit bb7a31a
Copy full SHA for bb7a31a

23 files changed

+1778
-2268
lines changed

‎numpy/core/include/numpy/ndarraytypes.h

Copy file name to clipboardExpand all lines: numpy/core/include/numpy/ndarraytypes.h
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,11 @@ typedef void (PyDataMem_EventHookFunc)(void *inp, void *outp, size_t size,
19201920
default_descr_function *default_descr;
19211921
common_dtype_function *common_dtype;
19221922
common_instance_function *common_instance;
1923+
/*
1924+
* The casting implementation (ArrayMethod) to convert between two
1925+
* instances of this DType, stored explicitly for fast access:
1926+
*/
1927+
PyObject *within_dtype_castingimpl;
19231928
/*
19241929
* Dictionary of ArrayMethods representing most possible casts
19251930
* (structured and object are exceptions).

‎numpy/core/src/common/lowlevel_strided_loops.h

Copy file name to clipboardExpand all lines: numpy/core/src/common/lowlevel_strided_loops.h
+32-99Lines changed: 32 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define __LOWLEVEL_STRIDED_LOOPS_H
33
#include "common.h"
44
#include <npy_config.h>
5+
#include <array_method.h>
6+
#include "dtype_transfer.h"
57
#include "mem_overlap.h"
68

79
/* For PyArray_ macros used below */
@@ -30,22 +32,26 @@
3032
* Use NPY_AUXDATA_CLONE and NPY_AUXDATA_FREE to deal with this data.
3133
*
3234
*/
33-
typedef int (PyArray_StridedUnaryOp)(
34-
char *dst, npy_intp dst_stride, char *src, npy_intp src_stride,
35-
npy_intp N, npy_intp src_itemsize, NpyAuxData *transferdata);
35+
// TODO: FIX! That comment belongs to something now in array-method
3636

3737
/*
3838
* This is for pointers to functions which behave exactly as
39-
* for PyArray_StridedUnaryOp, but with an additional mask controlling
39+
* for PyArrayMethod_StridedLoop, but with an additional mask controlling
4040
* which values are transformed.
4141
*
42+
* TODO: We should move this mask "capability" to the ArrayMethod itself
43+
* probably. Although for NumPy internal things this works decently,
44+
* and exposing it there should be well thought out to be useful beyond
45+
* NumPy if possible.
46+
*
4247
* In particular, the 'i'-th element is operated on if and only if
4348
* mask[i*mask_stride] is true.
4449
*/
4550
typedef int (PyArray_MaskedStridedUnaryOp)(
46-
char *dst, npy_intp dst_stride, char *src, npy_intp src_stride,
51+
PyArrayMethod_Context *context, char *const *args,
52+
const npy_intp *dimensions, const npy_intp *strides,
4753
npy_bool *mask, npy_intp mask_stride,
48-
npy_intp N, npy_intp src_itemsize, NpyAuxData *transferdata);
54+
NpyAuxData *auxdata);
4955

5056
/*
5157
* Gives back a function pointer to a specialized function for copying
@@ -65,7 +71,7 @@ typedef int (PyArray_MaskedStridedUnaryOp)(
6571
* Should be the item size if it will always be the same, 0 otherwise.
6672
*
6773
*/
68-
NPY_NO_EXPORT PyArray_StridedUnaryOp *
74+
NPY_NO_EXPORT PyArrayMethod_StridedLoop *
6975
PyArray_GetStridedCopyFn(int aligned,
7076
npy_intp src_stride, npy_intp dst_stride,
7177
npy_intp itemsize);
@@ -80,7 +86,7 @@ PyArray_GetStridedCopyFn(int aligned,
8086
*
8187
* Parameters are as for PyArray_GetStridedCopyFn.
8288
*/
83-
NPY_NO_EXPORT PyArray_StridedUnaryOp *
89+
NPY_NO_EXPORT PyArrayMethod_StridedLoop *
8490
PyArray_GetStridedCopySwapFn(int aligned,
8591
npy_intp src_stride, npy_intp dst_stride,
8692
npy_intp itemsize);
@@ -95,7 +101,7 @@ PyArray_GetStridedCopySwapFn(int aligned,
95101
*
96102
* Parameters are as for PyArray_GetStridedCopyFn.
97103
*/
98-
NPY_NO_EXPORT PyArray_StridedUnaryOp *
104+
NPY_NO_EXPORT PyArrayMethod_StridedLoop *
99105
PyArray_GetStridedCopySwapPairFn(int aligned,
100106
npy_intp src_stride, npy_intp dst_stride,
101107
npy_intp itemsize);
@@ -114,7 +120,7 @@ NPY_NO_EXPORT int
114120
PyArray_GetStridedZeroPadCopyFn(int aligned, int unicode_swap,
115121
npy_intp src_stride, npy_intp dst_stride,
116122
npy_intp src_itemsize, npy_intp dst_itemsize,
117-
PyArray_StridedUnaryOp **outstransfer,
123+
PyArrayMethod_StridedLoop **outstransfer,
118124
NpyAuxData **outtransferdata);
119125

120126
/*
@@ -123,7 +129,7 @@ PyArray_GetStridedZeroPadCopyFn(int aligned, int unicode_swap,
123129
* to dst_type_num. If a conversion is unsupported, returns NULL
124130
* without setting a Python exception.
125131
*/
126-
NPY_NO_EXPORT PyArray_StridedUnaryOp *
132+
NPY_NO_EXPORT PyArrayMethod_StridedLoop *
127133
PyArray_GetStridedNumericCastFn(int aligned,
128134
npy_intp src_stride, npy_intp dst_stride,
129135
int src_type_num, int dst_type_num);
@@ -138,7 +144,7 @@ NPY_NO_EXPORT int
138144
PyArray_GetDTypeCopySwapFn(int aligned,
139145
npy_intp src_stride, npy_intp dst_stride,
140146
PyArray_Descr *dtype,
141-
PyArray_StridedUnaryOp **outstransfer,
147+
PyArrayMethod_StridedLoop **outstransfer,
142148
NpyAuxData **outtransferdata);
143149

144150
/*
@@ -168,12 +174,10 @@ PyArray_GetDTypeCopySwapFn(int aligned,
168174
* If 0, the destination data gets new reference ownership.
169175
* If 1, the references from the source data are moved to
170176
* the destination data.
171-
* out_stransfer:
172-
* The resulting transfer function is placed here.
173-
* out_transferdata:
174-
* The auxiliary data for the transfer function is placed here.
175-
* When finished with the transfer function, the caller must call
176-
* NPY_AUXDATA_FREE on this data.
177+
* cast_info:
178+
* A pointer to an (uninitialized) `NPY_cast_info` struct, the caller
179+
* must call `NPY_cast_info_xfree` on it (except on error) and handle
180+
* its memory livespan.
177181
* out_needs_api:
178182
* If this is non-NULL, and the transfer function produced needs
179183
* to call into the (Python) API, this gets set to 1. This
@@ -191,60 +195,15 @@ PyArray_GetDTypeTransferFunction(int aligned,
191195
npy_intp src_stride, npy_intp dst_stride,
192196
PyArray_Descr *src_dtype, PyArray_Descr *dst_dtype,
193197
int move_references,
194-
PyArray_StridedUnaryOp **out_stransfer,
195-
NpyAuxData **out_transferdata,
198+
NPY_cast_info *cast_info,
196199
int *out_needs_api);
197200

198-
199-
/* Same as above, but only wraps copyswapn or legacy cast functions */
200-
NPY_NO_EXPORT int
201-
PyArray_GetLegacyDTypeTransferFunction(int aligned,
202-
npy_intp src_stride, npy_intp dst_stride,
203-
PyArray_Descr *src_dtype, PyArray_Descr *dst_dtype,
204-
int move_references,
205-
PyArray_StridedUnaryOp **out_stransfer,
206-
NpyAuxData **out_transferdata,
207-
int *out_needs_api, int wrap_if_unaligned);
208-
209-
/* Specialized dtype transfer functions */
210-
NPY_NO_EXPORT int
211-
get_nbo_cast_datetime_transfer_function(int aligned,
212-
PyArray_Descr *src_dtype, PyArray_Descr *dst_dtype,
213-
PyArray_StridedUnaryOp **out_stransfer,
214-
NpyAuxData **out_transferdata);
215-
216-
NPY_NO_EXPORT int
217-
get_nbo_datetime_to_string_transfer_function(
218-
PyArray_Descr *src_dtype, PyArray_Descr *dst_dtype,
219-
PyArray_StridedUnaryOp **out_stransfer, NpyAuxData **out_transferdata);
220-
221-
NPY_NO_EXPORT int
222-
get_datetime_to_unicode_transfer_function(int aligned,
223-
npy_intp src_stride, npy_intp dst_stride,
224-
PyArray_Descr *src_dtype, PyArray_Descr *dst_dtype,
225-
PyArray_StridedUnaryOp **out_stransfer,
226-
NpyAuxData **out_transferdata,
227-
int *out_needs_api);
228-
229-
NPY_NO_EXPORT int
230-
get_nbo_string_to_datetime_transfer_function(
231-
PyArray_Descr *src_dtype, PyArray_Descr *dst_dtype,
232-
PyArray_StridedUnaryOp **out_stransfer, NpyAuxData **out_transferdata);
233-
234-
NPY_NO_EXPORT int
235-
get_unicode_to_datetime_transfer_function(int aligned,
236-
npy_intp src_stride, npy_intp dst_stride,
237-
PyArray_Descr *src_dtype, PyArray_Descr *dst_dtype,
238-
PyArray_StridedUnaryOp **out_stransfer,
239-
NpyAuxData **out_transferdata,
240-
int *out_needs_api);
241-
242201
NPY_NO_EXPORT int
243202
get_fields_transfer_function(int aligned,
244203
npy_intp src_stride, npy_intp dst_stride,
245204
PyArray_Descr *src_dtype, PyArray_Descr *dst_dtype,
246205
int move_references,
247-
PyArray_StridedUnaryOp **out_stransfer,
206+
PyArrayMethod_StridedLoop **out_stransfer,
248207
NpyAuxData **out_transferdata,
249208
int *out_needs_api);
250209

@@ -253,30 +212,10 @@ get_subarray_transfer_function(int aligned,
253212
npy_intp src_stride, npy_intp dst_stride,
254213
PyArray_Descr *src_dtype, PyArray_Descr *dst_dtype,
255214
int move_references,
256-
PyArray_StridedUnaryOp **out_stransfer,
215+
PyArrayMethod_StridedLoop **out_stransfer,
257216
NpyAuxData **out_transferdata,
258217
int *out_needs_api);
259218

260-
NPY_NO_EXPORT int
261-
_strided_to_strided_move_references(char *dst, npy_intp dst_stride,
262-
char *src, npy_intp src_stride,
263-
npy_intp N, npy_intp src_itemsize,
264-
NpyAuxData *data);
265-
266-
NPY_NO_EXPORT int
267-
_strided_to_strided_copy_references(char *dst, npy_intp dst_stride,
268-
char *src, npy_intp src_stride,
269-
npy_intp N, npy_intp src_itemsize,
270-
NpyAuxData *data);
271-
272-
NPY_NO_EXPORT int
273-
wrap_aligned_contig_transfer_function_with_copyswapn(
274-
int aligned, npy_intp src_stride, npy_intp dst_stride,
275-
PyArray_Descr *src_dtype, PyArray_Descr *dst_dtype,
276-
PyArray_StridedUnaryOp **out_stransfer, NpyAuxData **out_transferdata,
277-
int *out_needs_api,
278-
PyArray_StridedUnaryOp *caststransfer, NpyAuxData *castdata);
279-
280219
/*
281220
* This is identical to PyArray_GetDTypeTransferFunction, but returns a
282221
* transfer function which also takes a mask as a parameter. The mask is used
@@ -301,8 +240,7 @@ PyArray_GetMaskedDTypeTransferFunction(int aligned,
301240
PyArray_Descr *dst_dtype,
302241
PyArray_Descr *mask_dtype,
303242
int move_references,
304-
PyArray_MaskedStridedUnaryOp **out_stransfer,
305-
NpyAuxData **out_transferdata,
243+
NPY_cast_info *cast_info,
306244
int *out_needs_api);
307245

308246
/*
@@ -357,11 +295,9 @@ PyArray_CastRawArrays(npy_intp count,
357295
* sizes, for example a casting operation, the 'stransfer' function
358296
* should be specialized for that, in which case 'stransfer' will use
359297
* this parameter as the source item size.
360-
* stransfer:
361-
* The strided transfer function.
362-
* transferdata:
363-
* An auxiliary data pointer passed to the strided transfer function.
364-
* This follows the conventions of NpyAuxData objects.
298+
* cast_info:
299+
* Pointer to the NPY_cast_info struct which summarizes all information
300+
* necessary to perform a cast.
365301
*/
366302
NPY_NO_EXPORT npy_intp
367303
PyArray_TransferNDimToStrided(npy_intp ndim,
@@ -370,8 +306,7 @@ PyArray_TransferNDimToStrided(npy_intp ndim,
370306
npy_intp const *coords, npy_intp coords_inc,
371307
npy_intp const *shape, npy_intp shape_inc,
372308
npy_intp count, npy_intp src_itemsize,
373-
PyArray_StridedUnaryOp *stransfer,
374-
NpyAuxData *transferdata);
309+
NPY_cast_info *cast_info);
375310

376311
NPY_NO_EXPORT npy_intp
377312
PyArray_TransferStridedToNDim(npy_intp ndim,
@@ -380,8 +315,7 @@ PyArray_TransferStridedToNDim(npy_intp ndim,
380315
npy_intp const *coords, npy_intp coords_inc,
381316
npy_intp const *shape, npy_intp shape_inc,
382317
npy_intp count, npy_intp src_itemsize,
383-
PyArray_StridedUnaryOp *stransfer,
384-
NpyAuxData *transferdata);
318+
NPY_cast_info *cast_info);
385319

386320
NPY_NO_EXPORT npy_intp
387321
PyArray_TransferMaskedStridedToNDim(npy_intp ndim,
@@ -391,8 +325,7 @@ PyArray_TransferMaskedStridedToNDim(npy_intp ndim,
391325
npy_intp const *coords, npy_intp coords_inc,
392326
npy_intp const *shape, npy_intp shape_inc,
393327
npy_intp count, npy_intp src_itemsize,
394-
PyArray_MaskedStridedUnaryOp *stransfer,
395-
NpyAuxData *data);
328+
NPY_cast_info *cast_info);
396329

397330
NPY_NO_EXPORT int
398331
mapiter_trivial_get(PyArrayObject *self, PyArrayObject *ind,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.