summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2019-05-29 12:11:18 -0700
committerGitHub <noreply@github.com>2019-05-29 12:11:18 -0700
commit7603e51c026f0cd4f7cbb6b0adbe034c90570e75 (patch)
treed2668eceb0dd012ef8733de752b12c9b97a4134e /numpy
parent4d51ffb70559653acb6c6c62959413bb9df31662 (diff)
parentfbd9c515fcbf46b10e983067266272ca90c6d342 (diff)
downloadnumpy-7603e51c026f0cd4f7cbb6b0adbe034c90570e75.tar.gz
Merge pull request #13655 from bashtage/integer-byteorder
BUG/MAINT: Non-native byteorder in random ints
Diffstat (limited to 'numpy')
-rw-r--r--numpy/random/generator.pyx8
-rw-r--r--numpy/random/mtrand.pyx12
-rw-r--r--numpy/random/tests/test_generator_mt19937.py4
-rw-r--r--numpy/random/tests/test_randomstate_regression.py9
4 files changed, 29 insertions, 4 deletions
diff --git a/numpy/random/generator.pyx b/numpy/random/generator.pyx
index ca5d4293c..fd93d5efe 100644
--- a/numpy/random/generator.pyx
+++ b/numpy/random/generator.pyx
@@ -429,9 +429,15 @@ cdef class Generator:
high = low
low = 0
- key = np.dtype(dtype).name
+ dt = np.dtype(dtype)
+ key = dt.name
if key not in _integers_types:
raise TypeError('Unsupported dtype "%s" for integers' % key)
+ if not dt.isnative:
+ raise ValueError('Providing a dtype with a non-native byteorder '
+ 'is not supported. If you require '
+ 'platform-independent byteorder, call byteswap '
+ 'when required.')
# Implementation detail: the old API used a masked method to generate
# bounded uniform integers. Lemire's method is preferrable since it is
diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx
index f1d45668e..a1364ac18 100644
--- a/numpy/random/mtrand.pyx
+++ b/numpy/random/mtrand.pyx
@@ -606,9 +606,17 @@ cdef class RandomState:
high = low
low = 0
- key = np.dtype(dtype).name
+ dt = np.dtype(dtype)
+ key = dt.name
if key not in _integers_types:
raise TypeError('Unsupported dtype "%s" for randint' % key)
+ if not dt.isnative:
+ # numpy 1.17.0, 2019-05-28
+ warnings.warn('Providing a dtype with a non-native byteorder is '
+ 'not supported. If you require platform-independent '
+ 'byteorder, call byteswap when required.\nIn future '
+ 'version, providing byteorder will raise a '
+ 'ValueError', DeprecationWarning)
# Implementation detail: the use a masked method to generate
# bounded uniform integers. Lemire's method is preferrable since it is
@@ -4272,5 +4280,3 @@ __all__ = [
'zipf',
'RandomState',
]
-
-
diff --git a/numpy/random/tests/test_generator_mt19937.py b/numpy/random/tests/test_generator_mt19937.py
index 192dcce08..64d87cd71 100644
--- a/numpy/random/tests/test_generator_mt19937.py
+++ b/numpy/random/tests/test_generator_mt19937.py
@@ -467,6 +467,10 @@ class TestIntegers(object):
assert_equal(random.integers(0, -10, size=0).shape, (0,))
assert_equal(random.integers(10, 10, size=0).shape, (0,))
+ def test_error_byteorder(self):
+ other_byteord_dt = '<i4' if sys.byteorder == 'big' else '>i4'
+ with pytest.raises(ValueError):
+ random.integers(0, 200, size=10, dtype=other_byteord_dt)
class TestRandomDist(object):
# Make sure the random distribution returns the correct value for a
diff --git a/numpy/random/tests/test_randomstate_regression.py b/numpy/random/tests/test_randomstate_regression.py
index 9c319319e..5bb1ddde5 100644
--- a/numpy/random/tests/test_randomstate_regression.py
+++ b/numpy/random/tests/test_randomstate_regression.py
@@ -1,4 +1,7 @@
import sys
+
+import pytest
+
from numpy.testing import (
assert_, assert_array_equal, assert_raises,
)
@@ -155,3 +158,9 @@ class TestRegression(object):
perm = random.permutation(m)
assert_array_equal(perm, np.array([2, 1, 4, 0, 3]))
assert_array_equal(m.__array__(), np.arange(5))
+
+ def test_warns_byteorder(self):
+ # GH 13159
+ other_byteord_dt = '<i4' if sys.byteorder == 'big' else '>i4'
+ with pytest.deprecated_call(match='non-native byteorder is not'):
+ random.randint(0, 200, size=10, dtype=other_byteord_dt)