diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-02 18:45:29 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-02 18:45:29 +0100 |
commit | 19f87b14f83669fd450e032e35d5b328b9acc118 (patch) | |
tree | e4033ef79ee1c30dc0df62cade4985a2350a2b0b | |
parent | 1cff4c060559242beb908f0f53e8d511964c47f7 (diff) | |
download | psutil-19f87b14f83669fd450e032e35d5b328b9acc118.tar.gz |
move custom exceptions in _common.py
-rw-r--r-- | psutil/__init__.py | 111 | ||||
-rw-r--r-- | psutil/_common.py | 104 | ||||
-rw-r--r-- | psutil/_psaix.py | 10 | ||||
-rw-r--r-- | psutil/_psbsd.py | 10 | ||||
-rw-r--r-- | psutil/_pslinux.py | 10 | ||||
-rw-r--r-- | psutil/_psosx.py | 12 | ||||
-rw-r--r-- | psutil/_psposix.py | 6 | ||||
-rw-r--r-- | psutil/_pssunos.py | 10 | ||||
-rw-r--r-- | psutil/_pswindows.py | 10 |
9 files changed, 129 insertions, 154 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py index 093acf0c..bf34b429 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -38,10 +38,15 @@ except ImportError: pwd = None from . import _common +from ._common import AccessDenied from ._common import deprecated_method +from ._common import Error from ._common import memoize from ._common import memoize_when_activated +from ._common import NoSuchProcess +from ._common import TimeoutExpired from ._common import wrap_numbers as _wrap_numbers +from ._common import ZombieProcess from ._compat import long from ._compat import PermissionError from ._compat import ProcessLookupError @@ -255,112 +260,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 = "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 macOS, 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 -if POSIX: - from . import _psposix - _psposix.TimeoutExpired = TimeoutExpired - - -# ===================================================================== # --- Utils # ===================================================================== diff --git a/psutil/_common.py b/psutil/_common.py index 2f74460b..729b1983 100644 --- a/psutil/_common.py +++ b/psutil/_common.py @@ -260,6 +260,110 @@ if AF_UNIX is not None: }) +# ===================================================================== +# --- Exceptions +# ===================================================================== + + +class Error(Exception): + """Base exception class. All other psutil exceptions inherit + from this one. + """ + __module__ = 'psutil' + + 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. + """ + __module__ = 'psutil' + + 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 + + def __path__(self): + return 'xxx' + + +class ZombieProcess(NoSuchProcess): + """Exception raised when querying a zombie process. This is + raised on macOS, 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. + """ + __module__ = 'psutil' + + 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.""" + __module__ = 'psutil' + + 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. + """ + __module__ = 'psutil' + + 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 + + # =================================================================== # --- utils # =================================================================== diff --git a/psutil/_psaix.py b/psutil/_psaix.py index 79e3be15..994366aa 100644 --- a/psutil/_psaix.py +++ b/psutil/_psaix.py @@ -18,13 +18,16 @@ from . import _common from . import _psposix from . import _psutil_aix as cext from . import _psutil_posix as cext_posix +from ._common import AccessDenied from ._common import conn_to_ntuple from ._common import get_procfs_path from ._common import memoize_when_activated from ._common import NIC_DUPLEX_FULL from ._common import NIC_DUPLEX_HALF from ._common import NIC_DUPLEX_UNKNOWN +from ._common import NoSuchProcess from ._common import usage_percent +from ._common import ZombieProcess from ._compat import FileNotFoundError from ._compat import PermissionError from ._compat import ProcessLookupError @@ -79,13 +82,6 @@ proc_info_map = dict( status=6, ttynr=7) -# These objects get set on "import psutil" from the __init__.py -# file, see: https://github.com/giampaolo/psutil/issues/1402 -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index 2f41dc0b..78e436f7 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -16,14 +16,17 @@ from . import _common from . import _psposix from . import _psutil_bsd as cext from . import _psutil_posix as cext_posix +from ._common import AccessDenied from ._common import conn_tmap from ._common import conn_to_ntuple from ._common import FREEBSD from ._common import memoize from ._common import memoize_when_activated from ._common import NETBSD +from ._common import NoSuchProcess from ._common import OPENBSD from ._common import usage_percent +from ._common import ZombieProcess from ._compat import FileNotFoundError from ._compat import PermissionError from ._compat import ProcessLookupError @@ -135,13 +138,6 @@ kinfo_proc_map = dict( name=24, ) -# These objects get set on "import psutil" from the __init__.py -# file, see: https://github.com/giampaolo/psutil/issues/1402 -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 694e307b..c681439d 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -25,6 +25,7 @@ from . import _common from . import _psposix from . import _psutil_linux as cext from . import _psutil_posix as cext_posix +from ._common import AccessDenied from ._common import decode from ._common import get_procfs_path from ._common import isfile_strict @@ -33,12 +34,14 @@ from ._common import memoize_when_activated from ._common import NIC_DUPLEX_FULL from ._common import NIC_DUPLEX_HALF from ._common import NIC_DUPLEX_UNKNOWN +from ._common import NoSuchProcess from ._common import open_binary from ._common import open_text from ._common import parse_environ_block from ._common import path_exists_strict from ._common import supports_ipv6 from ._common import usage_percent +from ._common import ZombieProcess from ._compat import b from ._compat import basestring from ._compat import FileNotFoundError @@ -161,13 +164,6 @@ TCP_STATUSES = { "0B": _common.CONN_CLOSING } -# These objects get set on "import psutil" from the __init__.py -# file, see: https://github.com/giampaolo/psutil/issues/1402 -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples diff --git a/psutil/_psosx.py b/psutil/_psosx.py index 7f28447b..e4296495 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -14,14 +14,17 @@ from . import _common from . import _psposix from . import _psutil_osx as cext from . import _psutil_posix as cext_posix +from ._common import AccessDenied from ._common import conn_tmap from ._common import conn_to_ntuple from ._common import isfile_strict from ._common import memoize_when_activated +from ._common import NoSuchProcess from ._common import parse_environ_block +from ._common import usage_percent +from ._common import ZombieProcess from ._compat import PermissionError from ._compat import ProcessLookupError -from ._common import usage_percent __extra__all__ = [] @@ -83,13 +86,6 @@ pidtaskinfo_map = dict( volctxsw=7, ) -# These objects get set on "import psutil" from the __init__.py -# file, see: https://github.com/giampaolo/psutil/issues/1402 -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples diff --git a/psutil/_psposix.py b/psutil/_psposix.py index 24570224..88213ef8 100644 --- a/psutil/_psposix.py +++ b/psutil/_psposix.py @@ -11,6 +11,7 @@ import time from ._common import memoize from ._common import sdiskusage +from ._common import TimeoutExpired from ._common import usage_percent from ._compat import ChildProcessError from ._compat import FileNotFoundError @@ -24,11 +25,6 @@ from ._compat import unicode __all__ = ['pid_exists', 'wait_pid', 'disk_usage', 'get_terminal_map'] -# This object gets set on "import psutil" from the __init__.py -# file, see: https://github.com/giampaolo/psutil/issues/1402 -TimeoutExpired = None - - def pid_exists(pid): """Check whether pid exists in the current process table.""" if pid == 0: diff --git a/psutil/_pssunos.py b/psutil/_pssunos.py index 9f0cac26..b1e2d1c9 100644 --- a/psutil/_pssunos.py +++ b/psutil/_pssunos.py @@ -17,13 +17,16 @@ from . import _common from . import _psposix from . import _psutil_posix as cext_posix from . import _psutil_sunos as cext +from ._common import AccessDenied from ._common import AF_INET6 from ._common import get_procfs_path from ._common import isfile_strict from ._common import memoize_when_activated +from ._common import NoSuchProcess from ._common import sockfam_to_enum from ._common import socktype_to_enum from ._common import usage_percent +from ._common import ZombieProcess from ._compat import b from ._compat import FileNotFoundError from ._compat import PermissionError @@ -87,13 +90,6 @@ proc_info_map = dict( gid=10, egid=11) -# These objects get set on "import psutil" from the __init__.py -# file, see: https://github.com/giampaolo/psutil/issues/1402 -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index 636b0af9..4ad511e5 100644 --- a/psutil/_pswindows.py +++ b/psutil/_pswindows.py @@ -32,6 +32,7 @@ except ImportError as err: else: raise +from ._common import AccessDenied from ._common import conn_tmap from ._common import conn_to_ntuple from ._common import ENCODING @@ -39,7 +40,9 @@ from ._common import ENCODING_ERRS from ._common import isfile_strict from ._common import memoize from ._common import memoize_when_activated +from ._common import NoSuchProcess from ._common import parse_environ_block +from ._common import TimeoutExpired from ._common import usage_percent from ._compat import long from ._compat import lru_cache @@ -154,13 +157,6 @@ pinfo_map = dict( mem_private=21, ) -# These objects get set on "import psutil" from the __init__.py -# file, see: https://github.com/giampaolo/psutil/issues/1402 -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # More values at: https://stackoverflow.com/a/20804735/376587 WIN_10 = (10, 0) WIN_8 = (6, 2) |