mirror of https://github.com/python/cpython
Move error test to the function that needs it. Improve error message. (GH-30008)
This commit is contained in:
parent
50669083fe
commit
3fee7776e6
|
@ -233,10 +233,8 @@ class Random(_random.Random):
|
||||||
break
|
break
|
||||||
|
|
||||||
def _randbelow_with_getrandbits(self, n):
|
def _randbelow_with_getrandbits(self, n):
|
||||||
"Return a random int in the range [0,n). Returns 0 if n==0."
|
"Return a random int in the range [0,n). Defined for n > 0."
|
||||||
|
|
||||||
if not n:
|
|
||||||
return 0
|
|
||||||
getrandbits = self.getrandbits
|
getrandbits = self.getrandbits
|
||||||
k = n.bit_length() # don't use (n-1) here because n can be 1
|
k = n.bit_length() # don't use (n-1) here because n can be 1
|
||||||
r = getrandbits(k) # 0 <= r < 2**k
|
r = getrandbits(k) # 0 <= r < 2**k
|
||||||
|
@ -245,7 +243,7 @@ class Random(_random.Random):
|
||||||
return r
|
return r
|
||||||
|
|
||||||
def _randbelow_without_getrandbits(self, n, maxsize=1<<BPF):
|
def _randbelow_without_getrandbits(self, n, maxsize=1<<BPF):
|
||||||
"""Return a random int in the range [0,n). Returns 0 if n==0.
|
"""Return a random int in the range [0,n). Defined for n > 0.
|
||||||
|
|
||||||
The implementation does not use getrandbits, but only random.
|
The implementation does not use getrandbits, but only random.
|
||||||
"""
|
"""
|
||||||
|
@ -256,8 +254,6 @@ class Random(_random.Random):
|
||||||
"enough bits to choose from a population range this large.\n"
|
"enough bits to choose from a population range this large.\n"
|
||||||
"To remove the range limitation, add a getrandbits() method.")
|
"To remove the range limitation, add a getrandbits() method.")
|
||||||
return _floor(random() * n)
|
return _floor(random() * n)
|
||||||
if n == 0:
|
|
||||||
return 0
|
|
||||||
rem = maxsize % n
|
rem = maxsize % n
|
||||||
limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0
|
limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0
|
||||||
r = random()
|
r = random()
|
||||||
|
@ -338,7 +334,8 @@ class Random(_random.Random):
|
||||||
|
|
||||||
def choice(self, seq):
|
def choice(self, seq):
|
||||||
"""Choose a random element from a non-empty sequence."""
|
"""Choose a random element from a non-empty sequence."""
|
||||||
# raises IndexError if seq is empty
|
if not seq:
|
||||||
|
raise IndexError('Cannot choose from an empty sequence')
|
||||||
return seq[self._randbelow(len(seq))]
|
return seq[self._randbelow(len(seq))]
|
||||||
|
|
||||||
def shuffle(self, x):
|
def shuffle(self, x):
|
||||||
|
|
|
@ -816,10 +816,6 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
|
||||||
maxsize+1, maxsize=maxsize
|
maxsize+1, maxsize=maxsize
|
||||||
)
|
)
|
||||||
self.gen._randbelow_without_getrandbits(5640, maxsize=maxsize)
|
self.gen._randbelow_without_getrandbits(5640, maxsize=maxsize)
|
||||||
# issue 33203: test that _randbelow returns zero on
|
|
||||||
# n == 0 also in its getrandbits-independent branch.
|
|
||||||
x = self.gen._randbelow_without_getrandbits(0, maxsize=maxsize)
|
|
||||||
self.assertEqual(x, 0)
|
|
||||||
|
|
||||||
# This might be going too far to test a single line, but because of our
|
# This might be going too far to test a single line, but because of our
|
||||||
# noble aim of achieving 100% test coverage we need to write a case in
|
# noble aim of achieving 100% test coverage we need to write a case in
|
||||||
|
|
Loading…
Reference in New Issue