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 amaury.forgeotdarc
Recipients amaury.forgeotdarc, rhettinger
Date 2011-11-13.16:43:40
SpamBayes Score 1.0658879e-07
Marked as misclassified No
Message-id <1321202621.61.0.74333018382.issue13396@psf.upfronthosting.co.za>
In-reply-to
Content
I noticed that several usages of random.getrandbits() actually need bytes.  A few examples:
- Lib/test/test_zlib.py calls "random.getrandbits(8 * _1M).to_bytes()"
- Twisted uses the %x format and then call .decode('hex')
Another examples found with Code Search:
- struct.pack("Q", random.getrandbits(64))
- for i in range(8): ClientChallenge+= chr(random.getrandbits(8))
- key = sha1(str(random.getrandbits(8*20))).digest()

random.getrandbits() itself is not a cheap call: it ends with a call to _PyLong_FromByteArray, and transformation to bytes will involve more copy, conversions from 30bit digits (or 15bit digits) to bytes, or worse.

This patch adds random.getrandbytes(), which creates a bytes string directly from calls to genrand_int32().
History
Date User Action Args
2011-11-13 16:43:41amaury.forgeotdarcsetrecipients: + amaury.forgeotdarc, rhettinger
2011-11-13 16:43:41amaury.forgeotdarcsetmessageid: <1321202621.61.0.74333018382.issue13396@psf.upfronthosting.co.za>
2011-11-13 16:43:41amaury.forgeotdarclinkissue13396 messages
2011-11-13 16:43:40amaury.forgeotdarccreate