Skip to content

Commit 263e983

Browse files
authored
[3.11] GH-99155: Fix NormalDist pickle with 0 and 1 protocols (GH-99156). (GH-99188)
(cherry picked from commit d7a00f1) Co-authored-by: Nikita Sobolev <[email protected]> Automerge-Triggered-By: GH:rhettinger
1 parent 58c8c1d commit 263e983

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Lib/statistics.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,3 +1382,9 @@ def __hash__(self):
13821382

13831383
def __repr__(self):
13841384
return f'{type(self).__name__}(mu={self._mu!r}, sigma={self._sigma!r})'
1385+
1386+
def __getstate__(self):
1387+
return self._mu, self._sigma
1388+
1389+
def __setstate__(self, state):
1390+
self._mu, self._sigma = state

Lib/test/test_statistics.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,14 +2986,19 @@ def __init__(self, mu, sigma):
29862986
nd = NormalDist(100, 15)
29872987
self.assertNotEqual(nd, lnd)
29882988

2989-
def test_pickle_and_copy(self):
2989+
def test_copy(self):
29902990
nd = self.module.NormalDist(37.5, 5.625)
29912991
nd1 = copy.copy(nd)
29922992
self.assertEqual(nd, nd1)
29932993
nd2 = copy.deepcopy(nd)
29942994
self.assertEqual(nd, nd2)
2995-
nd3 = pickle.loads(pickle.dumps(nd))
2996-
self.assertEqual(nd, nd3)
2995+
2996+
def test_pickle(self):
2997+
nd = self.module.NormalDist(37.5, 5.625)
2998+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
2999+
with self.subTest(proto=proto):
3000+
pickled = pickle.loads(pickle.dumps(nd, protocol=proto))
3001+
self.assertEqual(nd, pickled)
29973002

29983003
def test_hashability(self):
29993004
ND = self.module.NormalDist
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :class:`statistics.NormalDist` pickle with ``0`` and ``1`` protocols.

0 commit comments

Comments
 (0)