Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
091e95e9
Commit
091e95e9
authored
Apr 05, 2018
by
Wolfgang Maier
Committed by
Raymond Hettinger
Apr 05, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-33203: Ensure random.choice always raises IndexError on empty sequence (GH-6338)
parent
74940913
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
9 additions
and
1 deletion
+9
-1
Lib/random.py
Lib/random.py
+2
-0
Lib/test/test_random.py
Lib/test/test_random.py
+4
-1
Misc/NEWS.d/next/Library/2018-04-05-11-09-45.bpo-33203.Hje9Py.rst
...S.d/next/Library/2018-04-05-11-09-45.bpo-33203.Hje9Py.rst
+3
-0
No files found.
Lib/random.py
View file @
091e95e9
...
...
@@ -242,6 +242,8 @@ class Random(_random.Random):
"enough bits to choose from a population range this large.
\
n
"
"To remove the range limitation, add a getrandbits() method."
)
return
int
(
random
()
*
n
)
if
n
==
0
:
raise
ValueError
(
"Boundary cannot be zero"
)
rem
=
maxsize
%
n
limit
=
(
maxsize
-
rem
)
/
maxsize
# int(limit * maxsize) % n == 0
r
=
random
()
...
...
Lib/test/test_random.py
View file @
091e95e9
...
...
@@ -651,7 +651,10 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
# Population range too large (n >= maxsize)
self
.
gen
.
_randbelow
(
maxsize
+
1
,
maxsize
=
maxsize
)
self
.
gen
.
_randbelow
(
5640
,
maxsize
=
maxsize
)
# issue 33203: test that _randbelow raises ValueError on
# n == 0 also in its getrandbits-independent branch.
with
self
.
assertRaises
(
ValueError
):
self
.
gen
.
_randbelow
(
0
,
maxsize
=
maxsize
)
# 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
# which the following line in Random._randbelow() gets executed:
...
...
Misc/NEWS.d/next/Library/2018-04-05-11-09-45.bpo-33203.Hje9Py.rst
0 → 100644
View file @
091e95e9
``random.Random.choice()`` now raises ``IndexError`` for empty sequences
consistently even when called from subclasses without a ``getrandbits()``
implementation.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment