summaryrefslogtreecommitdiffstats
path: root/Python/random.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-03-19 22:36:33 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-03-19 22:36:33 (GMT)
commitc9382eb7ae26dd72ad2ee9736584c2ad6d109085 (patch)
tree68866708e78a8c954842ba9ee0d4bafb88a9ac63 /Python/random.c
parent54799672dad9e6a10adde0f4af87d9f8165e4d7a (diff)
downloadcpython-c9382eb7ae26dd72ad2ee9736584c2ad6d109085.zip
cpython-c9382eb7ae26dd72ad2ee9736584c2ad6d109085.tar.gz
cpython-c9382eb7ae26dd72ad2ee9736584c2ad6d109085.tar.bz2
Issue #23707: On UNIX, os.urandom() now calls the Python signal handler when
read() is interrupted by a signal. dev_urandom_python() now calls _Py_read() helper instead of calling directly read().
Diffstat (limited to 'Python/random.c')
-rw-r--r--Python/random.c30
1 files changed, 11 insertions, 19 deletions
diff --git a/Python/random.c b/Python/random.c
index c924323..a281829 100644
--- a/Python/random.c
+++ b/Python/random.c
@@ -262,29 +262,21 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
}
}
- Py_BEGIN_ALLOW_THREADS
do {
- do {
- n = read(fd, buffer, (size_t)size);
- } while (n < 0 && errno == EINTR);
- if (n <= 0)
- break;
+ n = _Py_read(fd, buffer, (size_t)size);
+ if (n == -1)
+ return -1;
+ if (n == 0) {
+ PyErr_Format(PyExc_RuntimeError,
+ "Failed to read %zi bytes from /dev/urandom",
+ size);
+ return -1;
+ }
+
buffer += n;
- size -= (Py_ssize_t)n;
+ size -= n;
} while (0 < size);
- Py_END_ALLOW_THREADS
- if (n <= 0)
- {
- /* stop on error or if read(size) returned 0 */
- if (n < 0)
- PyErr_SetFromErrno(PyExc_OSError);
- else
- PyErr_Format(PyExc_RuntimeError,
- "Failed to read %zi bytes from /dev/urandom",
- size);
- return -1;
- }
return 0;
}