[Python-checkins] cpython (3.6): Issue 28475: Improve error message for random.sample() with k < 0.

raymond.hettinger python-checkins at python.org
Mon Nov 21 17:34:57 EST 2016


https://hg.python.org/cpython/rev/89f95c78e0ab
changeset:   105306:89f95c78e0ab
branch:      3.6
parent:      105304:0a2a0061e425
user:        Raymond Hettinger <python at rcn.com>
date:        Mon Nov 21 14:34:33 2016 -0800
summary:
  Issue 28475:  Improve error message for random.sample() with k < 0. (Contributed by Francisco Couzo).

files:
  Lib/random.py           |  2 +-
  Lib/test/test_random.py |  1 +
  2 files changed, 2 insertions(+), 1 deletions(-)


diff --git a/Lib/random.py b/Lib/random.py
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -314,7 +314,7 @@
         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
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -110,6 +110,7 @@
         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

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list