mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
bpo-42222: Modernize integer test/conversion in randrange() (#23064)
This commit is contained in:
parent
1031f23fc3
commit
a9621bb301
4 changed files with 81 additions and 11 deletions
|
@ -51,6 +51,7 @@ from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
|
|||
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
|
||||
|
@ -297,28 +298,59 @@ class Random(_random.Random):
|
|||
|
||||
# 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:
|
||||
raise ValueError("non-integer arg 1 for randrange()")
|
||||
try:
|
||||
istart = _index(start)
|
||||
except TypeError:
|
||||
if int(start) == start:
|
||||
istart = int(start)
|
||||
_warn('Float arguments to randrange() have been deprecated\n'
|
||||
'since Python 3.10 and will be removed in a subsequent '
|
||||
'version.',
|
||||
DeprecationWarning, 2)
|
||||
else:
|
||||
_warn('randrange() will raise TypeError in the future',
|
||||
DeprecationWarning, 2)
|
||||
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:
|
||||
raise ValueError("non-integer stop for randrange()")
|
||||
try:
|
||||
istop = _index(stop)
|
||||
except TypeError:
|
||||
if int(stop) == stop:
|
||||
istop = int(stop)
|
||||
_warn('Float arguments to randrange() have been deprecated\n'
|
||||
'since Python 3.10 and will be removed in a subsequent '
|
||||
'version.',
|
||||
DeprecationWarning, 2)
|
||||
else:
|
||||
_warn('randrange() will raise TypeError in the future',
|
||||
DeprecationWarning, 2)
|
||||
raise ValueError("non-integer stop for randrange()")
|
||||
|
||||
try:
|
||||
istep = _index(step)
|
||||
except TypeError:
|
||||
if int(step) == step:
|
||||
istep = int(step)
|
||||
_warn('Float arguments to randrange() have been deprecated\n'
|
||||
'since Python 3.10 and will be removed in a subsequent '
|
||||
'version.',
|
||||
DeprecationWarning, 2)
|
||||
else:
|
||||
_warn('randrange() will raise TypeError in the future',
|
||||
DeprecationWarning, 2)
|
||||
raise ValueError("non-integer step for randrange()")
|
||||
width = istop - istart
|
||||
if step == 1 and width > 0:
|
||||
if istep == 1 and width > 0:
|
||||
return istart + self._randbelow(width)
|
||||
if step == 1:
|
||||
if istep == 1:
|
||||
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
|
||||
|
||||
# Non-unit step argument supplied.
|
||||
istep = int(step)
|
||||
if istep != step:
|
||||
raise ValueError("non-integer step for randrange()")
|
||||
if istep > 0:
|
||||
n = (width + istep - 1) // istep
|
||||
elif istep < 0:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue