This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author iwienand
Recipients iwienand
Date 2012-07-12.22:30:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1342132229.15.0.69254682318.issue15340@psf.upfronthosting.co.za>
In-reply-to
Content
Hi,

Lib/random.py has a fallback if os.urandom() returns NotImplementedError

---
from os import urandom as _urandom
...
   def seed(self, a=None):
        if a is None:
            try:
                a = long(_hexlify(_urandom(16)), 16)
            except NotImplementedError:
                import time
                a = long(time.time() * 256) # use fractional seconds
---

In 2.6, this is indeed what happens in Lib/os.py where "import urandom from os" gets [2]:

---
if not _exists("urandom"):
    def urandom(n):
...
      try:
            _urandomfd = open("/dev/urandom", O_RDONLY)
        except (OSError, IOError):
            raise NotImplementedError("/dev/urandom (or equivalent) not found")
---

however, in 2.7, things have shuffled around as a result of issue Issue #13703 and now _PyOS_URandom will return an OSError if it can't find /dev/urandom [3].

This means if you "import random" without "/dev/urandom" available it crashes trying to seed

I'm not sure if this is intentional?  One easy solution would be to catch OSError in random.py and fall back then too

[1] http://hg.python.org/cpython/file/70274d53c1dd/Python/random.c#l227
[2] http://hg.python.org/cpython/file/9f8771e09052/Lib/os.py#l746
[3] http://hg.python.org/cpython/file/70274d53c1dd/Lib/random.py#l111
History
Date User Action Args
2012-07-12 22:30:29iwienandsetrecipients: + iwienand
2012-07-12 22:30:29iwienandsetmessageid: <1342132229.15.0.69254682318.issue15340@psf.upfronthosting.co.za>
2012-07-12 22:30:28iwienandlinkissue15340 messages
2012-07-12 22:30:28iwienandcreate