Author iwienand
Recipients iwienand
Date 2012-07-12.22:30:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>

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

from os import urandom as _urandom
   def seed(self, a=None):
        if a is None:
                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/ where "import urandom from os" gets [2]:

if not _exists("urandom"):
    def urandom(n):
            _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 and fall back then too

Date User Action Args
2012-07-12 22:30:29iwienandsetrecipients: + iwienand
2012-07-12 22:30:29iwienandsetmessageid: <>
2012-07-12 22:30:28iwienandlinkissue15340 messages
2012-07-12 22:30:28iwienandcreate