changeset: 105894:0509844f38df branch: 3.6 parent: 105892:b09d0a2587da user: Raymond Hettinger date: Thu Dec 29 22:54:25 2016 -0700 files: Lib/secrets.py Lib/test/test_secrets.py Misc/ACKS Misc/NEWS description: Issue #29061: secrets.randbelow() would hang with a negative input diff -r b09d0a2587da -r 0509844f38df Lib/secrets.py --- a/Lib/secrets.py Fri Dec 30 11:57:40 2016 +0800 +++ b/Lib/secrets.py Thu Dec 29 22:54:25 2016 -0700 @@ -26,6 +26,8 @@ def randbelow(exclusive_upper_bound): """Return a random int in the range [0, n).""" + if exclusive_upper_bound <= 0: + raise ValueError("Upper bound must be positive.") return _sysrand._randbelow(exclusive_upper_bound) DEFAULT_ENTROPY = 32 # number of bytes to return by default diff -r b09d0a2587da -r 0509844f38df Lib/test/test_secrets.py --- a/Lib/test/test_secrets.py Fri Dec 30 11:57:40 2016 +0800 +++ b/Lib/test/test_secrets.py Thu Dec 29 22:54:25 2016 -0700 @@ -70,6 +70,7 @@ for i in range(2, 10): self.assertIn(secrets.randbelow(i), range(i)) self.assertRaises(ValueError, secrets.randbelow, 0) + self.assertRaises(ValueError, secrets.randbelow, -1) class Token_Tests(unittest.TestCase): diff -r b09d0a2587da -r 0509844f38df Misc/ACKS --- a/Misc/ACKS Fri Dec 30 11:57:40 2016 +0800 +++ b/Misc/ACKS Thu Dec 29 22:54:25 2016 -0700 @@ -369,6 +369,7 @@ Josip Djolonga Walter Dörwald Jaromir Dolecek +Brendan Donegan Ismail Donmez Robert Donohue Marcos Donolo diff -r b09d0a2587da -r 0509844f38df Misc/NEWS --- a/Misc/NEWS Fri Dec 30 11:57:40 2016 +0800 +++ b/Misc/NEWS Thu Dec 29 22:54:25 2016 -0700 @@ -43,6 +43,9 @@ - Issue #29085: Allow random.Random.seed() to use high quality OS randomness rather than the pid and time. +- Issue #29061: Fixed bug in secrets.randbelow() which would hang when given + a negative input. Patch by Brendan Donegan. + - Issue #29079: Prevent infinite loop in pathlib.resolve() on Windows - Issue #13051: Fixed recursion errors in large or resized