8000 BUG: Backport #7254 by ahaldane · Pull Request #7271 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Backport #7254 #7271

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
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
4 changes: 2 additions & 2 deletions numpy/core/tests/test_mem_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ def test_diophantine_fuzz():
A_max = min(max_int, A_max)
U_max = min(max_int-1, U_max)

A = tuple(rng.randint(1, A_max+1, dtype=np.intp)
A = tuple(int(rng.randint(1, A_max+1, dtype=np.intp))
for j in range(ndim))
U = tuple(rng.randint(0, U_max+2, dtype=np.intp)
U = tuple(int(rng.randint(0, U_max+2, dtype=np.intp))
for j in range(ndim))

b_ub = min(max_int-2, sum(a*ub for a, ub in zip(A, U)))
Expand Down
18 changes: 9 additions & 9 deletions numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def _rand_bool(low, high, size, rngstate):
off = <npy_bool>(low)
if size is None:
rk_random_bool(off, rng, 1, &buf, state)
return buf
return np.bool_(<npy_bool>buf)
else:
array = <ndarray>np.empty(size, np.bool_)
cnt = PyArray_SIZE(array)
Expand All @@ -639,7 +639,7 @@ def _rand_int8(low, high, size, rngstate):
off = <npy_uint8>(<npy_int8>low)
if size is None:
rk_random_uint8(off, rng, 1, &buf, state)
return <npy_int8>buf
return np.int8(<npy_int8>buf)
else:
array = <ndarray>np.empty(size, np.int8)
cnt = PyArray_SIZE(array)
Expand All @@ -666,7 +666,7 @@ def _rand_int16(low, high, size, rngstate):
off = <npy_uint16>(<npy_int16>low)
if size is None:
rk_random_uint16(off, rng, 1, &buf, state)
return <npy_int16>buf
return np.int16(<npy_int16>buf)
else:
array = <ndarray>np.empty(size, np.int16)
cnt = PyArray_SIZE(array)
Expand Down Expand Up @@ -717,7 +717,7 @@ def _rand_int32(low, high, size, rngstate):
off = <npy_uint32>(<npy_int32>low)
if size is None:
rk_random_uint32(off, rng, 1, &buf, state)
return <npy_int32>buf
return np.int32(<npy_int32>buf)
else:
array = <ndarray>np.empty(size, np.int32)
cnt = PyArray_SIZE(array)
Expand All @@ -744,7 +744,7 @@ def _rand_int64(low, high, size, rngstate):
off = <npy_uint64>(<npy_int64>low)
if size is None:
rk_random_uint64(off, rng, 1, &buf, state)
return <npy_int64>buf
return np.int64(<npy_int64>buf)
else:
array = <ndarray>np.empty(size, np.int64)
cnt = PyArray_SIZE(array)
Expand All @@ -770,7 +770,7 @@ def _rand_uint8(low, high, size, rngstate):
off = <npy_uint8>(low)
if size is None:
rk_random_uint8(off, rng, 1, &buf, state)
return buf
return np.uint8(<npy_uint8>buf)
else:
array = <ndarray>np.empty(size, np.uint8)
cnt = PyArray_SIZE(array)
Expand All @@ -797,7 +797,7 @@ def _rand_uint16(low, high, size, rngstate):
off = <npy_uint16>(low)
if size is None:
rk_random_uint16(off, rng, 1, &buf, state)
return buf
return np.uint16(<npy_uint16>buf)
else:
array = <ndarray>np.empty(size, np.uint16)
cnt = PyArray_SIZE(array)
Expand All @@ -824,7 +824,7 @@ def _rand_uint32(low, high, size, rngstate):
off = <npy_uint32>(low)
if size is None:
rk_random_uint32(off, rng, 1, &buf, state)
return <npy_uint32>buf
return np.uint32(<npy_uint32>buf)
else:
array = <ndarray>np.empty(size, np.uint32)
cnt = PyArray_SIZE(array)
Expand All @@ -851,7 +851,7 @@ def _rand_uint64(low, high, size, rngstate):
off = <npy_uint64>(low)
if size is None:
rk_random_uint64(off, rng, 1, &buf, state)
return <npy_uint64>buf
return np.uint64(<npy_uint64>buf)
else:
array = <ndarray>np.empty(size, np.uint64)
cnt = PyArray_SIZE(array)
Expand Down
9 changes: 9 additions & 0 deletions numpy/random/tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ def test_repeatability(self):
res = hashlib.md5(val).hexdigest()
assert_(tgt[np.dtype(np.bool).name] == res)

def test_respect_dtype_singleton(self):
# See gh-7203
for dt in self.itype:
lbnd = 0 if dt is np.bool else np.iinfo(dt).min
ubnd = 2 if dt is np.bool else np.iinfo(dt).max + 1

sample = self.rfunc(lbnd, ubnd, dtype=dt)
self.assertEqual(sample.dtype, np.dtype(dt))


class TestRandomDist(TestCase):
# Make sure the random distribution returns the correct value for a
Expand Down
0