Rebase 00250-getentropy.patch for 3.5
This commit is contained in:
parent
3ab335d62c
commit
3bc162cdb4
@ -1,7 +1,8 @@
|
||||
diff -r ee1390c9b585 Python/random.c
|
||||
--- a/Python/random.c Wed Jan 04 12:02:30 2017 +0100
|
||||
+++ b/Python/random.c Wed Jan 04 18:32:21 2017 +0100
|
||||
@@ -77,45 +77,8 @@ win32_urandom(unsigned char *buffer, Py_
|
||||
diff --git a/Python/random.c b/Python/random.c
|
||||
index 07dacfe..a4f7f40 100644
|
||||
--- a/Python/random.c
|
||||
+++ b/Python/random.c
|
||||
@@ -75,46 +75,7 @@ win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -13,29 +14,29 @@ diff -r ee1390c9b585 Python/random.c
|
||||
-/* Fill buffer with size pseudo-random bytes generated by getentropy().
|
||||
- Return 0 on success, or raise an exception and return -1 on error.
|
||||
-
|
||||
- If raise is zero, don't raise an exception on error. */
|
||||
- If fatal is nonzero, call Py_FatalError() instead of raising an exception
|
||||
- on error. */
|
||||
-static int
|
||||
-py_getentropy(char *buffer, Py_ssize_t size, int raise)
|
||||
-py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
|
||||
-{
|
||||
- while (size > 0) {
|
||||
- Py_ssize_t len = Py_MIN(size, 256);
|
||||
- int res;
|
||||
+#else /* !MS_WINDOWS */
|
||||
|
||||
- if (raise) {
|
||||
-
|
||||
- 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) {
|
||||
- if (raise) {
|
||||
- PyErr_SetFromErrno(PyExc_OSError);
|
||||
- }
|
||||
- return -1;
|
||||
- if (res < 0)
|
||||
- Py_FatalError("getentropy() failed");
|
||||
- }
|
||||
-
|
||||
- buffer += len;
|
||||
@ -45,10 +46,11 @@ diff -r ee1390c9b585 Python/random.c
|
||||
-}
|
||||
-
|
||||
-#else
|
||||
+#else /* !MS_WINDOWS */
|
||||
|
||||
#if defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL)
|
||||
#define PY_GETRANDOM 1
|
||||
@@ -217,6 +180,59 @@ py_getrandom(void *buffer, Py_ssize_t si
|
||||
@@ -209,6 +170,59 @@ py_getrandom(void *buffer, Py_ssize_t size, int raise)
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@ -108,40 +110,35 @@ diff -r ee1390c9b585 Python/random.c
|
||||
#endif
|
||||
|
||||
static struct {
|
||||
@@ -236,7 +252,7 @@ dev_urandom(char *buffer, Py_ssize_t siz
|
||||
{
|
||||
@@ -264,7 +278,7 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
|
||||
int fd;
|
||||
Py_ssize_t n;
|
||||
struct _Py_stat_struct st;
|
||||
-#ifdef PY_GETRANDOM
|
||||
+#if defined(PY_GETRANDOM) || defined(PY_GETENTROPY)
|
||||
int res;
|
||||
#endif
|
||||
|
||||
@@ -244,17 +260,20 @@ dev_urandom(char *buffer, Py_ssize_t siz
|
||||
@@ -273,12 +287,16 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
|
||||
|
||||
#ifdef PY_GETRANDOM
|
||||
res = py_getrandom(buffer, size, blocking, raise);
|
||||
res = py_getrandom(buffer, size, 1);
|
||||
+#elif defined(PY_GETENTROPY)
|
||||
+ res = py_getentropy(buffer, size, raise);
|
||||
+ res = py_getentropy(buffer, size, 1);
|
||||
+#endif
|
||||
+#if defined(PY_GETRANDOM) || defined(PY_GETENTROPY)
|
||||
if (res < 0) {
|
||||
if (res < 0)
|
||||
return -1;
|
||||
}
|
||||
if (res == 1) {
|
||||
if (res == 1)
|
||||
return 0;
|
||||
}
|
||||
- /* getrandom() failed with ENOSYS or EPERM,
|
||||
- fall back on reading /dev/urandom */
|
||||
- /* getrandom() is not supported by the running kernel, fall back
|
||||
- * on reading /dev/urandom */
|
||||
+ /* function failed with ENOSYS or EPERM, fall back on reading
|
||||
+ from /dev/urandom */
|
||||
#endif
|
||||
|
||||
-
|
||||
if (raise) {
|
||||
struct _Py_stat_struct st;
|
||||
|
||||
@@ -349,8 +368,8 @@ dev_urandom_close(void)
|
||||
if (urandom_cache.fd >= 0) {
|
||||
@@ -349,8 +367,8 @@ dev_urandom_close(void)
|
||||
urandom_cache.fd = -1;
|
||||
}
|
||||
}
|
||||
@ -151,16 +148,16 @@ diff -r ee1390c9b585 Python/random.c
|
||||
|
||||
/* Fill buffer with pseudo-random bytes generated by a linear congruent
|
||||
generator (LCG):
|
||||
@@ -395,8 +414,6 @@ pyurandom(void *buffer, Py_ssize_t size,
|
||||
@@ -391,8 +409,6 @@ _PyOS_URandom(void *buffer, Py_ssize_t size)
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
return win32_urandom((unsigned char *)buffer, size, raise);
|
||||
return win32_urandom((unsigned char *)buffer, size, 1);
|
||||
-#elif defined(PY_GETENTROPY)
|
||||
- return py_getentropy(buffer, size, raise);
|
||||
- return py_getentropy(buffer, size, 0);
|
||||
#else
|
||||
return dev_urandom(buffer, size, blocking, raise);
|
||||
return dev_urandom_python((char*)buffer, size);
|
||||
#endif
|
||||
@@ -491,8 +508,6 @@ void
|
||||
@@ -454,8 +470,6 @@ _PyRandom_Fini(void)
|
||||
CryptReleaseContext(hCryptProv, 0);
|
||||
hCryptProv = 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user