]> granicus.if.org Git - python/commitdiff
Issue #22585: os.urandom() now releases the GIL when the getentropy() is used
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 30 Mar 2015 09:18:30 +0000 (11:18 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Mon, 30 Mar 2015 09:18:30 +0000 (11:18 +0200)
(OpenBSD 5.6+).

Python/random.c

index 93d300dae45a5cee48ec35c850a8f2d7d1705707..3f307cf62a9f81cf19b4ef48d27ca5d9b653f261 100644 (file)
@@ -78,16 +78,24 @@ py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
 {
     while (size > 0) {
         Py_ssize_t len = Py_MIN(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;
     }