Author: Amaury Forgeot d'Arc <amaur...@gmail.com>
Branch: py3.6
Changeset: r93462:3f28eff2fcfc
Date: 2017-12-18 09:36 +0100
http://bitbucket.org/pypy/pypy/changeset/3f28eff2fcfc/

Log:    Since Python3.6, the seed() call to urandom() has been moved to the
        _random module.

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
@@ -1,13 +1,13 @@
 import time
 
-from pypy.interpreter.error import oefmt
+from pypy.interpreter.error import oefmt, OperationError
 from pypy.interpreter.typedef import TypeDef
 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.baseobjspace import W_Root
+from pypy.module.posix import interp_posix
 from rpython.rlib.rarithmetic import r_uint, intmask, widen
 from rpython.rlib import rbigint, rrandom, rstring
 
-
 def descr_new__(space, w_subtype, __args__):
     w_anything = __args__.firstarg()
     x = space.allocate_instance(W_Random, w_subtype)
@@ -25,14 +25,19 @@
         return space.newfloat(self._rnd.random())
 
     def seed(self, space, w_n=None):
-        if w_n is None:
-            w_n = space.newint(int(time.time()))
+        if space.is_none(w_n):
+            # TODO: Use a non-blocking version of urandom
+            try:
+                w_n = interp_posix.urandom(space, 8)
+            except OperationError as e:
+                if not e.match(space, space.w_OSError):
+                    raise
+                w_n = space.newint(int(time.time() * 256))
+        if space.isinstance_w(w_n, space.w_int):
+            w_n = space.abs(w_n)
         else:
-            if space.isinstance_w(w_n, space.w_int):
-                w_n = space.abs(w_n)
-            else:
-                n = space.hash_w(w_n)
-                w_n = space.newint(r_uint(n))
+            n = space.hash_w(w_n)
+            w_n = space.newint(r_uint(n))
         key = []
         w_one = space.newint(1)
         w_two = space.newint(2)
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
@@ -86,9 +86,9 @@
         rnd = _random.Random()
         rnd.seed()
         state1 = rnd.getstate()
-        import time; time.sleep(1.1)     # must be at least 1 second here
-        rnd.seed()                       # (note that random.py overrides
-        state2 = rnd.getstate()          # seed() to improve the resolution)
+        import time; time.sleep(0.01)
+        rnd.seed()
+        state2 = rnd.getstate()
         assert state1 != state2
 
     def test_randbits(self):
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to