https://github.com/python/cpython/commit/4c2e299d80c53591f05de2669c0edeaf8a…
commit: 4c2e299d80c53591f05de2669c0edeaf8acd8544
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: GitHub <noreply(a)github.com>
date: 2020-09-30T15:50:07-07:00
summary:
Fix grammar in secrets module documentation (GH-22467)
>From `In particularly,` to `In particular,`
(cherry picked from commit 63298930fb531ba2bb4f23bc3b915dbf1e17e9e1)
Co-authored-by: Max Smolens <msmolens(a)users.noreply.github.com>
files:
M Doc/library/secrets.rst
diff --git a/Doc/library/secrets.rst b/Doc/library/secrets.rst
index bc4766da2785b..afa8e2d385fa4 100644
--- a/Doc/library/secrets.rst
+++ b/Doc/library/secrets.rst
@@ -21,7 +21,7 @@ The :mod:`secrets` module is used for generating cryptographically strong
random numbers suitable for managing data such as passwords, account
authentication, security tokens, and related secrets.
-In particularly, :mod:`secrets` should be used in preference to the
+In particular, :mod:`secrets` should be used in preference to the
default pseudo-random number generator in the :mod:`random` module, which
is designed for modelling and simulation, not security or cryptography.
https://github.com/python/cpython/commit/8fc828ec4e867c111cef137a2daeea6c3d…
commit: 8fc828ec4e867c111cef137a2daeea6c3d5db2c7
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: GitHub <noreply(a)github.com>
date: 2020-09-30T15:49:52-07:00
summary:
Fix grammar in secrets module documentation (GH-22467)
>From `In particularly,` to `In particular,`
(cherry picked from commit 63298930fb531ba2bb4f23bc3b915dbf1e17e9e1)
Co-authored-by: Max Smolens <msmolens(a)users.noreply.github.com>
files:
M Doc/library/secrets.rst
diff --git a/Doc/library/secrets.rst b/Doc/library/secrets.rst
index bc4766da2785b..afa8e2d385fa4 100644
--- a/Doc/library/secrets.rst
+++ b/Doc/library/secrets.rst
@@ -21,7 +21,7 @@ The :mod:`secrets` module is used for generating cryptographically strong
random numbers suitable for managing data such as passwords, account
authentication, security tokens, and related secrets.
-In particularly, :mod:`secrets` should be used in preference to the
+In particular, :mod:`secrets` should be used in preference to the
default pseudo-random number generator in the :mod:`random` module, which
is designed for modelling and simulation, not security or cryptography.
https://github.com/python/cpython/commit/63298930fb531ba2bb4f23bc3b915dbf1e…
commit: 63298930fb531ba2bb4f23bc3b915dbf1e17e9e1
branch: master
author: Max Smolens <msmolens(a)users.noreply.github.com>
committer: GitHub <noreply(a)github.com>
date: 2020-09-30T15:05:51-07:00
summary:
Fix grammar in secrets module documentation (GH-22467)
>From `In particularly,` to `In particular,`
files:
M Doc/library/secrets.rst
diff --git a/Doc/library/secrets.rst b/Doc/library/secrets.rst
index bc4766da2785b..afa8e2d385fa4 100644
--- a/Doc/library/secrets.rst
+++ b/Doc/library/secrets.rst
@@ -21,7 +21,7 @@ The :mod:`secrets` module is used for generating cryptographically strong
random numbers suitable for managing data such as passwords, account
authentication, security tokens, and related secrets.
-In particularly, :mod:`secrets` should be used in preference to the
+In particular, :mod:`secrets` should be used in preference to the
default pseudo-random number generator in the :mod:`random` module, which
is designed for modelling and simulation, not security or cryptography.
https://github.com/python/cpython/commit/868c8e41eb1d7dc032679ae06e62c0d60e…
commit: 868c8e41eb1d7dc032679ae06e62c0d60edd7725
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: GitHub <noreply(a)github.com>
date: 2020-09-28T22:27:06-07:00
summary:
bpo-41774: Add programming FAQ entry (GH-22402)
In the "Sequences (Tuples/Lists)" section, add
"How do you remove multiple items from a list".
(cherry picked from commit 5b0181d1f6474c2cb9b80bdaf3bc56a78bf5fbe7)
Co-authored-by: Terry Jan Reedy <tjreedy(a)udel.edu>
files:
A Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
M Doc/faq/programming.rst
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index 66d210a55fac7..1435765063f73 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -1164,6 +1164,21 @@ This converts the list into a set, thereby removing duplicates, and then back
into a list.
+How do you remove multiple items from a list
+--------------------------------------------
+
+As with removing duplicates, explicitly iterating in reverse with a
+delete condition is one possibility. However, it is easier and faster
+to use slice replacement with an implicit or explicit forward iteration.
+Here are three variations.::
+
+ mylist[:] = filter(keep_function, mylist)
+ mylist[:] = (x for x in mylist if keep_condition)
+ mylist[:] = [x for x in mylist if keep_condition]
+
+If space is not an issue, the list comprehension may be fastest.
+
+
How do you make an array in Python?
-----------------------------------
diff --git a/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst b/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
new file mode 100644
index 0000000000000..af8e02437cb2b
--- /dev/null
+++ b/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
@@ -0,0 +1,2 @@
+In Programming FAQ "Sequences (Tuples/Lists)" section, add "How do you
+remove multiple items from a list".
https://github.com/python/cpython/commit/d50a0700265536a20bcce3fb108c954746…
commit: d50a0700265536a20bcce3fb108c954746d97625
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: GitHub <noreply(a)github.com>
date: 2020-09-28T22:11:06-07:00
summary:
bpo-41774: Add programming FAQ entry (GH-22402)
In the "Sequences (Tuples/Lists)" section, add
"How do you remove multiple items from a list".
(cherry picked from commit 5b0181d1f6474c2cb9b80bdaf3bc56a78bf5fbe7)
Co-authored-by: Terry Jan Reedy <tjreedy(a)udel.edu>
files:
A Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
M Doc/faq/programming.rst
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index 70e9190e5a520..1af04483eb329 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -1163,6 +1163,21 @@ This converts the list into a set, thereby removing duplicates, and then back
into a list.
+How do you remove multiple items from a list
+--------------------------------------------
+
+As with removing duplicates, explicitly iterating in reverse with a
+delete condition is one possibility. However, it is easier and faster
+to use slice replacement with an implicit or explicit forward iteration.
+Here are three variations.::
+
+ mylist[:] = filter(keep_function, mylist)
+ mylist[:] = (x for x in mylist if keep_condition)
+ mylist[:] = [x for x in mylist if keep_condition]
+
+If space is not an issue, the list comprehension may be fastest.
+
+
How do you make an array in Python?
-----------------------------------
diff --git a/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst b/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
new file mode 100644
index 0000000000000..af8e02437cb2b
--- /dev/null
+++ b/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
@@ -0,0 +1,2 @@
+In Programming FAQ "Sequences (Tuples/Lists)" section, add "How do you
+remove multiple items from a list".
https://github.com/python/cpython/commit/5b0181d1f6474c2cb9b80bdaf3bc56a78b…
commit: 5b0181d1f6474c2cb9b80bdaf3bc56a78bf5fbe7
branch: master
author: Terry Jan Reedy <tjreedy(a)udel.edu>
committer: GitHub <noreply(a)github.com>
date: 2020-09-29T01:02:44-04:00
summary:
bpo-41774: Add programming FAQ entry (GH-22402)
In the "Sequences (Tuples/Lists)" section, add
"How do you remove multiple items from a list".
files:
A Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
M Doc/faq/programming.rst
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index 4f4ea8b18176c..76ae4d260fad4 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -1164,6 +1164,21 @@ This converts the list into a set, thereby removing duplicates, and then back
into a list.
+How do you remove multiple items from a list
+--------------------------------------------
+
+As with removing duplicates, explicitly iterating in reverse with a
+delete condition is one possibility. However, it is easier and faster
+to use slice replacement with an implicit or explicit forward iteration.
+Here are three variations.::
+
+ mylist[:] = filter(keep_function, mylist)
+ mylist[:] = (x for x in mylist if keep_condition)
+ mylist[:] = [x for x in mylist if keep_condition]
+
+If space is not an issue, the list comprehension may be fastest.
+
+
How do you make an array in Python?
-----------------------------------
diff --git a/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst b/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
new file mode 100644
index 0000000000000..af8e02437cb2b
--- /dev/null
+++ b/Misc/NEWS.d/next/Documentation/2020-09-24-15-35-13.bpo-41774.5IqdGP.rst
@@ -0,0 +1,2 @@
+In Programming FAQ "Sequences (Tuples/Lists)" section, add "How do you
+remove multiple items from a list".
https://github.com/python/cpython/commit/b0dfc7581697f20385813582de7e92ba6b…
commit: b0dfc7581697f20385813582de7e92ba6ba0105f
branch: master
author: Ram Rachum <ram(a)rachum.com>
committer: GitHub <noreply(a)github.com>
date: 2020-09-28T18:32:10-07:00
summary:
bpo-41773: Raise exception for non-finite weights in random.choices(). (GH-22441)
files:
A Misc/NEWS.d/next/Library/2020-09-28-23-22-25.bpo-41773.oKkus0.rst
M Doc/library/random.rst
M Lib/random.py
M Lib/test/test_random.py
diff --git a/Doc/library/random.rst b/Doc/library/random.rst
index 0cdf0a6ac4a47..af5131df280c2 100644
--- a/Doc/library/random.rst
+++ b/Doc/library/random.rst
@@ -180,8 +180,8 @@ Functions for sequences
The *weights* or *cum_weights* can use any numeric type that interoperates
with the :class:`float` values returned by :func:`random` (that includes
- integers, floats, and fractions but excludes decimals). Behavior is
- undefined if any weight is negative. A :exc:`ValueError` is raised if all
+ integers, floats, and fractions but excludes decimals). Weights are assumed
+ to be non-negative and finite. A :exc:`ValueError` is raised if all
weights are zero.
For a given seed, the :func:`choices` function with equal weighting
diff --git a/Lib/random.py b/Lib/random.py
index 3ea369b81b3e5..139e8a40bb272 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -48,7 +48,7 @@
from warnings import warn as _warn
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
-from math import tau as TWOPI, floor as _floor
+from math import tau as TWOPI, floor as _floor, isfinite as _isfinite
from os import urandom as _urandom
from _collections_abc import Set as _Set, Sequence as _Sequence
from itertools import accumulate as _accumulate, repeat as _repeat
@@ -492,6 +492,8 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1):
total = cum_weights[-1] + 0.0 # convert to float
if total <= 0.0:
raise ValueError('Total of weights must be greater than zero')
+ if not _isfinite(total):
+ raise ValueError('Total of weights must be finite')
bisect = _bisect
hi = n - 1
return [population[bisect(cum_weights, random() * total, 0, hi)]
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index a80e71e67e4c6..0c1fdeec9915e 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -324,6 +324,22 @@ def test_choices_with_all_zero_weights(self):
with self.assertRaises(ValueError):
self.gen.choices('AB', [0.0, 0.0])
+ def test_choices_negative_total(self):
+ with self.assertRaises(ValueError):
+ self.gen.choices('ABC', [3, -5, 1])
+
+ def test_choices_infinite_total(self):
+ with self.assertRaises(ValueError):
+ self.gen.choices('A', [float('inf')])
+ with self.assertRaises(ValueError):
+ self.gen.choices('AB', [0.0, float('inf')])
+ with self.assertRaises(ValueError):
+ self.gen.choices('AB', [-float('inf'), 123])
+ with self.assertRaises(ValueError):
+ self.gen.choices('AB', [0.0, float('nan')])
+ with self.assertRaises(ValueError):
+ self.gen.choices('AB', [float('-inf'), float('inf')])
+
def test_gauss(self):
# Ensure that the seed() method initializes all the hidden state. In
# particular, through 2.2.1 it failed to reset a piece of state used
diff --git a/Misc/NEWS.d/next/Library/2020-09-28-23-22-25.bpo-41773.oKkus0.rst b/Misc/NEWS.d/next/Library/2020-09-28-23-22-25.bpo-41773.oKkus0.rst
new file mode 100644
index 0000000000000..cef7ff0188354
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-09-28-23-22-25.bpo-41773.oKkus0.rst
@@ -0,0 +1,2 @@
+Note in documentation that :func:`random.choices` doesn't support non-finite
+weights, raise :exc:`ValueError` when given non-finite weights.