I want to investigate and study the code of python 3.7's os.urandom()
function.
I had a look at the respective standard library's os.py
, but neither is it defined there, nor is it imported there.
I also tried to grep for the definition:
/usr/lib/python3.7 $ grep -rFl "def urandom" 2> /dev/null
But nothing.
I suspect that there is some magic happening regarding os.urandom().
Where can I find the function definition for study and cryptoanalysis?
A lot of names defined for the os
module are handled by Modules/posixmodule.c
, both on POSIX systems and on Windows. Note that the source code is in C, not Python, because accessing system functions requires native code.
You can see this is the os.py
source code, which either imports posix
or nt
based on the current OS, where both those names are compiled from the above posixmodule.c
file but are renamed to reflect the platform better and to indicate that the functionality differs between the two variants (the nt
variant provides fewer functions).
See the os_urandom_impl()
function for the implementation details of os.urandom()
. This delegates to an internal function, _PyOS_URandom()
, which together with other helper functions needed early in Python's startup process, is defined in Python/bootstrap_hash.c
.