From bf87126a636bddf2d9a8f432215fe2efb98ba8fe Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 21 Nov 2016 14:34:33 -0800 Subject: Issue 28475: Improve error message for random.sample() with k < 0. (Contributed by Francisco Couzo). --- Lib/random.py | 2 +- Lib/test/test_random.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/random.py b/Lib/random.py index ca90e14..49b0f14 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -314,7 +314,7 @@ class Random(_random.Random): randbelow = self._randbelow n = len(population) if not 0 <= k <= n: - raise ValueError("Sample larger than population") + raise ValueError("Sample larger than population or is negative") result = [None] * k setsize = 21 # size of a small set minus size of an empty list if k > 5: diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index fd0d2e3..84a1e83 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -110,6 +110,7 @@ class TestBasicOps: self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 # Exception raised if size of sample exceeds that of population self.assertRaises(ValueError, self.gen.sample, population, N+1) + self.assertRaises(ValueError, self.gen.sample, [], -1) def test_sample_distribution(self): # For the entire allowable range of 0 <= k <= N, validate that -- cgit v0.12