Message147557
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(). |
|
Date |
User |
Action |
Args |
2011-11-13 16:43:41 | amaury.forgeotdarc | set | recipients:
+ amaury.forgeotdarc, rhettinger |
2011-11-13 16:43:41 | amaury.forgeotdarc | set | messageid: <1321202621.61.0.74333018382.issue13396@psf.upfronthosting.co.za> |
2011-11-13 16:43:41 | amaury.forgeotdarc | link | issue13396 messages |
2011-11-13 16:43:40 | amaury.forgeotdarc | create | |
|