From bef9efabc3f899a8f05cc6bee009c7c5fb3d01ae Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 7 Nov 2022 05:56:41 +0300 Subject: GH-99155: Fix `NormalDist` pickle with `0` and `1` protocols (GH99156) --- Lib/statistics.py | 6 ++++++ Lib/test/test_statistics.py | 11 ++++++++--- .../Library/2022-11-06-12-44-51.gh-issue-99155.vLZOzi.rst | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-11-06-12-44-51.gh-issue-99155.vLZOzi.rst diff --git a/Lib/statistics.py b/Lib/statistics.py index b4adabd..07d1fd5 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -1446,3 +1446,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 05ce79f..31a3cb6 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -3003,14 +3003,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. -- cgit v0.12