summaryrefslogtreecommitdiff
path: root/psutil/__init__.py
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-01-02 18:45:29 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2020-01-02 18:45:29 +0100
commit19f87b14f83669fd450e032e35d5b328b9acc118 (patch)
treee4033ef79ee1c30dc0df62cade4985a2350a2b0b /psutil/__init__.py
parent1cff4c060559242beb908f0f53e8d511964c47f7 (diff)
downloadpsutil-19f87b14f83669fd450e032e35d5b328b9acc118.tar.gz
move custom exceptions in _common.py
Diffstat (limited to 'psutil/__init__.py')
-rw-r--r--psutil/__init__.py111
1 files changed, 5 insertions, 106 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
# =====================================================================