]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #23115: os.urandom() now releases the GIL when the getentropy() is used
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 30 Mar 2015 09:22:13 +0000 (11:22 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Mon, 30 Mar 2015 09:22:13 +0000 (11:22 +0200)
(OpenBSD 5.6+).

Python/random.c

index ad8993d9533eebf4416395136300ee3057e13a5a..d94f89a562c63cd2bf2e274c0aa7982d4d87f67f 100644 (file)
@@ -103,16 +103,24 @@ py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
 {
     while (size > 0) {
         Py_ssize_t len = size < 256 ? size : 256;
-        int res = getentropy(buffer, len);
-        if (res < 0) {
-            if (fatal) {
-                Py_FatalError("getentropy() failed");
-            }
-            else {
+        int res;
+
+        if (!fatal) {
+            Py_BEGIN_ALLOW_THREADS
+            res = getentropy(buffer, len);
+            Py_END_ALLOW_THREADS
+
+            if (res < 0) {
                 PyErr_SetFromErrno(PyExc_OSError);
                 return -1;
             }
         }
+        else {
+            res = getentropy(buffer, len);
+            if (res < 0)
+                Py_FatalError("getentropy() failed");
+        }
+
         buffer += len;
         size -= len;
     }