bpo-42222: Modernize integer test/conversion in randrange() (#23064) · python/cpython@a9621bb (original) (raw)

`@@ -51,6 +51,7 @@

`

51

51

`from math import tau as TWOPI, floor as _floor, isfinite as _isfinite

`

52

52

`from os import urandom as _urandom

`

53

53

`from _collections_abc import Set as _Set, Sequence as _Sequence

`

``

54

`+

from operator import index as _index

`

54

55

`from itertools import accumulate as _accumulate, repeat as _repeat

`

55

56

`from bisect import bisect as _bisect

`

56

57

`import os as _os

`

`@@ -297,28 +298,59 @@ def randrange(self, start, stop=None, step=1):

`

297

298

``

298

299

`# This code is a bit messy to make it fast for the

`

299

300

`# common case while still doing adequate error checking.

`

300

``

`-

istart = int(start)

`

301

``

`-

if istart != start:

`

302

``

`-

raise ValueError("non-integer arg 1 for randrange()")

`

``

301

`+

try:

`

``

302

`+

istart = _index(start)

`

``

303

`+

except TypeError:

`

``

304

`+

if int(start) == start:

`

``

305

`+

istart = int(start)

`

``

306

`+

_warn('Float arguments to randrange() have been deprecated\n'

`

``

307

`+

'since Python 3.10 and will be removed in a subsequent '

`

``

308

`+

'version.',

`

``

309

`+

DeprecationWarning, 2)

`

``

310

`+

else:

`

``

311

`+

_warn('randrange() will raise TypeError in the future',

`

``

312

`+

DeprecationWarning, 2)

`

``

313

`+

raise ValueError("non-integer arg 1 for randrange()")

`

303

314

`if stop is None:

`

304

315

`if istart > 0:

`

305

316

`return self._randbelow(istart)

`

306

317

`raise ValueError("empty range for randrange()")

`

307

318

``

308

319

`# stop argument supplied.

`

309

``

`-

istop = int(stop)

`

310

``

`-

if istop != stop:

`

311

``

`-

raise ValueError("non-integer stop for randrange()")

`

``

320

`+

try:

`

``

321

`+

istop = _index(stop)

`

``

322

`+

except TypeError:

`

``

323

`+

if int(stop) == stop:

`

``

324

`+

istop = int(stop)

`

``

325

`+

_warn('Float arguments to randrange() have been deprecated\n'

`

``

326

`+

'since Python 3.10 and will be removed in a subsequent '

`

``

327

`+

'version.',

`

``

328

`+

DeprecationWarning, 2)

`

``

329

`+

else:

`

``

330

`+

_warn('randrange() will raise TypeError in the future',

`

``

331

`+

DeprecationWarning, 2)

`

``

332

`+

raise ValueError("non-integer stop for randrange()")

`

``

333

+

``

334

`+

try:

`

``

335

`+

istep = _index(step)

`

``

336

`+

except TypeError:

`

``

337

`+

if int(step) == step:

`

``

338

`+

istep = int(step)

`

``

339

`+

_warn('Float arguments to randrange() have been deprecated\n'

`

``

340

`+

'since Python 3.10 and will be removed in a subsequent '

`

``

341

`+

'version.',

`

``

342

`+

DeprecationWarning, 2)

`

``

343

`+

else:

`

``

344

`+

_warn('randrange() will raise TypeError in the future',

`

``

345

`+

DeprecationWarning, 2)

`

``

346

`+

raise ValueError("non-integer step for randrange()")

`

312

347

`width = istop - istart

`

313

``

`-

if step == 1 and width > 0:

`

``

348

`+

if istep == 1 and width > 0:

`

314

349

`return istart + self._randbelow(width)

`

315

``

`-

if step == 1:

`

``

350

`+

if istep == 1:

`

316

351

`raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))

`

317

352

``

318

353

`# Non-unit step argument supplied.

`

319

``

`-

istep = int(step)

`

320

``

`-

if istep != step:

`

321

``

`-

raise ValueError("non-integer step for randrange()")

`

322

354

`if istep > 0:

`

323

355

`n = (width + istep - 1) // istep

`

324

356

`elif istep < 0:

`