Opened 9 years ago

Closed 9 years ago

#10253 closed (invalid) random.SystemRandom() fails

Reported by: monkut Owned by: nobody
Component: contrib.sessions Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Alex Gaynor)

Related to changeset 8340.

# Use the system (hardware-based) random number generator if it exists.
if hasattr(random, 'SystemRandom'):
    randrange = random.SystemRandom().randrange
    randrange = random.randrange
MAX_SESSION_KEY = 18446744073709551616L     # 2 << 63

Platform info:

>>> platform.system()
>>> platform.version()
>>> platform.release()
>>> platform.python_version()

Condition in line 17 of, "if hasattr(random, 'SystemRandom'):", evaluates to True, but randrange fails.

>>> hasattr(random, 'SystemRandom')

>>> randrange = random.SystemRandom().randrange
>>> randrange(0, MAX_SESSION_KEY)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/apps/public/python_2.5.2/lib/python2.5/", line 188, in randrange
    return int(istart + self._randbelow(width))
  File "/apps/public/python_2.5.2/lib/python2.5/", line 235, in _randbelow
    r = getrandbits(k)
  File "/apps/public/python_2.5.2/lib/python2.5/", line 778, in getrandbits
    x = long(_hexlify(_urandom(bytes)), 16)
  File "/apps/public/python_2.5.2/lib/python2.5/", line 730, in urandom
    raise NotImplementedError("/dev/urandom (or equivalent) not found")
NotImplementedError: /dev/urandom (or equivalent) not found

Exception occurs at when attempting to generate a new session key (when trying to access admin).

File "/django/contrib/sessions/backends/", line 142, in _get_new_session_key % (randrange(0, MAX_SESSION_KEY), pid, time.time(),

Change History (4)

comment:1 Changed 9 years ago by Alex Gaynor

Description: modified (diff)

Please use the preview button folks.

comment:2 Changed 9 years ago by Ramiro Morales

I'd say this is either a) a Python (the project) programing bug when running in that platform, b) a bug in the Python package/port (if installed in such ways) or the set of options choosen when building python (if complied manually) or c) a sysadmin problem with the RNG setup in the system.

In all cases, it's in a lower level of the stack than Django and it would be better to explore/ask for support/fill bugs with the relevant provider.

comment:3 Changed 9 years ago by Rob Hudson <treborhudson@…>

Component: Uncategorizeddjango.contrib.sessions

comment:4 Changed 9 years ago by Jacob

Resolution: invalid
Status: newclosed

Yes, this is a problem either with Python itself or the Python/Solaris package you're using. If SystemRandom.randrange exists, it ought to work; otherwise the platform shouldn't provide it.

Note: See TracTickets for help on using tickets.
Back to Top