summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2018-03-28 21:21:40 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2018-03-28 21:21:40 +0200
commitbc71d9d3c76ea49963720fc8f7f56f249a99d241 (patch)
tree3cb7a01ed66a2b84d9e20748686148470cd7c058
parenta67d6c683c5e134e41aaed6001365dba664896b7 (diff)
downloadpsutil-bc71d9d3c76ea49963720fc8f7f56f249a99d241.tar.gz
return None if cpu_count() is undetermined + add mock test
-rw-r--r--psutil/_psutil_windows.c14
-rw-r--r--psutil/_pswindows.py4
-rwxr-xr-xpsutil/tests/test_windows.py8
3 files changed, 15 insertions, 11 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 130ffd41..53c4a33c 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -591,7 +591,7 @@ psutil_cpu_count(PyObject *self, PyObject *args) {
"GetLogicalProcessorInformation");
if (glpi == NULL) {
psutil_debug("failed loading GetLogicalProcessorInformation()");
- goto return_none;
+ goto return_null_result;
}
while (1) {
@@ -608,7 +608,7 @@ psutil_cpu_count(PyObject *self, PyObject *args) {
}
}
else {
- goto return_none;
+ goto return_null_result;
}
}
else {
@@ -628,16 +628,12 @@ psutil_cpu_count(PyObject *self, PyObject *args) {
}
free(buffer);
- if (logicalProcessorCount == 0)
- goto return_none;
- else
- return Py_BuildValue("II", logicalProcessorCount, processorCoreCount);
+ return Py_BuildValue("II", logicalProcessorCount, processorCoreCount);
-return_none:
- // mimic os.cpu_count()
+return_null_result:
if (buffer != NULL)
free(buffer);
- Py_RETURN_NONE;
+ return Py_BuildValue("ii", 0, 0);
}
diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py
index 56546b43..078f225a 100644
--- a/psutil/_pswindows.py
+++ b/psutil/_pswindows.py
@@ -284,12 +284,12 @@ def per_cpu_times():
def cpu_count_logical():
"""Return the number of logical CPUs in the system."""
- return cext.cpu_count()[0]
+ return cext.cpu_count()[0] or None
def cpu_count_physical():
"""Return the number of physical CPU cores in the system."""
- return cext.cpu_count()[1]
+ return cext.cpu_count()[1] or None
def cpu_stats():
diff --git a/psutil/tests/test_windows.py b/psutil/tests/test_windows.py
index 32c46f67..252201c5 100755
--- a/psutil/tests/test_windows.py
+++ b/psutil/tests/test_windows.py
@@ -92,6 +92,14 @@ class TestSystemAPIs(unittest.TestCase):
psutil_value = psutil.cpu_count()
self.assertEqual(sys_value, psutil_value)
+ def test_cpu_count_mocked(self):
+ # Emulate a case where the C extension is unable to determine
+ # CPU count.
+ with mock.patch('psutil._pswindows.cext.cpu_count',
+ return_value=(0, 0)):
+ self.assertIsNone(psutil.cpu_count(logical=True))
+ self.assertIsNone(psutil.cpu_count(logical=False))
+
def test_cpu_freq(self):
w = wmi.WMI()
proc = w.Win32_Processor()[0]