summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2015-06-20 03:03:38 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2015-06-20 03:03:38 +0200
commit3163e7ec7df88b6fdfd9b1fcdec29aa0fef695e8 (patch)
tree379621618ef38631a73ba40b73b4401a7789dbec
parent48f898afa584ac9eed209b63e46a93f1876278f4 (diff)
downloadpsutil-3163e7ec7df88b6fdfd9b1fcdec29aa0fef695e8.tar.gz
(linux) proc io nice: provide better error messages
-rw-r--r--psutil/_pslinux.py28
-rw-r--r--test/test_psutil.py10
2 files changed, 29 insertions, 9 deletions
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index 3f1ff721..f3b5b080 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -1044,25 +1044,35 @@ class Process(object):
@wrap_exceptions
def ionice_set(self, ioclass, value):
+ if value is not None:
+ if not PY3 and not isinstance(value, (int, long)):
+ msg = "value argument is not an integer (gor %r)" % value
+ raise TypeError(msg)
+ if not 0 <= value <= 8:
+ raise ValueError(
+ "value argument range expected is between 0 and 8")
+
if ioclass in (IOPRIO_CLASS_NONE, None):
if value:
- msg = "can't specify value with IOPRIO_CLASS_NONE"
+ msg = "can't specify value with IOPRIO_CLASS_NONE " \
+ "(got %r)" % value
raise ValueError(msg)
ioclass = IOPRIO_CLASS_NONE
value = 0
- if ioclass in (IOPRIO_CLASS_RT, IOPRIO_CLASS_BE):
- if value is None:
- value = 4
elif ioclass == IOPRIO_CLASS_IDLE:
if value:
- msg = "can't specify value with IOPRIO_CLASS_IDLE"
+ msg = "can't specify value with IOPRIO_CLASS_IDLE " \
+ "(got %r)" % value
raise ValueError(msg)
value = 0
+ elif ioclass in (IOPRIO_CLASS_RT, IOPRIO_CLASS_BE):
+ if value is None:
+ # TODO: add comment explaining why this is 4 (?)
+ value = 4
else:
- value = 0
- if not 0 <= value <= 8:
- raise ValueError(
- "value argument range expected is between 0 and 8")
+ # otherwise we would get OSError(EVINAL)
+ raise ValueError("invalid ioclass argument %r" % ioclass)
+
return cext.proc_ioprio_set(self.pid, ioclass, value)
if HAS_PRLIMIT:
diff --git a/test/test_psutil.py b/test/test_psutil.py
index 842fff78..bc750fdc 100644
--- a/test/test_psutil.py
+++ b/test/test_psutil.py
@@ -1356,7 +1356,17 @@ class TestProcess(unittest.TestCase):
ioclass, value = p.ionice()
self.assertEqual(ioclass, 2)
self.assertEqual(value, 7)
+ #
self.assertRaises(ValueError, p.ionice, 2, 10)
+ self.assertRaises(ValueError, p.ionice, 2, -1)
+ self.assertRaises(ValueError, p.ionice, 4)
+ self.assertRaises(TypeError, p.ionice, 2, "foo")
+ self.assertRaisesRegexp(
+ ValueError, "can't specify value with IOPRIO_CLASS_NONE",
+ p.ionice, psutil.IOPRIO_CLASS_NONE, 1)
+ self.assertRaisesRegexp(
+ ValueError, "can't specify value with IOPRIO_CLASS_IDLE",
+ p.ionice, psutil.IOPRIO_CLASS_IDLE, 1)
finally:
p.ionice(IOPRIO_CLASS_NONE)
else: