Kaydet (Commit) 5f078ff7 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

SF bug #759889: Pickling of Random is broken

* Implement __reduce__() to support pickling.
* Add a test case to prove a successful roundtrip through pickle.
üst 663219a8
...@@ -123,6 +123,9 @@ class Random(_random.Random): ...@@ -123,6 +123,9 @@ class Random(_random.Random):
def __setstate__(self, state): # for pickle def __setstate__(self, state): # for pickle
self.setstate(state) self.setstate(state)
def __reduce__(self):
return self.__class__, (), self.getstate()
## -------------------- integer methods ------------------- ## -------------------- integer methods -------------------
def randrange(self, start, stop=None, step=1, int=int, default=None): def randrange(self, start, stop=None, step=1, int=int, default=None):
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import unittest import unittest
import random import random
import time import time
import pickle
from math import log, exp, sqrt, pi from math import log, exp, sqrt, pi
from sets import Set from sets import Set
from test import test_support from test import test_support
...@@ -102,6 +103,12 @@ class TestBasicOps(unittest.TestCase): ...@@ -102,6 +103,12 @@ class TestBasicOps(unittest.TestCase):
self.assertEqual(x1, x2) self.assertEqual(x1, x2)
self.assertEqual(y1, y2) self.assertEqual(y1, y2)
def test_pickling(self):
state = pickle.dumps(self.gen)
origseq = [self.gen.random() for i in xrange(10)]
newgen = pickle.loads(state)
restoredseq = [newgen.random() for i in xrange(10)]
self.assertEqual(origseq, restoredseq)
class WichmannHill_TestBasicOps(TestBasicOps): class WichmannHill_TestBasicOps(TestBasicOps):
gen = random.WichmannHill() gen = random.WichmannHill()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment