Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb9f814c

Browse files
authored
gh-111881: Import _sha2 lazily in random (#111889)
The random module now imports the _sha2 module lazily in theRandom.seed() method for str, bytes and bytearray seeds. It alsoimports lazily the warnings module in the _randbelow() method forclasses without getrandbits(). Lazy import makes Python startupfaster and reduces the number of imported modules at startup.
1 parent0802fd6 commitb9f814c

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

‎Lib/random.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
# Adrian Baddeley. Adapted by Raymond Hettinger for use with
5151
# the Mersenne Twister and os.urandom() core generators.
5252

53-
fromwarningsimportwarnas_warn
5453
frommathimportlogas_log,expas_exp,pias_pi,eas_e,ceilas_ceil
5554
frommathimportsqrtas_sqrt,acosas_acos,cosas_cos,sinas_sin
5655
frommathimporttauasTWOPI,flooras_floor,isfiniteas_isfinite
@@ -63,13 +62,6 @@
6362
importosas_os
6463
import_random
6564

66-
try:
67-
# hashlib is pretty heavy to load, try lean internal module first
68-
from_sha2importsha512as_sha512
69-
exceptImportError:
70-
# fallback to official implementation
71-
fromhashlibimportsha512as_sha512
72-
7365
__all__= [
7466
"Random",
7567
"SystemRandom",
@@ -105,6 +97,7 @@
10597
BPF=53# Number of bits in a float
10698
RECIP_BPF=2**-BPF
10799
_ONE=1
100+
_sha512=None
108101

109102

110103
classRandom(_random.Random):
@@ -159,6 +152,16 @@ def seed(self, a=None, version=2):
159152
a=-2ifx==-1elsex
160153

161154
elifversion==2andisinstance(a, (str,bytes,bytearray)):
155+
global_sha512
156+
if_sha512isNone:
157+
try:
158+
# hashlib is pretty heavy to load, try lean internal
159+
# module first
160+
from_sha2importsha512as_sha512
161+
exceptImportError:
162+
# fallback to official implementation
163+
fromhashlibimportsha512as_sha512
164+
162165
ifisinstance(a,str):
163166
a=a.encode()
164167
a=int.from_bytes(a+_sha512(a).digest())
@@ -257,9 +260,10 @@ def _randbelow_without_getrandbits(self, n, maxsize=1<<BPF):
257260

258261
random=self.random
259262
ifn>=maxsize:
260-
_warn("Underlying random() generator does not supply\n"
261-
"enough bits to choose from a population range this large.\n"
262-
"To remove the range limitation, add a getrandbits() method.")
263+
fromwarningsimportwarn
264+
warn("Underlying random() generator does not supply\n"
265+
"enough bits to choose from a population range this large.\n"
266+
"To remove the range limitation, add a getrandbits() method.")
263267
return_floor(random()*n)
264268
rem=maxsize%n
265269
limit= (maxsize-rem)/maxsize# int(limit * maxsize) % n == 0

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp