Skip to content

Commit bef9efa

Browse files
authored
GH-99155: Fix NormalDist pickle with 0 and 1 protocols (GH99156)
1 parent f626b7b commit bef9efa

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
@@ -1446,3 +1446,9 @@ def __hash__(self):
14461446

14471447
def __repr__(self):
14481448
return f'{type(self).__name__}(mu={self._mu!r}, sigma={self._sigma!r})'
1449+
1450+
def __getstate__(self):
1451+
return self._mu, self._sigma
1452+
1453+
def __setstate__(self, state):
1454+
self._mu, self._sigma = state

Lib/test/test_statistics.py

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

3006-
def test_pickle_and_copy(self):
3006+
def test_copy(self):
30073007
nd = self.module.NormalDist(37.5, 5.625)
30083008
nd1 = copy.copy(nd)
30093009
self.assertEqual(nd, nd1)
30103010
nd2 = copy.deepcopy(nd)
30113011
self.assertEqual(nd, nd2)
3012-
nd3 = pickle.loads(pickle.dumps(nd))
3013-
self.assertEqual(nd, nd3)
3012+
3013+
def test_pickle(self):
3014+
nd = self.module.NormalDist(37.5, 5.625)
3015+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
3016+
with self.subTest(proto=proto):
3017+
pickled = pickle.loads(pickle.dumps(nd, protocol=proto))
3018+
self.assertEqual(nd, pickled)
30143019

30153020
def test_hashability(self):
30163021
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)