Skip to content

BUG: np.random.Generator cannot be unpickled using 3rd party bit generators #22012

@bashtage

Description

@bashtage

Describe the issue:

The unpickling mechanism uses a hard-coded list of bit generators when unpickling a generator instance. Because of this, it cannot be used with 3rd party bit generators, or, if the name of the 3rd party bit generator is the same as the NumPy bit generator, it will unpickle using a different class than the original bit generator class

Reproduce the code example:

import numpy as np
import randomgen as rg
import pickle

# Generator(MT64)
g = np.random.Generator(rg.MT64())
pickle.loads(pickle.dumps(g))

Error message:

Traceback (most recent call last):
  File "C:\git\randomgen\exp.py", line 7, in <module>
    pickle.loads(pickle.dumps(g))
  File "c:\git\numpy\numpy\random\_pickle.py", line 52, in __generator_ctor
    raise ValueError(
ValueError: MT64  is not a known NumPy BitGenerator class.

NumPy/Python version information:

1.24.0.dev0+559.g5ba36b700 3.10.4 | packaged by conda-forge | (main, Mar 30 2022, 08:38:02) [MSC v.1916 64 bit (AMD64)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions