summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2022-11-07 09:41:08 (GMT)
committerGitHub <noreply@github.com>2022-11-07 09:41:08 (GMT)
commit263e9830aafcea1b0bea60478360604a0d5e04da (patch)
tree62db99247d77b7b4427ecfc8c67e42a419398799
parent58c8c1dee798d394344b5b56e890a2cff1c41082 (diff)
downloadcpython-263e9830aafcea1b0bea60478360604a0d5e04da.zip
cpython-263e9830aafcea1b0bea60478360604a0d5e04da.tar.gz
cpython-263e9830aafcea1b0bea60478360604a0d5e04da.tar.bz2
[3.11] GH-99155: Fix `NormalDist` pickle with `0` and `1` protocols (GH-99156). (GH-99188)
(cherry picked from commit d7a00f1e8eee05fc5ae97ea1ef0615feefce887b) Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Automerge-Triggered-By: GH:rhettinger
-rw-r--r--Lib/statistics.py6
-rw-r--r--Lib/test/test_statistics.py11
-rw-r--r--Misc/NEWS.d/next/Library/2022-11-06-12-44-51.gh-issue-99155.vLZOzi.rst1
3 files changed, 15 insertions, 3 deletions
diff --git a/Lib/statistics.py b/Lib/statistics.py
index 9598ab6..3b3b43b 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -1382,3 +1382,9 @@ class NormalDist:
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 6de9824..3e172e9 100644
--- a/Lib/test/test_statistics.py
+++ b/Lib/test/test_statistics.py
@@ -2986,14 +2986,19 @@ class TestNormalDist:
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 0000000..a84caa6ac
--- /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.