Skip to content

Commit

Permalink
bpo-41052: Opt out serialization/deserialization for _random.Random (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 authored and fasihahmad committed Jun 29, 2020
1 parent 603fafc commit 5b04e58
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
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.

0 comments on commit 5b04e58

Please sign in to comment.