Author: mattip <[email protected]>
Branch:
Changeset: r76275:a2a352a3b535
Date: 2015-03-07 22:51 +0200
http://bitbucket.org/pypy/pypy/changeset/a2a352a3b535/
Log: test, fix random.getstate() to return longs so that repr(state) is
identical w/cpython
diff --git a/pypy/module/_random/interp_random.py
b/pypy/module/_random/interp_random.py
--- a/pypy/module/_random/interp_random.py
+++ b/pypy/module/_random/interp_random.py
@@ -4,7 +4,7 @@
from pypy.interpreter.typedef import TypeDef
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.baseobjspace import W_Root
-from rpython.rlib.rarithmetic import r_uint, intmask
+from rpython.rlib.rarithmetic import r_uint, intmask, widen
from rpython.rlib import rbigint, rrandom, rstring
@@ -54,8 +54,8 @@
def getstate(self, space):
state = [None] * (rrandom.N + 1)
for i in range(rrandom.N):
- state[i] = space.newint(intmask(self._rnd.state[i]))
- state[rrandom.N] = space.newint(self._rnd.index)
+ state[i] = space.wrap(widen(self._rnd.state[i]))
+ state[rrandom.N] = space.newlong(self._rnd.index)
return space.newtuple(state)
def setstate(self, space, w_state):
diff --git a/pypy/module/_random/test/test_random.py
b/pypy/module/_random/test/test_random.py
--- a/pypy/module/_random/test/test_random.py
+++ b/pypy/module/_random/test/test_random.py
@@ -41,6 +41,17 @@
# does not crash
rnd1.setstate((-1, ) * 624 + (0, ))
+ def test_state_repr(self):
+ # since app-level jumpahead salts with repr(state),
+ # it is important the repr is consistent with cpython
+ import _random
+ rnd = _random.Random()
+ rnd.seed(1234)
+ state = rnd.getstate()
+ s = repr(state)
+ assert len(s) == 7956
+ assert s.count('L') == 625
+
def test_seed(self):
import _random, sys
rnd = _random.Random()
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit