[Python-checkins] cpython (3.4): Issue #22585: os.urandom() now releases the GIL when the getentropy() is used

victor.stinner python-checkins at python.org
Mon Mar 30 11:20:15 CEST 2015


https://hg.python.org/cpython/rev/9d3013a383eb
changeset:   95293:9d3013a383eb
branch:      3.4
parent:      95282:442ebebad516
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Mar 30 11:18:30 2015 +0200
summary:
  Issue #22585: os.urandom() now releases the GIL when the getentropy() is used
(OpenBSD 5.6+).

files:
  Python/random.c |  20 ++++++++++++++------
  1 files changed, 14 insertions(+), 6 deletions(-)


diff --git a/Python/random.c b/Python/random.c
--- a/Python/random.c
+++ b/Python/random.c
@@ -78,16 +78,24 @@
 {
     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;
     }

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list