Skip to content

Move error test to function that needs it. Improve error message. #30008

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 25 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bbd2da9
Merge pull request #1 from python/master
rhettinger Mar 16, 2021
74bdf1b
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
6c53f1a
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
a487c4f
.
rhettinger Mar 24, 2021
eb56423
.
rhettinger Mar 25, 2021
cc7ba06
.
rhettinger Mar 26, 2021
d024dd0
.
rhettinger Apr 22, 2021
b10f912
merge
rhettinger May 5, 2021
fb6744d
merge
rhettinger May 6, 2021
7f21a1c
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 15, 2021
7da42d4
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Aug 25, 2021
e31757b
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 31, 2021
f058a6f
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 31, 2021
1fc29bd
Merge branch 'main' of github.com:python/cpython
rhettinger Sep 4, 2021
e5c0184
Merge branch 'main' of github.com:python/cpython
rhettinger Oct 30, 2021
3c86ec1
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 9, 2021
96675e4
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Nov 9, 2021
de558c6
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 9, 2021
418a07f
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 14, 2021
ea23a8b
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 21, 2021
ba248b7
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 27, 2021
9bc1df1
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 1, 2021
d4466ba
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 1, 2021
a89f02e
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 8, 2021
1cbb961
Move error test to function that needs it. Improve error message.
rhettinger Dec 9, 2021
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
11 changes: 4 additions & 7 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,8 @@ def __init_subclass__(cls, /, **kwargs):
break

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
k = n.bit_length() # don't use (n-1) here because n can be 1
r = getrandbits(k) # 0 <= r < 2**k
Expand All @@ -245,7 +243,7 @@ def _randbelow_with_getrandbits(self, n):
return r

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.
"""
Expand All @@ -256,8 +254,6 @@ def _randbelow_without_getrandbits(self, n, maxsize=1<<BPF):
"enough bits to choose from a population range this large.\n"
"To remove the range limitation, add a getrandbits() method.")
return _floor(random() * n)
if n == 0:
return 0
rem = maxsize % n
limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0
r = random()
Expand Down Expand Up @@ -338,7 +334,8 @@ def randint(self, a, b):

def choice(self, seq):
"""Choose a random element from a non-empty sequence."""
# raises IndexError if seq is empty
if not seq:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhettinger : This change prevents operation on numpy arrays: if not seq will produce a ValueError (ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()). While it might be perverse to use random from stdlib on a numpy array, such code exists in the wild, such as in the test suite of scikit-learn which this change then breaks. Perhaps the test is better seq is None or len(seq) == 0, since that's what is actually needed for the next line about an empty sequence?

raise IndexError('Cannot choose from an empty sequence')
return seq[self._randbelow(len(seq))]

def shuffle(self, x):
Expand Down
4 changes: 0 additions & 4 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,10 +816,6 @@ def test_randbelow_without_getrandbits(self):
maxsize+1, 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
# noble aim of achieving 100% test coverage we need to write a case in
Expand Down