diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-11-13 00:27:06 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-11-13 00:27:06 +0100 |
commit | 684c6fb5a425a9667654d0b8a70e790f1b1069e7 (patch) | |
tree | 0db7f1deffbea3d5e58ae125ee7f150a41bbc9a6 | |
parent | 3c5438978f621f18b8e9b0b70fde4182221d7cb3 (diff) | |
parent | 4d3f5ba337c5a91efa642699cc96331718889b4f (diff) | |
download | psutil-1173-debug-mode.tar.gz |
Merge branch 'master' into 1173-debug-mode1173-debug-mode
-rw-r--r-- | CREDITS | 2 | ||||
-rw-r--r-- | MANIFEST.in | 1 | ||||
-rw-r--r-- | psutil/__init__.py | 110 | ||||
-rw-r--r-- | psutil/_exceptions.py | 94 | ||||
-rw-r--r-- | psutil/_psaix.py | 17 | ||||
-rw-r--r-- | psutil/_psbsd.py | 14 | ||||
-rw-r--r-- | psutil/_pslinux.py | 14 | ||||
-rw-r--r-- | psutil/_psosx.py | 14 | ||||
-rw-r--r-- | psutil/_psposix.py | 12 | ||||
-rw-r--r-- | psutil/_pssunos.py | 14 | ||||
-rw-r--r-- | psutil/_pswindows.py | 8 |
11 files changed, 129 insertions, 171 deletions
@@ -57,7 +57,7 @@ W: http://www.jayloden.com N: Arnon Yaari (wiggin15) W: https://github.com/wiggin15 -I: 517, 607, 610, 1131, 1123, 1130, 1154, 1164 +I: 517, 607, 610, 1131, 1123, 1130, 1154, 1164, 1174 N: Jeff Tang W: https://github.com/mrjefftang diff --git a/MANIFEST.in b/MANIFEST.in index 11945017..7a92a4e5 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -24,6 +24,7 @@ include psutil/DEVNOTES include psutil/__init__.py include psutil/_common.py include psutil/_compat.py +include psutil/_exceptions.py include psutil/_psaix.py include psutil/_psbsd.py include psutil/_pslinux.py diff --git a/psutil/__init__.py b/psutil/__init__.py index f80079d0..12cd2c4c 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -85,6 +85,12 @@ from ._common import POSIX # NOQA from ._common import SUNOS from ._common import WINDOWS +from ._exceptions import AccessDenied +from ._exceptions import Error +from ._exceptions import NoSuchProcess +from ._exceptions import TimeoutExpired +from ._exceptions import ZombieProcess + if LINUX: # This is public API and it will be retrieved from _pslinux.py # via sys.modules. @@ -244,110 +250,6 @@ if (int(__version__.replace('.', '')) != # ===================================================================== -# --- exceptions -# ===================================================================== - - -class Error(Exception): - """Base exception class. All other psutil exceptions inherit - from this one. - """ - - def __init__(self, msg=""): - Exception.__init__(self, msg) - self.msg = msg - - def __repr__(self): - ret = "%s.%s %s" % (self.__class__.__module__, - self.__class__.__name__, self.msg) - return ret.strip() - - __str__ = __repr__ - - -class NoSuchProcess(Error): - """Exception raised when a process with a certain PID doesn't - or no longer exists. - """ - - def __init__(self, pid, name=None, msg=None): - Error.__init__(self, msg) - self.pid = pid - self.name = name - self.msg = msg - if msg is None: - if name: - details = "(pid=%s, name=%s)" % (self.pid, repr(self.name)) - else: - details = "(pid=%s)" % self.pid - self.msg = "process no longer exists " + details - - -class ZombieProcess(NoSuchProcess): - """Exception raised when querying a zombie process. This is - raised on OSX, BSD and Solaris only, and not always: depending - on the query the OS may be able to succeed anyway. - On Linux all zombie processes are querable (hence this is never - raised). Windows doesn't have zombie processes. - """ - - def __init__(self, pid, name=None, ppid=None, msg=None): - NoSuchProcess.__init__(self, msg) - self.pid = pid - self.ppid = ppid - self.name = name - self.msg = msg - if msg is None: - args = ["pid=%s" % pid] - if name: - args.append("name=%s" % repr(self.name)) - if ppid: - args.append("ppid=%s" % self.ppid) - details = "(%s)" % ", ".join(args) - self.msg = "process still exists but it's a zombie " + details - - -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, msg) - self.pid = pid - self.name = name - self.msg = msg - if msg is None: - 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 - else: - self.msg = "" - - -class TimeoutExpired(Error): - """Raised on Process.wait(timeout) if timeout expires and process - is still alive. - """ - - def __init__(self, seconds, pid=None, name=None): - Error.__init__(self, "timeout after %s seconds" % seconds) - self.seconds = seconds - self.pid = pid - self.name = name - 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 - - -# push exception classes into platform specific module namespace -_psplatform.NoSuchProcess = NoSuchProcess -_psplatform.ZombieProcess = ZombieProcess -_psplatform.AccessDenied = AccessDenied -_psplatform.TimeoutExpired = TimeoutExpired - - -# ===================================================================== # --- Process class # ===================================================================== diff --git a/psutil/_exceptions.py b/psutil/_exceptions.py new file mode 100644 index 00000000..c08e6d83 --- /dev/null +++ b/psutil/_exceptions.py @@ -0,0 +1,94 @@ +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + + +class Error(Exception): + """Base exception class. All other psutil exceptions inherit + from this one. + """ + + def __init__(self, msg=""): + Exception.__init__(self, msg) + self.msg = msg + + def __repr__(self): + ret = "psutil.%s %s" % (self.__class__.__name__, self.msg) + return ret.strip() + + __str__ = __repr__ + + +class NoSuchProcess(Error): + """Exception raised when a process with a certain PID doesn't + or no longer exists. + """ + + def __init__(self, pid, name=None, msg=None): + Error.__init__(self, msg) + self.pid = pid + self.name = name + self.msg = msg + if msg is None: + if name: + details = "(pid=%s, name=%s)" % (self.pid, repr(self.name)) + else: + details = "(pid=%s)" % self.pid + self.msg = "process no longer exists " + details + + +class ZombieProcess(NoSuchProcess): + """Exception raised when querying a zombie process. This is + raised on OSX, BSD and Solaris only, and not always: depending + on the query the OS may be able to succeed anyway. + On Linux all zombie processes are querable (hence this is never + raised). Windows doesn't have zombie processes. + """ + + def __init__(self, pid, name=None, ppid=None, msg=None): + NoSuchProcess.__init__(self, msg) + self.pid = pid + self.ppid = ppid + self.name = name + self.msg = msg + if msg is None: + args = ["pid=%s" % pid] + if name: + args.append("name=%s" % repr(self.name)) + if ppid: + args.append("ppid=%s" % self.ppid) + details = "(%s)" % ", ".join(args) + self.msg = "process still exists but it's a zombie " + details + + +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, msg) + self.pid = pid + self.name = name + self.msg = msg + if msg is None: + 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 + else: + self.msg = "" + + +class TimeoutExpired(Error): + """Raised on Process.wait(timeout) if timeout expires and process + is still alive. + """ + + def __init__(self, seconds, pid=None, name=None): + Error.__init__(self, "timeout after %s seconds" % seconds) + self.seconds = seconds + self.pid = pid + self.name = name + 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 diff --git a/psutil/_psaix.py b/psutil/_psaix.py index c78922b0..9abc8d17 100644 --- a/psutil/_psaix.py +++ b/psutil/_psaix.py @@ -28,6 +28,9 @@ from ._common import sockfam_to_enum from ._common import socktype_to_enum from ._common import usage_percent from ._compat import PY3 +from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess +from ._exceptions import ZombieProcess __extra__all__ = ["PROCFS_PATH"] @@ -76,12 +79,6 @@ proc_info_map = dict( status=6, ttynr=7) -# these get overwritten on "import psutil" from the __init__.py file -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples @@ -561,13 +558,7 @@ class Process(object): @wrap_exceptions def wait(self, timeout=None): - try: - return _psposix.wait_pid(self.pid, timeout) - except _psposix.TimeoutExpired: - # support for private module import - if TimeoutExpired is None: - raise - raise TimeoutExpired(timeout, self.pid, self._name) + return _psposix.wait_pid(self.pid, timeout, self._name) @wrap_exceptions def io_counters(self): diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index 6517f244..0553401a 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -27,6 +27,9 @@ from ._common import sockfam_to_enum from ._common import socktype_to_enum from ._common import usage_percent from ._compat import which +from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess +from ._exceptions import ZombieProcess __extra__all__ = [] @@ -128,12 +131,6 @@ kinfo_proc_map = dict( name=24, ) -# these get overwritten on "import psutil" from the __init__.py file -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples @@ -760,10 +757,7 @@ class Process(object): @wrap_exceptions def wait(self, timeout=None): - try: - return _psposix.wait_pid(self.pid, timeout) - except _psposix.TimeoutExpired: - raise TimeoutExpired(timeout, self.pid, self._name) + return _psposix.wait_pid(self.pid, timeout, self._name) @wrap_exceptions def nice_get(self): diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 2fec8ea7..3fe62c5c 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -41,6 +41,9 @@ from ._compat import b from ._compat import basestring from ._compat import long from ._compat import PY3 +from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess +from ._exceptions import ZombieProcess if sys.version_info >= (3, 4): import enum @@ -137,12 +140,6 @@ TCP_STATUSES = { "0B": _common.CONN_CLOSING } -# these get overwritten on "import psutil" from the __init__.py file -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples @@ -1536,10 +1533,7 @@ class Process(object): @wrap_exceptions def wait(self, timeout=None): - try: - return _psposix.wait_pid(self.pid, timeout) - except _psposix.TimeoutExpired: - raise TimeoutExpired(timeout, self.pid, self._name) + return _psposix.wait_pid(self.pid, timeout, self._name) @wrap_exceptions def create_time(self): diff --git a/psutil/_psosx.py b/psutil/_psosx.py index 8093e814..e9b6ed82 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -23,6 +23,9 @@ from ._common import parse_environ_block from ._common import sockfam_to_enum from ._common import socktype_to_enum from ._common import usage_percent +from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess +from ._exceptions import ZombieProcess __extra__all__ = [] @@ -84,12 +87,6 @@ pidtaskinfo_map = dict( volctxsw=7, ) -# these get overwritten on "import psutil" from the __init__.py file -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples @@ -502,10 +499,7 @@ class Process(object): @wrap_exceptions def wait(self, timeout=None): - try: - return _psposix.wait_pid(self.pid, timeout) - except _psposix.TimeoutExpired: - raise TimeoutExpired(timeout, self.pid, self._name) + return _psposix.wait_pid(self.pid, timeout, self._name) @wrap_exceptions def nice_get(self): diff --git a/psutil/_psposix.py b/psutil/_psposix.py index 66d81a3d..6bb8444d 100644 --- a/psutil/_psposix.py +++ b/psutil/_psposix.py @@ -15,14 +15,10 @@ from ._common import sdiskusage from ._common import usage_percent from ._compat import PY3 from ._compat import unicode +from ._exceptions import TimeoutExpired -__all__ = ['TimeoutExpired', 'pid_exists', 'wait_pid', 'disk_usage', - 'get_terminal_map'] - - -class TimeoutExpired(Exception): - pass +__all__ = ['pid_exists', 'wait_pid', 'disk_usage', 'get_terminal_map'] def pid_exists(pid): @@ -53,7 +49,7 @@ def pid_exists(pid): return True -def wait_pid(pid, timeout=None): +def wait_pid(pid, timeout=None, proc_name=None): """Wait for process with pid 'pid' to terminate and return its exit status code as an integer. @@ -67,7 +63,7 @@ def wait_pid(pid, timeout=None): def check_timeout(delay): if timeout is not None: if timer() >= stop_at: - raise TimeoutExpired() + raise TimeoutExpired(timeout, pid=pid, name=proc_name) time.sleep(delay) return min(delay * 2, 0.04) diff --git a/psutil/_pssunos.py b/psutil/_pssunos.py index 06e8bbba..5471d5aa 100644 --- a/psutil/_pssunos.py +++ b/psutil/_pssunos.py @@ -24,6 +24,9 @@ from ._common import socktype_to_enum from ._common import usage_percent from ._compat import b from ._compat import PY3 +from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess +from ._exceptions import ZombieProcess __extra__all__ = ["CONN_IDLE", "CONN_BOUND", "PROCFS_PATH"] @@ -78,12 +81,6 @@ proc_info_map = dict( status=6, ttynr=7) -# these get overwritten on "import psutil" from the __init__.py file -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples @@ -725,7 +722,4 @@ class Process(object): @wrap_exceptions def wait(self, timeout=None): - try: - return _psposix.wait_pid(self.pid, timeout) - except _psposix.TimeoutExpired: - raise TimeoutExpired(timeout, self.pid, self._name) + return _psposix.wait_pid(self.pid, timeout, self._name) diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index 8903a307..83936d08 100644 --- a/psutil/_pswindows.py +++ b/psutil/_pswindows.py @@ -45,6 +45,9 @@ from ._compat import lru_cache from ._compat import PY3 from ._compat import unicode from ._compat import xrange +from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess +from ._exceptions import TimeoutExpired from ._psutil_windows import ABOVE_NORMAL_PRIORITY_CLASS from ._psutil_windows import BELOW_NORMAL_PRIORITY_CLASS from ._psutil_windows import HIGH_PRIORITY_CLASS @@ -139,11 +142,6 @@ pinfo_map = dict( mem_private=21, ) -# these get overwritten on "import psutil" from the __init__.py file -NoSuchProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples |