]> granicus.if.org Git - python/commitdiff
Issue 28475: Improve error message for random.sample() with k < 0. (Contributed...
authorRaymond Hettinger <python@rcn.com>
Mon, 21 Nov 2016 22:34:33 +0000 (14:34 -0800)
committerRaymond Hettinger <python@rcn.com>
Mon, 21 Nov 2016 22:34:33 +0000 (14:34 -0800)
Lib/random.py
Lib/test/test_random.py

index ca90e1459d9964632d62ec8618891e223aed3239..49b0f149a5a5001a42045d29c01aea7ed7514798 100644 (file)
@@ -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:
index fd0d2e319ec507418d16f0f1a8f87093321e351a..84a1e831fd75a94bf0f987f447c2836ff97a068f 100644 (file)
@@ -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