[issue36559] "import random" should import hashlib on demand (nor load OpenSSL)

STINNER Victor report at bugs.python.org
Mon Apr 8 16:32:55 EDT 2019


STINNER Victor <vstinner at redhat.com> added the comment:

Raymond:
> In general, we don't do deferred imports unless there is a compelling reason (i.e. it is very slow or it is sometimes unavailable).

While I was working on adding OpenSSL 1.1.1 to Python 3.4, my _hashopenssl module was broken. In that case, "import random" fails with ImportError because of hashlib failures. I was surprised, since random doesn't need hashlib at startup.

I would like to be able to use "import random" even if hashlib is broken. For me, random is a key component, whereas I see hashlib more as optional. (Even if in practice, it should always be available).


Raymond:
> Otherwise, it is a false optimization.

Brett:
> Could you explain a bit more, Victor, about why you want to avoid importing hashlib and OpenSSL so much?

Well, OpenSSL is not a random tiny library. For example, loading it increases Python RSS of around 2.7 MiB.

Example with script x.py:
---
import os
os.system(f"grep ^VmRSS /proc/{os.getpid()}/status")
import random
os.system(f"grep ^VmRSS /proc/{os.getpid()}/status")
---

Output without the change on Fedora 29:

VmRSS:	    7396 kB
VmRSS:	   11796 kB  # +4.4 MiB

With the change:

VmRSS:	    7272 kB
VmRSS:	    8988 kB  # +1.7 MiB


Another example is that OpenSSL loads the libz.so dynamic library by dependency.

I would prefer to minimize Python footprint if possible.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36559>
_______________________________________


More information about the Python-bugs-list mailing list