Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-41052: Opt out serialization/deserialization for _random.Random #21002

Merged
merged 1 commit into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
bpo-41052: Opt out serialization/deserialization for _random.Random
  • Loading branch information
corona10 committed Jun 20, 2020
commit ce4307bc1cbe9ad02fa4538fd741409ae7578639
10 changes: 10 additions & 0 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import time
import pickle
import warnings
import test.support

from functools import partial
from math import log, exp, pi, fsum, sin, factorial
from test import support
Expand Down Expand Up @@ -372,6 +374,14 @@ def test_pickling(self):
restoredseq = [newgen.random() for i in range(10)]
self.assertEqual(origseq, restoredseq)

@test.support.cpython_only
def test_bug_41052(self):
# _random.Random should not be allowed to serialization
import _random
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
r = _random.Random()
self.assertRaises(TypeError, pickle.dumps, r, proto)

def test_bug_1727780(self):
# verify that version-2-pickles can be loaded
# fine, whether they are created on 32-bit or 64-bit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Opt out serialization/deserialization for _random.Random
18 changes: 18 additions & 0 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,12 +535,30 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return (PyObject *)self;
}


/*[clinic input]

_random.Random.__reduce__

[clinic start generated code]*/

static PyObject *
_random_Random___reduce___impl(RandomObject *self)
/*[clinic end generated code: output=ddea0dcdb60ffd6d input=bd38ec35fd157e0f]*/
{
PyErr_Format(PyExc_TypeError,
"cannot pickle %s object",
Py_TYPE(self)->tp_name);
return NULL;
}

static PyMethodDef random_methods[] = {
_RANDOM_RANDOM_RANDOM_METHODDEF
_RANDOM_RANDOM_SEED_METHODDEF
_RANDOM_RANDOM_GETSTATE_METHODDEF
_RANDOM_RANDOM_SETSTATE_METHODDEF
_RANDOM_RANDOM_GETRANDBITS_METHODDEF
_RANDOM_RANDOM___REDUCE___METHODDEF
{NULL, NULL} /* sentinel */
};

Expand Down
19 changes: 18 additions & 1 deletion Modules/clinic/_randommodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.