diff options
Diffstat (limited to 'numpy/core/src')
-rw-r--r-- | numpy/core/src/multiarray/arrayobject.h | 7 | ||||
-rw-r--r-- | numpy/core/src/umath/ufunc_object.c | 38 |
2 files changed, 42 insertions, 3 deletions
diff --git a/numpy/core/src/multiarray/arrayobject.h b/numpy/core/src/multiarray/arrayobject.h index f7d0734db..d9900439c 100644 --- a/numpy/core/src/multiarray/arrayobject.h +++ b/numpy/core/src/multiarray/arrayobject.h @@ -40,6 +40,13 @@ static const int NPY_ARRAY_WARN_ON_WRITE = (1 << 31); static const int NPY_ARRAY_WAS_PYTHON_INT = (1 << 30); static const int NPY_ARRAY_WAS_PYTHON_FLOAT = (1 << 29); static const int NPY_ARRAY_WAS_PYTHON_COMPLEX = (1 << 28); +/* + * Mark that this was a huge int and the array needed replace (no re-use). + * This flag is only used in the ufunc machinery where it is tricky to cover + * correct all type resolution paths where `np.array(large_integer)` returns + * an object array. + */ +static const int NPY_ARRAY_WAS_INT_AND_REPLACED = (1 << 27); static const int NPY_ARRAY_WAS_PYTHON_LITERAL = (1 << 30 | 1 << 29 | 1 << 28); #endif /* NUMPY_CORE_SRC_MULTIARRAY_ARRAYOBJECT_H_ */ diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 39e64decb..94cd73ef6 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -1011,6 +1011,34 @@ convert_ufunc_arguments(PyUFuncObject *ufunc, * necessary to propagate the information to the legacy type resolution. */ if (npy_mark_tmp_array_if_pyscalar(obj, out_op[i], &out_op_DTypes[i])) { + if (PyArray_FLAGS(out_op[i]) & NPY_ARRAY_WAS_PYTHON_INT + && PyArray_TYPE(out_op[i]) != NPY_LONG) { + /* + * When `np.array(integer)` is not the default integer (mainly + * object dtype), this confuses many type resolvers. Simply + * forcing a default integer array is unfortunately easiest. + * In this disables the optional NEP 50 warnings, but in + * practice when this happens we should _usually_ pick the + * default integer loop and that raises an error. + * (An exception is `float64(1.) + 10**100` which silently + * will give a float64 result rather than a Python float.) + * + * TODO: Just like the general dual NEP 50/legacy promotion + * support this is meant as a temporary hack for NumPy 1.25. + */ + static PyArrayObject *zero_arr = NULL; + if (NPY_UNLIKELY(zero_arr == NULL)) { + zero_arr = (PyArrayObject *)PyArray_ZEROS( + 0, NULL, NPY_LONG, NPY_FALSE); + if (zero_arr == NULL) { + goto fail; + } + ((PyArrayObject_fields *)zero_arr)->flags |= ( + NPY_ARRAY_WAS_PYTHON_INT|NPY_ARRAY_WAS_INT_AND_REPLACED); + } + Py_INCREF(zero_arr); + Py_SETREF(out_op[i], zero_arr); + } *promoting_pyscalars = NPY_TRUE; } } @@ -4929,9 +4957,13 @@ ufunc_generic_fastcall(PyUFuncObject *ufunc, if (!(orig_flags & NPY_ARRAY_WAS_PYTHON_LITERAL)) { continue; } - /* If the descriptor matches, no need to worry about conversion */ - if (PyArray_EquivTypes( - PyArray_DESCR(operands[i]), operation_descrs[i])) { + /* + * If descriptor matches, no need to convert, but integers may + * have been too large. + */ + if (!(orig_flags & NPY_ARRAY_WAS_INT_AND_REPLACED) + && PyArray_EquivTypes( + PyArray_DESCR(operands[i]), operation_descrs[i])) { continue; } /* Otherwise, replace the operand with a new array */ |