Author vstinner
Recipients vstinner
Date 2019-04-08.13:11:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1554729061.73.0.692414846311.issue36559@roundup.psfhosted.org>
In-reply-to
Content
Currently, when the random module is imported, the hashlib module is always imported which loads the OpenSSL library, whereas hashlib is only needed when a Random() instance is created with a string seed.

For example, "rnd = random.Random()" and "rnd = random.Random(12345)" don't need hashlib.

Example on Linux:

$ python3
Python 3.7.2 (default, Mar 21 2019, 10:09:12) 
>>> import os, sys

>>> 'hashlib' in sys.modules
False
>>> res=os.system(f"grep ssl /proc/{os.getpid()}/maps")

>>> import random
>>> 'hashlib' in sys.modules
True
>>> res=os.system(f"grep ssl /proc/{os.getpid()}/maps")
7f463ec38000-7f463ec55000 r--p 00000000 00:2a 5791335                    /usr/lib64/libssl.so.1.1.1b
7f463ec55000-7f463eca5000 r-xp 0001d000 00:2a 5791335                    /usr/lib64/libssl.so.1.1.1b
...


Attached PR only imports hashlib on demand.

Note: I noticed this issue while working on adding OpenSSL 1.1.1 support to Python 3.4 :-)
History
Date User Action Args
2019-04-08 13:11:01vstinnersetrecipients: + vstinner
2019-04-08 13:11:01vstinnersetmessageid: <1554729061.73.0.692414846311.issue36559@roundup.psfhosted.org>
2019-04-08 13:11:01vstinnerlinkissue36559 messages
2019-04-08 13:11:01vstinnercreate