[pypy-commit] pypy default: Persist the file descriptor used by os.urandom, which speeds up individual calls to it
alex_gaynor
noreply at buildbot.pypy.org
Sat Apr 19 03:03:51 CEST 2014
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch:
Changeset: r70764:6810f401d08e
Date: 2014-04-18 18:03 -0700
http://bitbucket.org/pypy/pypy/changeset/6810f401d08e/
Log: Persist the file descriptor used by os.urandom, which speeds up
individual calls to it
diff --git a/rpython/rlib/rurandom.py b/rpython/rlib/rurandom.py
--- a/rpython/rlib/rurandom.py
+++ b/rpython/rlib/rurandom.py
@@ -5,11 +5,13 @@
import os, sys
import errno
+from rpython.rtyper.lltypesystem import lltype, rffi
+
+
if sys.platform == 'win32':
from rpython.rlib import rwin32
from rpython.translator.tool.cbuild import ExternalCompilationInfo
from rpython.rtyper.tool import rffi_platform
- from rpython.rtyper.lltypesystem import lltype, rffi
eci = ExternalCompilationInfo(
includes = ['windows.h', 'wincrypt.h'],
@@ -81,25 +83,28 @@
return buf.str(n)
else: # Posix implementation
def init_urandom():
- pass
+ """NOT_RPYTHON
+ Return an array of one int, initialized to 0.
+ It is filled automatically the first time urandom() is called.
+ """
+ return lltype.malloc(rffi.CArray(lltype.Signed), 1,
+ immortal=True, zero=True)
def urandom(context, n):
"Read n bytes from /dev/urandom."
result = ''
if n == 0:
return result
- fd = os.open("/dev/urandom", os.O_RDONLY, 0777)
- try:
- while n > 0:
- try:
- data = os.read(fd, n)
- except OSError, e:
- if e.errno != errno.EINTR:
- raise
- data = ''
- result += data
- n -= len(data)
- finally:
- os.close(fd)
+ if not context[0]:
+ context[0] = os.open("/dev/urandom", os.O_RDONLY, 0777)
+ while n > 0:
+ try:
+ data = os.read(context[0], n)
+ except OSError, e:
+ if e.errno != errno.EINTR:
+ raise
+ data = ''
+ result += data
+ n -= len(data)
return result
More information about the pypy-commit
mailing list