Skip to content
Merged
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
Next Next commit
Use index() instead of testing int(x)==x
  • Loading branch information
rhettinger committed Oct 31, 2020
commit e11b1dc05e664c236d15bcb801e37c08a1ff807d
16 changes: 10 additions & 6 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from math import tau as TWOPI, floor as _floor, isfinite as _isfinite
from os import urandom as _urandom
from _collections_abc import Set as _Set, Sequence as _Sequence
from operator import index as _index
from itertools import accumulate as _accumulate, repeat as _repeat
from bisect import bisect as _bisect
import os as _os
Expand Down Expand Up @@ -297,17 +298,19 @@ def randrange(self, start, stop=None, step=1):

# This code is a bit messy to make it fast for the
# common case while still doing adequate error checking.
istart = int(start)
if istart != start:
try:
istart = _index(start)
except TypeError:
raise ValueError("non-integer arg 1 for randrange()")
if stop is None:
if istart > 0:
return self._randbelow(istart)
raise ValueError("empty range for randrange()")

# stop argument supplied.
istop = int(stop)
if istop != stop:
try:
istop = _index(stop)
except TypeError:
raise ValueError("non-integer stop for randrange()")
width = istop - istart
if step == 1 and width > 0:
Expand All @@ -316,8 +319,9 @@ def randrange(self, start, stop=None, step=1):
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))

# Non-unit step argument supplied.
istep = int(step)
if istep != step:
try:
istep = _index(step)
except TypeError:
raise ValueError("non-integer step for randrange()")
if istep > 0:
n = (width + istep - 1) // istep
Expand Down