diff options
author | Christopher Wilcox <git@crwilcox.com> | 2017-09-01 19:28:47 (GMT) |
---|---|---|
committer | Antoine Pitrou <pitrou@free.fr> | 2017-09-01 19:28:47 (GMT) |
commit | 58521fdba1657f6553a1ead5cbaa100967a167b3 (patch) | |
tree | 56c7a06b75dae625e04022292c662cd9fb68e8a6 /Modules | |
parent | ea767915f7476c1fe97f7b1a53304d57f105bdd2 (diff) | |
download | cpython-58521fdba1657f6553a1ead5cbaa100967a167b3.zip cpython-58521fdba1657f6553a1ead5cbaa100967a167b3.tar.gz cpython-58521fdba1657f6553a1ead5cbaa100967a167b3.tar.bz2 |
bpo-30581: Windows: os.cpu_count() returns wrong number of processors (#2934) (#3267)
* Fixes #30581 by adding a path to use newer GetMaximumProcessorCount API on Windows calls to os.cpu_count()
* Add NEWS.d entry for bpo-30581, os.cpu_count on Windows.
* Tweak NEWS entry
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/posixmodule.c | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 111e4c9..ead2ea9 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -11028,9 +11028,22 @@ os_cpu_count_impl(PyObject *module) { int ncpu = 0; #ifdef MS_WINDOWS - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); - ncpu = sysinfo.dwNumberOfProcessors; + /* Vista is supported and the GetMaximumProcessorCount API is Win7+ + Need to fallback to Vista behavior if this call isn't present */ + HINSTANCE hKernel32; + hKernel32 = GetModuleHandleW(L"KERNEL32"); + + static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL; + *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32, + "GetMaximumProcessorCount"); + if (_GetMaximumProcessorCount != NULL) { + ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS); + } + else { + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + ncpu = sysinfo.dwNumberOfProcessors; + } #elif defined(__hpux) ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |