Skip to content

statistics.NormalDist fails to be pickled with 0 and 1 protocols #99155

Closed
@sobolevn

Description

@sobolevn

Repro:

» ./python.exe
Python 3.12.0a1+ (heads/cover-pysequence-slice-dirty:04cb2ecd5d, Nov  5 2022, 16:01:29) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle, statistics
>>> n = statistics.NormalDist(37.5, 5.625)
>>> pickle.dumps(n, protocol=0)  # or `1`
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sobolev/Desktop/cpython/Lib/copyreg.py", line 94, in _reduce_ex
    raise TypeError("a class that defines __slots__ without "
TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled

This is actually tested in test_statistics like this:

nd3 = pickle.loads(pickle.dumps(nd))
self.assertEqual(nd, nd3)

But, it is only testing default protocol and is missing for proto in range(pickle.HIGHEST_PROTOCOL + 1): part. Note: this is the only place in all of the CPython tests, where protocol= is not passed. All other places do have the full check.

It looks like a bug to me and I will send a PR with the fix.
CC @rhettinger

Metadata

Metadata

Labels

stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions