]> granicus.if.org Git - python/commitdiff
bpo-36559: random module: optimize sha512 import (GH-12742)
authorChristian Heimes <christian@python.org>
Wed, 10 Apr 2019 20:18:02 +0000 (22:18 +0200)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>
Wed, 10 Apr 2019 20:18:02 +0000 (13:18 -0700)
The random module now prefers the lean internal _sha512 module over hashlib
for seed(version=2) to optimize import time.

Signed-off-by: Christian Heimes <christian@python.org>
Lib/random.py
Misc/NEWS.d/next/Library/2019-04-09-12-02-35.bpo-36559.LbDRrw.rst [new file with mode: 0644]

index 79ef30d7d18d174f311af4ef2d12fe3a1f10194d..53981f3e4f89bd0e21f31114ae112c753fb2240a 100644 (file)
@@ -42,11 +42,18 @@ from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
 from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
 from os import urandom as _urandom
 from _collections_abc import Set as _Set, Sequence as _Sequence
-from hashlib import sha512 as _sha512
 from itertools import accumulate as _accumulate, repeat as _repeat
 from bisect import bisect as _bisect
 import os as _os
 
+try:
+    # hashlib is pretty heavy to load, try lean internal module first
+    from _sha512 import sha512 as _sha512
+except ImportError:
+    # fallback to official implementation
+    from hashlib import sha512 as _sha512
+
+
 __all__ = ["Random","seed","random","uniform","randint","choice","sample",
            "randrange","shuffle","normalvariate","lognormvariate",
            "expovariate","vonmisesvariate","gammavariate","triangular",
diff --git a/Misc/NEWS.d/next/Library/2019-04-09-12-02-35.bpo-36559.LbDRrw.rst b/Misc/NEWS.d/next/Library/2019-04-09-12-02-35.bpo-36559.LbDRrw.rst
new file mode 100644 (file)
index 0000000..2f6ee78
--- /dev/null
@@ -0,0 +1,2 @@
+The random module now prefers the lean internal _sha512 module over hashlib
+for seed(version=2) to optimize import time.