Skip to content

Instantly share code, notes, and snippets.

@PureForm
Created April 8, 2013 21:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PureForm/5340738 to your computer and use it in GitHub Desktop.
Save PureForm/5340738 to your computer and use it in GitHub Desktop.
Add this to the end of `os.py`. This fixes the "from os import urandom as _urandom ImportError: cannot import name urandom" issue.
if not _exists("urandom"):
def urandom(n):
"""urandom(n) -> str
Return a string of n random bytes suitable for cryptographic use.
"""
try:
_urandomfd = open("/dev/urandom", O_RDONLY)
except (OSError, IOError):
raise NotImplementedError("/dev/urandom (or equivalent) not found")
try:
bs = b""
while n - len(bs) >= 1:
bs += read(_urandomfd, n - len(bs))
finally:
close(_urandomfd)
return bs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment