[pypy-commit] pypy stdlib-unification/py3k: implement applevel urandom for posix os

RonnyPfannschmidt noreply at buildbot.pypy.org
Wed Apr 18 18:19:12 CEST 2012


Author: Ronny Pfannschmidt <Ronny.Pfannschmidt at gmx.de>
Branch: stdlib-unification/py3k
Changeset: r54506:23aebe5793a4
Date: 2012-04-18 18:18 +0200
http://bitbucket.org/pypy/pypy/changeset/23aebe5793a4/

Log:	implement applevel urandom for posix os

diff --git a/pypy/module/posix/__init__.py b/pypy/module/posix/__init__.py
--- a/pypy/module/posix/__init__.py
+++ b/pypy/module/posix/__init__.py
@@ -35,8 +35,10 @@
     appleveldefs = {
     'error'      : 'app_posix.error',
     'stat_result': 'app_posix.stat_result',
+    'urandom': 'app_posix.urandom',
     }
     if os.name == 'nt':
+        del appleveldefs['urandom'] # at interp on win32
         appleveldefs.update({
                 'popen2' : 'app_posix.popen2',
                 'popen3' : 'app_posix.popen3',
diff --git a/pypy/module/posix/app_posix.py b/pypy/module/posix/app_posix.py
--- a/pypy/module/posix/app_posix.py
+++ b/pypy/module/posix/app_posix.py
@@ -1,5 +1,4 @@
 # NOT_RPYTHON
-
 from _structseq import structseqtype, structseqfield
 
 # XXX we need a way to access the current module's globals more directly...
@@ -102,6 +101,19 @@
         from _pypy_wait import wait4
         return wait4(pid, options)
 
+    def urandom(n):
+        """urandom(n) -> str
+
+        Return a string of n random bytes suitable for cryptographic use.
+
+        """
+        try:
+            with open('/dev/urandom', 'rb', buffering=0) as fd:
+                return fd.read(n)
+        except (OSError, IOError):
+            raise NotImplementedError("/dev/urandom (or equivalent) not found")
+
+
 else:
     # Windows implementations
 


More information about the pypy-commit mailing list