[Python-checkins] bpo-30581: Windows: os.cpu_count() returns wrong number of processors (#2934)

Antoine Pitrou webhook-mailer at python.org
Wed Aug 30 05:01:13 EDT 2017


https://github.com/python/cpython/commit/c67bae04780f9d7590f9f91b4ee5f31c5d75b3c3
commit: c67bae04780f9d7590f9f91b4ee5f31c5d75b3c3
branch: master
author: Christopher Wilcox <git at crwilcox.com>
committer: Antoine Pitrou <pitrou at free.fr>
date: 2017-08-30T11:01:08+02:00
summary:

bpo-30581: Windows: os.cpu_count() returns wrong number of processors (#2934)

* 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

files:
A Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst
M Modules/posixmodule.c

diff --git a/Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst b/Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst
new file mode 100644
index 00000000000..6591fa0df00
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst
@@ -0,0 +1,2 @@
+os.cpu_count() now returns the correct number of processors on Windows
+when the number of logical processors is greater than 64.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index f0577874334..e8138d5d3c3 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -11174,9 +11174,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)



More information about the Python-checkins mailing list