From d4377b4837b2caed9dc568c692697d105e6d05f6 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 6 Nov 2022 12:46:50 +0300 Subject: [PATCH 1/3] gh-99155: Fix `NormalDist` pickle with `0` and `1` protocols --- Lib/statistics.py | 8 ++++++++ Lib/test/test_statistics.py | 11 ++++++++--- .../2022-11-06-12-44-51.gh-issue-99155.vLZOzi.rst | 1 + 3 files changed, 17 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 b4adabd3f05ae8..d1104d564a2f13 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -1446,3 +1446,11 @@ def __hash__(self): def __repr__(self): return f'{type(self).__name__}(mu={self._mu!r}, sigma={self._sigma!r})' + + def __getstate__(self): + "Pickle support." + return (self._mu, self._sigma) + + def __setstate__(self, state): + "Unpickle support." + self._mu, self._sigma = state diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py index 05ce79f126590a..31a3cb6b53a6f2 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -3003,14 +3003,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 00000000000000..a84caa6ac2ea37 --- /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. From 4c8e383755698eac289be737cc3a53100754c61e Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 6 Nov 2022 13:00:43 -0600 Subject: [PATCH 2/3] Update statistics.py We don't usually put docstrings on these two methods. --- Lib/statistics.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/statistics.py b/Lib/statistics.py index d1104d564a2f13..8ec3bca9bd1ef0 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -1448,9 +1448,7 @@ def __repr__(self): return f'{type(self).__name__}(mu={self._mu!r}, sigma={self._sigma!r})' def __getstate__(self): - "Pickle support." return (self._mu, self._sigma) def __setstate__(self, state): - "Unpickle support." self._mu, self._sigma = state From 928f451ecd822d5ab0547a2d27874aa7c9faa8d5 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 6 Nov 2022 13:04:52 -0600 Subject: [PATCH 3/3] Update statistics.py Formatting nit --- Lib/statistics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/statistics.py b/Lib/statistics.py index 8ec3bca9bd1ef0..07d1fd5ba6e98e 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -1448,7 +1448,7 @@ def __repr__(self): return f'{type(self).__name__}(mu={self._mu!r}, sigma={self._sigma!r})' def __getstate__(self): - return (self._mu, self._sigma) + return self._mu, self._sigma def __setstate__(self, state): self._mu, self._sigma = state