Skip to content

Commit

Permalink
#885: ValueError is raised if a negative integer is passed to cpu_per…
Browse files Browse the repository at this point in the history
…cent() functions.
  • Loading branch information
giampaolo committed Sep 13, 2016
1 parent 7b3a5ab commit d1b4ae0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues

- #798: [Windows] Process.open_files() returns and empty list on Windows 10.
- #880: [Windows] Handle race condition inside psutil_net_connections.
- #885: ValueError is raised if a negative integer is passed to cpu_percent()
functions.


4.3.1 - 2016-09-01
Expand Down
8 changes: 8 additions & 0 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,13 +921,17 @@ def cpu_percent(self, interval=None):
>>>
"""
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 = cpu_count() or 1

if POSIX:
def timer():
return _timer() * num_cpus
else:
def timer():
return sum(cpu_times())

if blocking:
st1 = timer()
pt1 = self._proc.cpu_times()
Expand Down Expand Up @@ -1566,6 +1570,8 @@ def cpu_percent(interval=None, percpu=False):
global _last_cpu_times
global _last_per_cpu_times
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)

def calculate(t1, t2):
t1_all = sum(t1)
Expand Down Expand Up @@ -1639,6 +1645,8 @@ def cpu_times_percent(interval=None, percpu=False):
global _last_cpu_times_2
global _last_per_cpu_times_2
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)

def calculate(t1, t2):
nums = []
Expand Down
2 changes: 2 additions & 0 deletions psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ def test_cpu_percent(self):
self.assertLessEqual(percent, 100.0)
else:
self.assertGreaterEqual(percent, 0.0)
with self.assertRaises(ValueError):
p.cpu_percent(interval=-1)

def test_cpu_times(self):
times = psutil.Process().cpu_times()
Expand Down
4 changes: 4 additions & 0 deletions psutil/tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ def test_cpu_percent(self):
new = psutil.cpu_percent(interval=None)
self._test_cpu_percent(new, last, new)
last = new
with self.assertRaises(ValueError):
psutil.cpu_percent(interval=-1)

def test_per_cpu_percent(self):
last = psutil.cpu_percent(interval=0.001, percpu=True)
Expand All @@ -368,6 +370,8 @@ def test_per_cpu_percent(self):
for percent in new:
self._test_cpu_percent(percent, last, new)
last = new
with self.assertRaises(ValueError):
psutil.cpu_percent(interval=-1, percpu=True)

def test_cpu_times_percent(self):
last = psutil.cpu_times_percent(interval=0.001)
Expand Down

0 comments on commit d1b4ae0

Please sign in to comment.