[Python-checkins] [3.10] [ GH-99155: Fix `NormalDist` pickle with `0` and `1` protocols (GH-99156). (GH-99188) (GH-99190)
rhettinger
webhook-mailer at python.org
Mon Nov 7 05:17:04 EST 2022
https://github.com/python/cpython/commit/ea2316a220ea5ae5646518e3855ef22b9c84d64d
commit: ea2316a220ea5ae5646518e3855ef22b9c84d64d
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2022年11月07日T04:16:54-06:00
summary:
[3.10] [ GH-99155: Fix `NormalDist` pickle with `0` and `1` protocols (GH-99156). (GH-99188) (GH-99190)
files:
A Misc/NEWS.d/next/Library/2022-11-06-12-44-51.gh-issue-99155.vLZOzi.rst
M Lib/statistics.py
M Lib/test/test_statistics.py
diff --git a/Lib/statistics.py b/Lib/statistics.py
index f66245380abb..52f17851591b 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -1265,3 +1265,9 @@ def __hash__(self):
def __repr__(self):
return f'{type(self).__name__}(mu={self._mu!r}, sigma={self._sigma!r})'
+
+ def __getstate__(self):
+ return self._mu, self._sigma
+
+ def __setstate__(self, state):
+ self._mu, self._sigma = state
diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py
index 2853b1b2b2f4..341abef8b1c1 100644
--- a/Lib/test/test_statistics.py
+++ b/Lib/test/test_statistics.py
@@ -2880,14 +2880,19 @@ def __init__(self, mu, sigma):
nd = NormalDist(100, 15)
self.assertNotEqual(nd, lnd)
- def test_pickle_and_copy(self):
+ def test_copy(self):
nd = self.module.NormalDist(37.5, 5.625)
nd1 = copy.copy(nd)
self.assertEqual(nd, nd1)
nd2 = copy.deepcopy(nd)
self.assertEqual(nd, nd2)
- nd3 = pickle.loads(pickle.dumps(nd))
- self.assertEqual(nd, nd3)
+
+ def test_pickle(self):
+ nd = self.module.NormalDist(37.5, 5.625)
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ with self.subTest(proto=proto):
+ pickled = pickle.loads(pickle.dumps(nd, protocol=proto))
+ self.assertEqual(nd, pickled)
def test_hashability(self):
ND = self.module.NormalDist
diff --git a/Misc/NEWS.d/next/Library/2022-11-06-12-44-51.gh-issue-99155.vLZOzi.rst b/Misc/NEWS.d/next/Library/2022-11-06-12-44-51.gh-issue-99155.vLZOzi.rst
new file mode 100644
index 000000000000..a84caa6ac2ea
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-11-06-12-44-51.gh-issue-99155.vLZOzi.rst
@@ -0,0 +1 @@
+Fix :class:`statistics.NormalDist` pickle with ``0`` and ``1`` protocols.
More information about the Python-checkins
mailing list