Skip to content

BUG/MAINT: Non-native byteorder in random ints #13655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion numpy/random/generator.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions numpy/random/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -4272,5 +4280,3 @@ __all__ = [
'zipf',
'RandomState',
]


4 changes: 4 additions & 0 deletions numpy/random/tests/test_generator_mt19937.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ def test_zero_size(self, endpoint):
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
Expand Down
9 changes: 9 additions & 0 deletions numpy/random/tests/test_randomstate_regression.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import sys

import pytest

from numpy.testing import (
assert_, assert_array_equal, assert_raises,
)
Expand Down Expand Up @@ -155,3 +158,9 @@ def __array__(self):
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)