summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2021-07-22 20:18:06 -0500
committerSebastian Berg <sebastian@sipsolutions.net>2021-07-27 14:47:07 -0700
commit1f8105a19da4bacb4cf9926e18e96bfc5cdc2c82 (patch)
tree5a5912603c8513d9b13df0d7bcefc67e42528eee
parentc10abf5916b37d565149cc501570050c43134ccc (diff)
downloadnumpy-1f8105a19da4bacb4cf9926e18e96bfc5cdc2c82.tar.gz
BUG,MAINT: New style DTypes should never use legacy promotion
Also modify the signatures to prefer npy_bool over ints
-rw-r--r--numpy/core/src/umath/dispatching.c27
-rw-r--r--numpy/core/src/umath/dispatching.h14
-rw-r--r--numpy/core/src/umath/ufunc_object.c17
3 files changed, 32 insertions, 26 deletions
diff --git a/numpy/core/src/umath/dispatching.c b/numpy/core/src/umath/dispatching.c
index 2b0c52ad9..e63780458 100644
--- a/numpy/core/src/umath/dispatching.c
+++ b/numpy/core/src/umath/dispatching.c
@@ -52,10 +52,12 @@
/* forward declaration */
-static PyObject *
+static NPY_INLINE PyObject *
promote_and_get_info_and_ufuncimpl(PyUFuncObject *ufunc,
- PyArrayObject *const ops[], PyArray_DTypeMeta *signature[],
- PyArray_DTypeMeta *op_dtypes[], int do_legacy_fallback, int cache);
+ PyArrayObject *const ops[],
+ PyArray_DTypeMeta *signature[],
+ PyArray_DTypeMeta *op_dtypes[],
+ npy_bool allow_legacy_promotion, npy_bool cache);
/**
@@ -512,8 +514,10 @@ add_and_return_legacy_wrapping_ufunc_loop(PyUFuncObject *ufunc,
*/
static NPY_INLINE PyObject *
promote_and_get_info_and_ufuncimpl(PyUFuncObject *ufunc,
- PyArrayObject *const ops[], PyArray_DTypeMeta *signature[],
- PyArray_DTypeMeta *op_dtypes[], int do_legacy_fallback, int cache)
+ PyArrayObject *const ops[],
+ PyArray_DTypeMeta *signature[],
+ PyArray_DTypeMeta *op_dtypes[],
+ npy_bool allow_legacy_promotion, npy_bool cache)
{
/*
* Fetch the dispatching info which consists of the implementation and
@@ -576,7 +580,7 @@ promote_and_get_info_and_ufuncimpl(PyUFuncObject *ufunc,
* However, we need to give the legacy implementation a chance here.
* (it will modify `op_dtypes`).
*/
- if (!do_legacy_fallback || ufunc->type_resolver == NULL ||
+ if (!allow_legacy_promotion || ufunc->type_resolver == NULL ||
(ufunc->ntypes == 0 && ufunc->userloops == NULL)) {
/* Already tried or not a "legacy" ufunc (no loop found, return) */
return NULL;
@@ -589,7 +593,7 @@ promote_and_get_info_and_ufuncimpl(PyUFuncObject *ufunc,
return NULL;
}
info = promote_and_get_info_and_ufuncimpl(ufunc,
- ops, signature, new_op_dtypes, 0, cacheable);
+ ops, signature, new_op_dtypes, NPY_FALSE, cacheable);
for (int i = 0; i < ufunc->nargs; i++) {
Py_XDECREF(new_op_dtypes);
}
@@ -619,8 +623,11 @@ promote_and_get_info_and_ufuncimpl(PyUFuncObject *ufunc,
*/
NPY_NO_EXPORT PyArrayMethodObject *
promote_and_get_ufuncimpl(PyUFuncObject *ufunc,
- PyArrayObject *const ops[], PyArray_DTypeMeta *signature[],
- PyArray_DTypeMeta *op_dtypes[], int force_legacy_promotion)
+ PyArrayObject *const ops[],
+ PyArray_DTypeMeta *signature[],
+ PyArray_DTypeMeta *op_dtypes[],
+ npy_bool force_legacy_promotion,
+ npy_bool allow_legacy_promotion)
{
int nargs = ufunc->nargs;
@@ -654,7 +661,7 @@ promote_and_get_ufuncimpl(PyUFuncObject *ufunc,
}
PyObject *info = promote_and_get_info_and_ufuncimpl(ufunc,
- ops, signature, op_dtypes, 1, 1);
+ ops, signature, op_dtypes, allow_legacy_promotion, NPY_TRUE);
if (info == NULL) {
if (!PyErr_Occurred()) {
diff --git a/numpy/core/src/umath/dispatching.h b/numpy/core/src/umath/dispatching.h
index f14aa6051..cefad691f 100644
--- a/numpy/core/src/umath/dispatching.h
+++ b/numpy/core/src/umath/dispatching.h
@@ -7,20 +7,16 @@
#include "array_method.h"
-typedef int promoter_function(PyUFuncObject *ufunc,
- PyArray_DTypeMeta *op_dtypes[], PyArray_DTypeMeta *signature[],
- PyArray_DTypeMeta *new_op_dtypes[]);
-
NPY_NO_EXPORT PyArrayMethodObject *
promote_and_get_ufuncimpl(PyUFuncObject *ufunc,
- PyArrayObject *const ops[], PyArray_DTypeMeta *signature[],
- PyArray_DTypeMeta *op_dtypes[], int force_legacy_promotion);
+ PyArrayObject *const ops[],
+ PyArray_DTypeMeta *signature[],
+ PyArray_DTypeMeta *op_dtypes[],
+ npy_bool force_legacy_promotion,
+ npy_bool allow_legacy_promotion);
NPY_NO_EXPORT PyObject *
add_and_return_legacy_wrapping_ufunc_loop(PyUFuncObject *ufunc,
PyArray_DTypeMeta *operation_dtypes[], int ignore_duplicate);
-NPY_NO_EXPORT int
-install_logical_ufunc_promoter(PyObject *ufunc);
-
#endif /*_NPY_DISPATCHING_H */
diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c
index 2308d724b..c2554bd2e 100644
--- a/numpy/core/src/umath/ufunc_object.c
+++ b/numpy/core/src/umath/ufunc_object.c
@@ -922,7 +922,8 @@ _wheremask_converter(PyObject *obj, PyArrayObject **wheremask)
static int
convert_ufunc_arguments(PyUFuncObject *ufunc,
ufunc_full_args full_args, PyArrayObject *out_op[],
- PyArray_DTypeMeta *out_op_DTypes[], int *force_legacy_promotion,
+ PyArray_DTypeMeta *out_op_DTypes[],
+ npy_bool *force_legacy_promotion, npy_bool *allow_legacy_promotion,
PyObject *order_obj, NPY_ORDER *out_order,
PyObject *casting_obj, NPY_CASTING *out_casting,
PyObject *subok_obj, npy_bool *out_subok,
@@ -937,7 +938,8 @@ convert_ufunc_arguments(PyUFuncObject *ufunc,
/* Convert and fill in input arguments */
npy_bool all_scalar = NPY_TRUE;
npy_bool any_scalar = NPY_FALSE;
- npy_bool all_legacy = NPY_TRUE;
+ *allow_legacy_promotion = NPY_TRUE;
+ *force_legacy_promotion = NPY_FALSE;
for (int i = 0; i < nin; i++) {
obj = PyTuple_GET_ITEM(full_args.in, i);
@@ -956,7 +958,7 @@ convert_ufunc_arguments(PyUFuncObject *ufunc,
Py_INCREF(out_op_DTypes[i]);
if (!out_op_DTypes[i]->legacy) {
- all_legacy = NPY_FALSE;
+ *allow_legacy_promotion = NPY_FALSE;
}
if (PyArray_NDIM(out_op[i]) == 0) {
any_scalar = NPY_TRUE;
@@ -977,7 +979,7 @@ convert_ufunc_arguments(PyUFuncObject *ufunc,
* happen inside the Python operators.
*/
}
- if (all_legacy && (!all_scalar && any_scalar)) {
+ if (*allow_legacy_promotion && (!all_scalar && any_scalar)) {
*force_legacy_promotion = should_use_min_scalar(nin, out_op, 0, NULL);
/*
* TODO: if this is False, we end up in a "very slow" path that should
@@ -4824,11 +4826,12 @@ ufunc_generic_fastcall(PyUFuncObject *ufunc,
NPY_CASTING casting = NPY_DEFAULT_ASSIGN_CASTING;
npy_bool subok = NPY_TRUE;
int keepdims = -1; /* We need to know if it was passed */
- int force_legacy_promotion = 0;
+ npy_bool force_legacy_promotion;
+ npy_bool allow_legacy_promotion;
if (convert_ufunc_arguments(ufunc,
/* extract operand related information: */
full_args, operands,
- operand_DTypes, &force_legacy_promotion,
+ operand_DTypes, &force_legacy_promotion, &allow_legacy_promotion,
/* extract general information: */
order_obj, &order,
casting_obj, &casting,
@@ -4848,7 +4851,7 @@ ufunc_generic_fastcall(PyUFuncObject *ufunc,
*/
PyArrayMethodObject *ufuncimpl = promote_and_get_ufuncimpl(ufunc,
operands, signature,
- operand_DTypes, force_legacy_promotion);
+ operand_DTypes, force_legacy_promotion, allow_legacy_promotion);
if (ufuncimpl == NULL) {
goto fail;
}