diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-19 20:32:02 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-19 20:32:02 +0200 |
commit | e85a44a9e61c7c5cb06a3bfc109d0a64027098e4 (patch) | |
tree | 916f617603587a206589be363692c97996e5cafc | |
parent | 3a30cfd9827ab426cad2c17f95f7e633be8eb2fc (diff) | |
parent | eb6ab10d12eb4ecc636e8bac41781db36ac8a12b (diff) | |
download | psutil-e85a44a9e61c7c5cb06a3bfc109d0a64027098e4.tar.gz |
Merge branch 'master' into win-openprocess-fix-2
-rwxr-xr-x | .ci/travis/run.sh | 6 | ||||
-rw-r--r-- | HISTORY.rst | 7 | ||||
-rw-r--r-- | appveyor.yml | 2 | ||||
-rw-r--r-- | psutil/__init__.py | 10 | ||||
-rw-r--r-- | psutil/_psutil_linux.c | 2 | ||||
-rwxr-xr-x | psutil/tests/test_process.py | 6 | ||||
-rwxr-xr-x | psutil/tests/test_system.py | 12 |
7 files changed, 32 insertions, 13 deletions
diff --git a/.ci/travis/run.sh b/.ci/travis/run.sh index b8526dff..b27e6695 100755 --- a/.ci/travis/run.sh +++ b/.ci/travis/run.sh @@ -20,14 +20,14 @@ python setup.py develop # run tests (with coverage) if [[ $PYVER == '2.7' ]] && [[ "$(uname -s)" != 'Darwin' ]]; then - python -Wa -m coverage run psutil/tests/__main__.py + PSUTIL_TESTING=1 python -Wa -m coverage run psutil/tests/__main__.py else - python -Wa psutil/tests/__main__.py + PSUTIL_TESTING=1 python -Wa psutil/tests/__main__.py fi if [ "$PYVER" == "2.7" ] || [ "$PYVER" == "3.6" ]; then # run mem leaks test - python -Wa psutil/tests/test_memory_leaks.py + PSUTIL_TESTING=1 python -Wa psutil/tests/test_memory_leaks.py # run linter (on Linux only) if [[ "$(uname -s)" != 'Darwin' ]]; then python -Wa -m flake8 diff --git a/HISTORY.rst b/HISTORY.rst index bdca8f24..bb20811a 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -50,7 +50,9 @@ - 1047_: [Windows] Process username(): memory leak in case exception is thrown. - 1048_: [Windows] users()'s host field report an invalid IP address. - 1050_: [Windows] Process.memory_maps memory() leaks memory. -- 1055_: cpu_count() is no longer cached. +- 1055_: cpu_count() is no longer cached; this is useful on systems such as + Linux where CPUs can be disabled at runtime. This also reflects on + Process.cpu_percent() which no longer uses the cache. - 1058_: fixed Python warnings. - 1062_: disk_io_counters() and net_io_counters() raise TypeError if no disks or NICs are installed on the system. @@ -68,6 +70,9 @@ Oleksii Shevchuk) - 1079_: [FreeBSD] net_connections() didn't list locally connected sockets. (patch by Gleb Smirnoff) +- 1085_: cpu_count() return value is now checked and forced to None if <= 1. +- 1087_: Process.cpu_percent() guard against cpu_count() returning None and + assumes 1 instead. **Porting notes** diff --git a/appveyor.yml b/appveyor.yml index 1390f962..d4671784 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -91,7 +91,7 @@ build: off test_script: - "%WITH_COMPILER% %PYTHON%/python -V" - - "%WITH_COMPILER% %PYTHON%/python psutil/tests/__main__.py" + - "set PSUTIL_TESTING=1 && %WITH_COMPILER% %PYTHON%/python psutil/tests/__main__.py" after_test: - "%WITH_COMPILER% %PYTHON%/python setup.py bdist_wheel" diff --git a/psutil/__init__.py b/psutil/__init__.py index a05d6249..fc45abf1 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -209,7 +209,6 @@ AF_LINK = _psplatform.AF_LINK POWER_TIME_UNLIMITED = _common.POWER_TIME_UNLIMITED POWER_TIME_UNKNOWN = _common.POWER_TIME_UNKNOWN _TOTAL_PHYMEM = None -_NUM_CPUS = None _timer = getattr(time, 'monotonic', time.time) @@ -1043,7 +1042,7 @@ class Process(object): blocking = interval is not None and interval > 0.0 if interval is not None and interval < 0: raise ValueError("interval is not positive (got %r)" % interval) - num_cpus = _NUM_CPUS or cpu_count() + num_cpus = cpu_count() or 1 def timer(): return _timer() * num_cpus @@ -1643,12 +1642,11 @@ def cpu_count(logical=True): >>> psutil.cpu_count.cache_clear() """ - global _NUM_CPUS if logical: - _NUM_CPUS = _psplatform.cpu_count_logical() - return _NUM_CPUS + ret = _psplatform.cpu_count_logical() else: - return _psplatform.cpu_count_physical() + ret = _psplatform.cpu_count_physical() + return ret if ret >= 1 else None def cpu_times(percpu=False): diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c index e262ac70..a15ebe5c 100644 --- a/psutil/_psutil_linux.c +++ b/psutil/_psutil_linux.c @@ -203,8 +203,6 @@ psutil_disk_partitions(PyObject *self, PyObject *args) { if (py_retlist == NULL) return NULL; - psutil_testing(); - // MOUNTED constant comes from mntent.h and it's == '/etc/mtab' Py_BEGIN_ALLOW_THREADS file = setmntent(MOUNTED, "r"); diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index 3410ec0b..cab5a2fe 100755 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -246,6 +246,12 @@ class TestProcess(unittest.TestCase): with self.assertRaises(ValueError): p.cpu_percent(interval=-1) + def test_cpu_percent_numcpus_none(self): + # See: https://github.com/giampaolo/psutil/issues/1087 + with mock.patch('psutil.cpu_count', return_value=None) as m: + psutil.Process().cpu_percent() + assert m.called + def test_cpu_times(self): times = psutil.Process().cpu_times() assert (times.user > 0.0) or (times.system > 0.0), times diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py index fed7a222..e93bb6b5 100755 --- a/psutil/tests/test_system.py +++ b/psutil/tests/test_system.py @@ -270,6 +270,18 @@ class TestSystemAPIs(unittest.TestCase): self.assertGreaterEqual(physical, 1) self.assertGreaterEqual(logical, physical) + def test_cpu_count_none(self): + # https://github.com/giampaolo/psutil/issues/1085 + for val in (-1, 0, None): + with mock.patch('psutil._psplatform.cpu_count_logical', + return_value=val) as m: + self.assertIsNone(psutil.cpu_count()) + assert m.called + with mock.patch('psutil._psplatform.cpu_count_physical', + return_value=val) as m: + self.assertIsNone(psutil.cpu_count(logical=False)) + assert m.called + def test_cpu_times(self): # Check type, value >= 0, str(). total = 0 |