summaryrefslogtreecommitdiff
path: root/psutil/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'psutil/__init__.py')
-rw-r--r--psutil/__init__.py57
1 files changed, 26 insertions, 31 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py
index 608db167..7a3fb066 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -158,7 +158,7 @@ __all__ = [
]
__all__.extend(_psplatform.__extra__all__)
__author__ = "Giampaolo Rodola'"
-__version__ = "3.0.0"
+__version__ = "3.0.2"
version_info = tuple([int(num) for num in __version__.split('.')])
AF_LINK = _psplatform.AF_LINK
_TOTAL_PHYMEM = None
@@ -190,6 +190,12 @@ class Error(Exception):
from this one.
"""
+ def __init__(self, msg=""):
+ self.msg = msg
+
+ def __str__(self):
+ return self.msg
+
class NoSuchProcess(Error):
"""Exception raised when a process with a certain PID doesn't
@@ -197,7 +203,7 @@ class NoSuchProcess(Error):
"""
def __init__(self, pid, name=None, msg=None):
- Error.__init__(self)
+ Error.__init__(self, msg)
self.pid = pid
self.name = name
self.msg = msg
@@ -208,9 +214,6 @@ class NoSuchProcess(Error):
details = "(pid=%s)" % self.pid
self.msg = "process no longer exists " + details
- def __str__(self):
- return self.msg
-
class ZombieProcess(NoSuchProcess):
"""Exception raised when querying a zombie process. This is
@@ -221,7 +224,7 @@ class ZombieProcess(NoSuchProcess):
"""
def __init__(self, pid, name=None, ppid=None, msg=None):
- Error.__init__(self)
+ Error.__init__(self, msg)
self.pid = pid
self.ppid = ppid
self.name = name
@@ -236,15 +239,12 @@ class ZombieProcess(NoSuchProcess):
details = "(pid=%s)" % self.pid
self.msg = "process still exists but it's a zombie " + details
- def __str__(self):
- return self.msg
-
class AccessDenied(Error):
"""Exception raised when permission to perform an action is denied."""
def __init__(self, pid=None, name=None, msg=None):
- Error.__init__(self)
+ Error.__init__(self, msg)
self.pid = pid
self.name = name
self.msg = msg
@@ -256,9 +256,6 @@ class AccessDenied(Error):
else:
self.msg = ""
- def __str__(self):
- return self.msg
-
class TimeoutExpired(Error):
"""Raised on Process.wait(timeout) if timeout expires and process
@@ -266,18 +263,15 @@ class TimeoutExpired(Error):
"""
def __init__(self, seconds, pid=None, name=None):
- Error.__init__(self)
+ Error.__init__(self, "timeout after %s seconds" % seconds)
self.seconds = seconds
self.pid = pid
self.name = name
- self.msg = "timeout after %s seconds" % seconds
if (pid is not None) and (name is not None):
self.msg += " (pid=%s, name=%s)" % (pid, repr(name))
elif (pid is not None):
self.msg += " (pid=%s)" % self.pid
- def __str__(self):
- return self.msg
# push exception classes into platform specific module namespace
_psplatform.NoSuchProcess = NoSuchProcess
@@ -290,6 +284,7 @@ _psplatform.TimeoutExpired = TimeoutExpired
# --- Process class
# =====================================================================
+
def _assert_pid_not_reused(fun):
"""Decorator which raises NoSuchProcess in case a process is no
longer running or its PID has been reused.
@@ -686,7 +681,7 @@ class Process(object):
"""
if ioclass is None:
if value is not None:
- raise ValueError("'ioclass' must be specified")
+ raise ValueError("'ioclass' argument must be specified")
return self._proc.ionice_get()
else:
return self._proc.ionice_set(ioclass, value)
@@ -1012,10 +1007,12 @@ class Process(object):
if _POSIX:
def _send_signal(self, sig):
- # XXX: according to "man 2 kill" PID 0 has a special
- # meaning as it refers to <<every process in the process
- # group of the calling process>>, so should we prevent
- # it here?
+ if self.pid == 0:
+ # see "man 2 kill"
+ raise ValueError(
+ "preventing sending signal to process with PID 0 as it "
+ "would affect every process in the process group of the "
+ "calling process (os.getpid()) instead of PID 0")
try:
os.kill(self.pid, sig)
except OSError as err:
@@ -1105,6 +1102,7 @@ class Process(object):
# --- Popen class
# =====================================================================
+
class Popen(Process):
"""A more convenient interface to stdlib subprocess module.
It starts a sub process and deals with it exactly as when using
@@ -1172,6 +1170,7 @@ class Popen(Process):
# --- system processes related functions
# =====================================================================
+
def pids():
"""Return a list of current running PIDs."""
return _psplatform.pids()
@@ -1338,13 +1337,14 @@ def wait_procs(procs, timeout=None, callback=None):
# --- CPU related functions
# =====================================================================
+
@memoize
def cpu_count(logical=True):
"""Return the number of logical CPUs in the system (same as
os.cpu_count() in Python 3.4).
If logical is False return the number of physical cores only
- (hyper thread CPUs are excluded).
+ (e.g. hyper thread CPUs are excluded).
Return None if undetermined.
@@ -1548,6 +1548,7 @@ def cpu_times_percent(interval=None, percpu=False):
# --- system memory related functions
# =====================================================================
+
def virtual_memory():
"""Return statistics about system memory usage as a namedtuple
including the following fields, expressed in bytes:
@@ -1628,6 +1629,7 @@ def swap_memory():
# --- disks/paritions related functions
# =====================================================================
+
def disk_usage(path):
"""Return disk usage statistics about the given path as a namedtuple
including total, used and free space expressed in bytes plus the
@@ -1682,6 +1684,7 @@ def disk_io_counters(perdisk=False):
# --- network related functions
# =====================================================================
+
def net_io_counters(pernic=False):
"""Return network I/O statistics as a namedtuple including
the following fields:
@@ -1852,14 +1855,6 @@ def test():
time.localtime(sum(pinfo['cpu_times'])))
try:
user = p.username()
- except KeyError:
- if _POSIX:
- if pinfo['uids']:
- user = str(pinfo['uids'].real)
- else:
- user = ''
- else:
- raise
except Error:
user = ''
if _WINDOWS and '\\' in user: