Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Cover the case with a unit step
  • Loading branch information
rhettinger committed Dec 24, 2020
commit 9e6945d9d2a361c3ce947490823fab114ce96b81
13 changes: 7 additions & 6 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,7 @@ def randrange(self, start, stop=None, step=1):
_warn('randrange() will raise TypeError in the future',
DeprecationWarning, 2)
raise ValueError("non-integer stop for randrange()")
width = istop - istart
if step == 1 and width > 0:
return istart + self._randbelow(width)
if step == 1:
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))

# Non-unit step argument supplied.
try:
istep = _index(step)
except TypeError:
Expand All @@ -350,6 +344,13 @@ def randrange(self, start, stop=None, step=1):
_warn('randrange() will raise TypeError in the future',
DeprecationWarning, 2)
raise ValueError("non-integer step for randrange()")
width = istop - istart
if istep == 1 and width > 0:
return istart + self._randbelow(width)
if istep == 1:
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))

# Non-unit step argument supplied.
if istep > 0:
n = (width + istep - 1) // istep
elif istep < 0:
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ def test_randrange_argument_handling(self):
randrange(10.0, 20, 2)
with self.assertWarns(DeprecationWarning):
randrange(10, 20.0, 2)
with self.assertWarns(DeprecationWarning):
randrange(10, 20, 1.0)
with self.assertWarns(DeprecationWarning):
randrange(10, 20, 2.0)
with self.assertWarns(DeprecationWarning):
Expand Down