From fe68b30dacec3255b023fcefac5c9095d96a692f Mon Sep 17 00:00:00 2001 From: wiggin15 Date: Mon, 13 Nov 2017 00:38:12 +0200 Subject: Move exceptions to separate file (#1174) --- psutil/__init__.py | 110 +++----------------------------------------------- psutil/_exceptions.py | 94 ++++++++++++++++++++++++++++++++++++++++++ psutil/_psaix.py | 10 ++--- psutil/_psbsd.py | 10 ++--- psutil/_pslinux.py | 10 ++--- psutil/_psosx.py | 10 ++--- psutil/_pssunos.py | 10 ++--- psutil/_pswindows.py | 8 ++-- 8 files changed, 123 insertions(+), 139 deletions(-) create mode 100644 psutil/_exceptions.py (limited to 'psutil') diff --git a/psutil/__init__.py b/psutil/__init__.py index f80079d0..d14c5e90 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 Error +from ._exceptions import NoSuchProcess +from ._exceptions import ZombieProcess +from ._exceptions import AccessDenied +from ._exceptions import TimeoutExpired + if LINUX: # This is public API and it will be retrieved from _pslinux.py # via sys.modules. @@ -243,110 +249,6 @@ if (int(__version__.replace('.', '')) != raise ImportError(msg) -# ===================================================================== -# --- 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..f63e66ed 100644 --- a/psutil/_psaix.py +++ b/psutil/_psaix.py @@ -28,6 +28,10 @@ from ._common import sockfam_to_enum from ._common import socktype_to_enum from ._common import usage_percent from ._compat import PY3 +from ._exceptions import NoSuchProcess +from ._exceptions import ZombieProcess +from ._exceptions import AccessDenied +from ._exceptions import TimeoutExpired __extra__all__ = ["PROCFS_PATH"] @@ -76,12 +80,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 diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index 6517f244..a9d16450 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -27,6 +27,10 @@ from ._common import sockfam_to_enum from ._common import socktype_to_enum from ._common import usage_percent from ._compat import which +from ._exceptions import NoSuchProcess +from ._exceptions import ZombieProcess +from ._exceptions import AccessDenied +from ._exceptions import TimeoutExpired __extra__all__ = [] @@ -128,12 +132,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 diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 2fec8ea7..228dbd98 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -41,6 +41,10 @@ from ._compat import b from ._compat import basestring from ._compat import long from ._compat import PY3 +from ._exceptions import NoSuchProcess +from ._exceptions import ZombieProcess +from ._exceptions import AccessDenied +from ._exceptions import TimeoutExpired if sys.version_info >= (3, 4): import enum @@ -137,12 +141,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 diff --git a/psutil/_psosx.py b/psutil/_psosx.py index 8093e814..c2a8b3af 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -23,6 +23,10 @@ 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 NoSuchProcess +from ._exceptions import ZombieProcess +from ._exceptions import AccessDenied +from ._exceptions import TimeoutExpired __extra__all__ = [] @@ -84,12 +88,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 diff --git a/psutil/_pssunos.py b/psutil/_pssunos.py index 06e8bbba..aafb3c68 100644 --- a/psutil/_pssunos.py +++ b/psutil/_pssunos.py @@ -24,6 +24,10 @@ from ._common import socktype_to_enum from ._common import usage_percent from ._compat import b from ._compat import PY3 +from ._exceptions import NoSuchProcess +from ._exceptions import ZombieProcess +from ._exceptions import AccessDenied +from ._exceptions import TimeoutExpired __extra__all__ = ["CONN_IDLE", "CONN_BOUND", "PROCFS_PATH"] @@ -78,12 +82,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 diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index 8903a307..ee30308b 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 NoSuchProcess +from ._exceptions import AccessDenied +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 -- cgit v1.2.1 From 100391f880ef2a2c5b124bba4b0722623f3edb3e Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sun, 12 Nov 2017 23:41:30 +0100 Subject: sort imports by name --- psutil/__init__.py | 4 ++-- psutil/_psaix.py | 4 ++-- psutil/_psbsd.py | 4 ++-- psutil/_pslinux.py | 4 ++-- psutil/_psosx.py | 4 ++-- psutil/_pssunos.py | 4 ++-- psutil/_pswindows.py | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) (limited to 'psutil') diff --git a/psutil/__init__.py b/psutil/__init__.py index d14c5e90..12cd2c4c 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -85,11 +85,11 @@ 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 ZombieProcess -from ._exceptions import AccessDenied from ._exceptions import TimeoutExpired +from ._exceptions import ZombieProcess if LINUX: # This is public API and it will be retrieved from _pslinux.py diff --git a/psutil/_psaix.py b/psutil/_psaix.py index f63e66ed..36989623 100644 --- a/psutil/_psaix.py +++ b/psutil/_psaix.py @@ -28,10 +28,10 @@ from ._common import sockfam_to_enum from ._common import socktype_to_enum from ._common import usage_percent from ._compat import PY3 -from ._exceptions import NoSuchProcess -from ._exceptions import ZombieProcess from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess from ._exceptions import TimeoutExpired +from ._exceptions import ZombieProcess __extra__all__ = ["PROCFS_PATH"] diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index a9d16450..c26300a3 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -27,10 +27,10 @@ from ._common import sockfam_to_enum from ._common import socktype_to_enum from ._common import usage_percent from ._compat import which -from ._exceptions import NoSuchProcess -from ._exceptions import ZombieProcess from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess from ._exceptions import TimeoutExpired +from ._exceptions import ZombieProcess __extra__all__ = [] diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 228dbd98..fc38fb14 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -41,10 +41,10 @@ from ._compat import b from ._compat import basestring from ._compat import long from ._compat import PY3 -from ._exceptions import NoSuchProcess -from ._exceptions import ZombieProcess from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess from ._exceptions import TimeoutExpired +from ._exceptions import ZombieProcess if sys.version_info >= (3, 4): import enum diff --git a/psutil/_psosx.py b/psutil/_psosx.py index c2a8b3af..583ed355 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -23,10 +23,10 @@ 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 NoSuchProcess -from ._exceptions import ZombieProcess from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess from ._exceptions import TimeoutExpired +from ._exceptions import ZombieProcess __extra__all__ = [] diff --git a/psutil/_pssunos.py b/psutil/_pssunos.py index aafb3c68..d1f5df79 100644 --- a/psutil/_pssunos.py +++ b/psutil/_pssunos.py @@ -24,10 +24,10 @@ from ._common import socktype_to_enum from ._common import usage_percent from ._compat import b from ._compat import PY3 -from ._exceptions import NoSuchProcess -from ._exceptions import ZombieProcess from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess from ._exceptions import TimeoutExpired +from ._exceptions import ZombieProcess __extra__all__ = ["CONN_IDLE", "CONN_BOUND", "PROCFS_PATH"] diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index ee30308b..83936d08 100644 --- a/psutil/_pswindows.py +++ b/psutil/_pswindows.py @@ -45,8 +45,8 @@ from ._compat import lru_cache from ._compat import PY3 from ._compat import unicode from ._compat import xrange -from ._exceptions import NoSuchProcess 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 -- cgit v1.2.1 From 40573cbe58407a3f8dfcb0c3b71237444b10fc0a Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Mon, 13 Nov 2017 00:00:30 +0100 Subject: #1174: use TimeoutExpired in wait_pid() --- psutil/_psaix.py | 9 +-------- psutil/_psbsd.py | 6 +----- psutil/_pslinux.py | 6 +----- psutil/_psosx.py | 6 +----- psutil/_psposix.py | 12 ++++-------- psutil/_pssunos.py | 6 +----- 6 files changed, 9 insertions(+), 36 deletions(-) (limited to 'psutil') diff --git a/psutil/_psaix.py b/psutil/_psaix.py index 36989623..9abc8d17 100644 --- a/psutil/_psaix.py +++ b/psutil/_psaix.py @@ -30,7 +30,6 @@ from ._common import usage_percent from ._compat import PY3 from ._exceptions import AccessDenied from ._exceptions import NoSuchProcess -from ._exceptions import TimeoutExpired from ._exceptions import ZombieProcess @@ -559,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 c26300a3..0553401a 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -29,7 +29,6 @@ from ._common import usage_percent from ._compat import which from ._exceptions import AccessDenied from ._exceptions import NoSuchProcess -from ._exceptions import TimeoutExpired from ._exceptions import ZombieProcess __extra__all__ = [] @@ -758,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 fc38fb14..3fe62c5c 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -43,7 +43,6 @@ from ._compat import long from ._compat import PY3 from ._exceptions import AccessDenied from ._exceptions import NoSuchProcess -from ._exceptions import TimeoutExpired from ._exceptions import ZombieProcess if sys.version_info >= (3, 4): @@ -1534,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 583ed355..e9b6ed82 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -25,7 +25,6 @@ from ._common import socktype_to_enum from ._common import usage_percent from ._exceptions import AccessDenied from ._exceptions import NoSuchProcess -from ._exceptions import TimeoutExpired from ._exceptions import ZombieProcess @@ -500,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 d1f5df79..5471d5aa 100644 --- a/psutil/_pssunos.py +++ b/psutil/_pssunos.py @@ -26,7 +26,6 @@ from ._compat import b from ._compat import PY3 from ._exceptions import AccessDenied from ._exceptions import NoSuchProcess -from ._exceptions import TimeoutExpired from ._exceptions import ZombieProcess @@ -723,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) -- cgit v1.2.1