]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41052: Opt out serialization/deserialization for _random.Random (GH-21002)
authorDong-hee Na <donghee.na92@gmail.com>
Sun, 21 Jun 2020 09:44:58 +0000 (18:44 +0900)
committerGitHub <noreply@github.com>
Sun, 21 Jun 2020 09:44:58 +0000 (18:44 +0900)
Lib/test/test_random.py
Misc/NEWS.d/next/Core and Builtins/2020-06-20-22-46-18.bpo-41052.46MPeF.rst [new file with mode: 0644]
Modules/_randommodule.c
Modules/clinic/_randommodule.c.h

index a3710f4aa48a682b6aadeb47d3417d25aacd9a27..a80e71e67e4c6ce037f6f1ed38f109198e8aa0e7 100644 (file)
@@ -5,6 +5,8 @@ import os
 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
@@ -372,6 +374,14 @@ class TestBasicOps:
             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
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-06-20-22-46-18.bpo-41052.46MPeF.rst b/Misc/NEWS.d/next/Core and Builtins/2020-06-20-22-46-18.bpo-41052.46MPeF.rst
new file mode 100644 (file)
index 0000000..82969bf
--- /dev/null
@@ -0,0 +1 @@
+Opt out serialization/deserialization for _random.Random
index 3e3139e4990cc35fe099136be27c518338b9e94c..b8bc0449c1b1b2e36db0755850860cf034817e5e 100644 (file)
@@ -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 */
 };
 
index b3cd435b6f20477fd2253a68764a49bdad1724a2..3322a370288c3f9c68dc79e78b147773e3a99eac 100644 (file)
@@ -109,4 +109,21 @@ _random_Random_getrandbits(RandomObject *self, PyObject *arg)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=cc8a23b2757dc6ba input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(_random_Random___reduce____doc__,
+"__reduce__($self, /)\n"
+"--\n"
+"\n");
+
+#define _RANDOM_RANDOM___REDUCE___METHODDEF    \
+    {"__reduce__", (PyCFunction)_random_Random___reduce__, METH_NOARGS, _random_Random___reduce____doc__},
+
+static PyObject *
+_random_Random___reduce___impl(RandomObject *self);
+
+static PyObject *
+_random_Random___reduce__(RandomObject *self, PyObject *Py_UNUSED(ignored))
+{
+    return _random_Random___reduce___impl(self);
+}
+/*[clinic end generated code: output=450f0961c2c92389 input=a9049054013a1b77]*/