Skip to content

Commit

Permalink
[3.10] [ GH-99155: Fix NormalDist pickle with 0 and 1 protocols (
Browse files Browse the repository at this point in the history
  • Loading branch information
miss-islington committed Nov 7, 2022
1 parent 1b5a62b commit ea2316a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,3 +1265,9 @@ def __hash__(self):

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
11 changes: 8 additions & 3 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2880,14 +2880,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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :class:`statistics.NormalDist` pickle with ``0`` and ``1`` protocols.

0 comments on commit ea2316a

Please sign in to comment.