diff -r 276515d2ceed Lib/test/test_os.py --- a/Lib/test/test_os.py Fri Nov 28 22:42:06 2014 +0100 +++ b/Lib/test/test_os.py Fri Nov 28 23:57:52 2014 +0100 @@ -27,6 +27,7 @@ import codecs import decimal import fractions import pickle +import sysconfig try: import threading except ImportError: @@ -1097,6 +1098,12 @@ class URandomTests(unittest.TestCase): data2 = self.get_urandom_subprocess(16) self.assertNotEqual(data1, data2) + +HAVE_GETENTROPY = (sysconfig.get_config_var('HAVE_GETENTROPY') == 1) + +@unittest.skipIf(HAVE_GETENTROPY, + "getentropy() does not use a file descriptor") +class URandomFDTests(unittest.TestCase): @unittest.skipUnless(resource, "test requires the resource module") def test_urandom_failure(self): # Check urandom() failing when it is not able to open /dev/random. diff -r 276515d2ceed Python/random.c --- a/Python/random.c Fri Nov 28 22:42:06 2014 +0100 +++ b/Python/random.c Fri Nov 28 23:57:52 2014 +0100 @@ -36,7 +36,7 @@ error: } /* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen - API. Return 0 on success, or -1 on error. */ + API. Return 0 on success, or raise an exception and return -1 on error. */ static int win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise) { @@ -66,10 +66,39 @@ win32_urandom(unsigned char *buffer, Py_ } return 0; } -#endif /* MS_WINDOWS */ +#elif HAVE_GETENTROPY +static void +py_getentropy_noraise(unsigned char *buffer, Py_ssize_t size) +{ + while (size > 0) { + Py_ssize_t len = Py_MIN(size, 256); + /* ignore errors */ + (void)getentropy(buffer, len); + buffer += len; + size -= len; + } +} -#ifndef MS_WINDOWS +/* Fill buffer with size pseudo-random bytes generated by getentropy(). + Return 0 on success, or raise an exception and return -1 on error. */ +static int +py_getentropy(unsigned char *buffer, Py_ssize_t size) +{ + while (size > 0) { + Py_ssize_t len = Py_MIN(size, 256); + int res = getentropy(buffer, len); + if (res < 0) { + PyErr_SetFromErrno(PyExc_OSError); + return -1; + } + buffer += len; + size -= len; + } + return 0; +} + +#else static struct { int fd; dev_t st_dev; @@ -201,7 +230,7 @@ dev_urandom_close(void) } } -#endif /* MS_WINDOWS */ +#endif /* HAVE_GETENTROPY */ /* Fill buffer with pseudo-random bytes generated by a linear congruent generator (LCG): @@ -242,6 +271,8 @@ int #ifdef MS_WINDOWS return win32_urandom((unsigned char *)buffer, size, 1); +#elif HAVE_GETENTROPY + return py_getentropy(buffer, size); #else return dev_urandom_python((char*)buffer, size); #endif @@ -287,6 +318,8 @@ void else { #ifdef MS_WINDOWS (void)win32_urandom(secret, secret_size, 0); +#elif HAVE_GETENTROPY + py_getentropy_noraise(secret, secret_size); #else dev_urandom_noraise(secret, secret_size); #endif @@ -301,6 +334,8 @@ void CryptReleaseContext(hCryptProv, 0); hCryptProv = 0; } +#elif HAVE_GETENTROPY + /* nothing to clean */ #else dev_urandom_close(); #endif diff -r 276515d2ceed configure --- a/configure Fri Nov 28 22:42:06 2014 +0100 +++ b/configure Fri Nov 28 23:57:52 2014 +0100 @@ -10665,7 +10665,7 @@ fi for ac_func in alarm accept4 setitimer getitimer bind_textdomain_codeset chown \ clock confstr ctermid dup3 execv faccessat fchmod fchmodat fchown fchownat \ fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \ - futimens futimes gai_strerror \ + futimens futimes gai_strerror getentropy \ getgrouplist getgroups getlogin getloadavg getpeername getpgid getpid \ getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \ if_nameindex \ diff -r 276515d2ceed configure.ac --- a/configure.ac Fri Nov 28 22:42:06 2014 +0100 +++ b/configure.ac Fri Nov 28 23:57:52 2014 +0100 @@ -3004,7 +3004,7 @@ fi AC_CHECK_FUNCS(alarm accept4 setitimer getitimer bind_textdomain_codeset chown \ clock confstr ctermid dup3 execv faccessat fchmod fchmodat fchown fchownat \ fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \ - futimens futimes gai_strerror \ + futimens futimes gai_strerror getentropy \ getgrouplist getgroups getlogin getloadavg getpeername getpgid getpid \ getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \ if_nameindex \ diff -r 276515d2ceed pyconfig.h.in --- a/pyconfig.h.in Fri Nov 28 22:42:06 2014 +0100 +++ b/pyconfig.h.in Fri Nov 28 23:57:52 2014 +0100 @@ -332,6 +332,9 @@ /* Define this if you have flockfile(), getc_unlocked(), and funlockfile() */ #undef HAVE_GETC_UNLOCKED +/* Define to 1 if you have the `getentropy' function. */ +#undef HAVE_GETENTROPY + /* Define to 1 if you have the `getgrouplist' function. */ #undef HAVE_GETGROUPLIST