summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2022-06-29 12:28:43 -0700
committerSebastian Berg <sebastianb@nvidia.com>2022-10-12 10:41:40 +0200
commitfed11cd2553dc3b9742a1540cd3805c5689c1ec0 (patch)
tree2b236ada5eb7d6977e559f309ffd434637d38248 /numpy
parent2143ccae68b71754381ac5b95349520d0b505275 (diff)
downloadnumpy-fed11cd2553dc3b9742a1540cd3805c5689c1ec0.tar.gz
BUG: Ensure new-style promotion is not accidentally used for some ints
This is the *actual* correct fix for the test adaptations. The test adaptations should only be necessary when running in weak-promotion mode, but they are NOT doing that currently.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/umath/scalarmath.c.src6
-rw-r--r--numpy/core/tests/test_nep50_promotions.py9
2 files changed, 14 insertions, 1 deletions
diff --git a/numpy/core/src/umath/scalarmath.c.src b/numpy/core/src/umath/scalarmath.c.src
index 89c065549..e359605d6 100644
--- a/numpy/core/src/umath/scalarmath.c.src
+++ b/numpy/core/src/umath/scalarmath.c.src
@@ -997,7 +997,11 @@ convert_to_@name@(PyObject *value, @type@ *result, npy_bool *may_need_deferring)
int overflow;
long val = PyLong_AsLongAndOverflow(value, &overflow);
if (overflow) {
- return CONVERT_PYSCALAR; /* handle as if "unsafe" */
+ /* handle as if "unsafe" */
+ if (npy_promotion_state != NPY_USE_WEAK_PROMOTION) {
+ return PROMOTION_REQUIRED;
+ }
+ return CONVERT_PYSCALAR;
}
if (error_converting(val)) {
return CONVERSION_ERROR; /* should not be possible */
diff --git a/numpy/core/tests/test_nep50_promotions.py b/numpy/core/tests/test_nep50_promotions.py
index 8129c8b46..2ee4e8d76 100644
--- a/numpy/core/tests/test_nep50_promotions.py
+++ b/numpy/core/tests/test_nep50_promotions.py
@@ -89,3 +89,12 @@ def test_nep50_integer_conversion_errors():
# Error message depends on platform (maybe unsigned int or unsigned long)
with pytest.raises(OverflowError, match=".*unsigned"):
np.uint8(1) + -1
+
+
+def test_nep50_integer_regression():
+ # Test the old integer promotion rules. When the integer is too large,
+ # we need to keep using the old-style promotion.
+ np._set_promotion_state("legacy")
+ arr = np.array(1)
+ assert (arr + 2**63).dtype == np.float64
+ assert (arr[()] + 2**63).dtype == np.float64