Skip to content

Commit 9f81ab8

Browse files
Use index() instead of int() to check args of randrange().
1 parent bace59d commit 9f81ab8

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

Lib/random.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from _collections_abc import Set as _Set, Sequence as _Sequence
4545
from itertools import accumulate as _accumulate, repeat as _repeat
4646
from bisect import bisect as _bisect
47+
from operator import index as _index
4748
import os as _os
4849

4950
try:
@@ -208,7 +209,7 @@ def __reduce__(self):
208209

209210
## -------------------- integer methods -------------------
210211

211-
def randrange(self, start, stop=None, step=1, _int=int):
212+
def randrange(self, start, stop=None, step=1, _index=_index):
212213
"""Choose a random item from range(start, stop[, step]).
213214
214215
This fixes the problem with randint() which includes the
@@ -218,39 +219,33 @@ def randrange(self, start, stop=None, step=1, _int=int):
218219

219220
# This code is a bit messy to make it fast for the
220221
# common case while still doing adequate error checking.
221-
istart = _int(start)
222-
if istart != start:
223-
raise ValueError("non-integer arg 1 for randrange()")
222+
start = _index(start)
224223
if stop is None:
225-
if istart > 0:
226-
return self._randbelow(istart)
224+
if start > 0:
225+
return self._randbelow(start)
227226
raise ValueError("empty range for randrange()")
228227

229228
# stop argument supplied.
230-
istop = _int(stop)
231-
if istop != stop:
232-
raise ValueError("non-integer stop for randrange()")
233-
width = istop - istart
229+
stop = _index(stop)
230+
width = stop - start
234231
if step == 1 and width > 0:
235-
return istart + self._randbelow(width)
232+
return start + self._randbelow(width)
236233
if step == 1:
237-
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
234+
raise ValueError("empty range for randrange() (%d, %d, %d)" % (start, stop, width))
238235

239236
# Non-unit step argument supplied.
240-
istep = _int(step)
241-
if istep != step:
242-
raise ValueError("non-integer step for randrange()")
243-
if istep > 0:
244-
n = (width + istep - 1) // istep
245-
elif istep < 0:
246-
n = (width + istep + 1) // istep
237+
step = _index(step)
238+
if step > 0:
239+
n = (width + step - 1) // step
240+
elif step < 0:
241+
n = (width + step + 1) // step
247242
else:
248243
raise ValueError("zero step for randrange()")
249244

250245
if n <= 0:
251246
raise ValueError("empty range for randrange()")
252247

253-
return istart + istep*self._randbelow(n)
248+
return start + step*self._randbelow(n)
254249

255250
def randint(self, a, b):
256251
"""Return random integer in range [a, b], including both end points.

Lib/test/test_random.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,14 @@ def test_randrange_errors(self):
368368
raises(-721)
369369
raises(0, 100, -12)
370370
# Non-integer start/stop
371-
raises(3.14159)
372-
raises(0, 2.71828)
371+
self.assertRaises(TypeError, self.gen.randrange, 3.0)
372+
self.assertRaises(TypeError, self.gen.randrange, Fraction(3, 1))
373+
self.assertRaises(TypeError, self.gen.randrange, 0, 2.0)
374+
self.assertRaises(TypeError, self.gen.randrange, 0, Fraction(2, 1))
373375
# Zero and non-integer step
374376
raises(0, 42, 0)
375-
raises(0, 42, 3.14159)
377+
self.assertRaises(TypeError, self.gen.randrange, 0, 42, 3.0)
378+
self.assertRaises(TypeError, self.gen.randrange, 0, 42, Fraction(3, 1))
376379

377380
def test_genrandbits(self):
378381
# Verify ranges

0 commit comments

Comments
 (0)