Author: Armin Rigo <ar...@tunes.org>
Branch: 
Changeset: r57259:1feab101071c
Date: 2012-09-09 22:55 +0200
http://bitbucket.org/pypy/pypy/changeset/1feab101071c/

Log:    Hopefully fix issue1254 (bad usage of _freeze_, and missing lazy
        initialization).

diff --git a/pypy/module/posix/interp_posix.py 
b/pypy/module/posix/interp_posix.py
--- a/pypy/module/posix/interp_posix.py
+++ b/pypy/module/posix/interp_posix.py
@@ -500,7 +500,9 @@
     def _freeze_(self):
         # don't capture the environment in the translated pypy
         self.space.call_method(self.w_environ, 'clear')
-        self.random_context = None
+        # also reset random_context to a fresh new context (empty so far,
+        # to be filled at run-time by rurandom.urandom())
+        self.random_context = rurandom.init_urandom()
         return True
 
 def get(space):
diff --git a/pypy/rlib/rurandom.py b/pypy/rlib/rurandom.py
--- a/pypy/rlib/rurandom.py
+++ b/pypy/rlib/rurandom.py
@@ -43,23 +43,28 @@
         compilation_info=eci)
 
     def init_urandom():
-        "Acquire context."
-        # This handle is never explicitly released. The operating
-        # system will release it when the process terminates.
-        with lltype.scoped_alloc(rffi.CArray(HCRYPTPROV), 1) as ptr:
+        """NOT_RPYTHON
+        Return an array of one HCRYPTPROV, initialized to NULL.
+        It is filled automatically the first time urandom() is called.
+        """
+        return lltype.malloc(rffi.CArray(HCRYPTPROV), 1,
+                             immortal=True, zero=True)
+
+    def urandom(context, n):
+        provider = context[0]
+        if not provider:
+            # This handle is never explicitly released. The operating
+            # system will release it when the process terminates.
             if not CryptAcquireContext(
-                ptr, None, None,
+                context, None, None,
                 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT):
                 raise rwin32.lastWindowsError("CryptAcquireContext")
-            return ptr[0]
-
-    def urandom(context, n):
-        provider = context
+            provider = context[0]
         # TODO(win64) This is limited to 2**31
         with lltype.scoped_alloc(rffi.CArray(rwin32.BYTE), n,
                                  zero=True, # zero seed
                                  ) as buf:
-            if not CryptGenRandom(rffi.cast(HCRYPTPROV, provider), n, buf):
+            if not CryptGenRandom(provider, n, buf):
                 raise rwin32.lastWindowsError("CryptGenRandom")
 
             return rffi.charpsize2str(rffi.cast(rffi.CCHARP, buf), n)
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to