Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move exceptions to separate file #1174

Merged
merged 1 commit into from
Nov 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 6 additions & 104 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
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.
Expand Down Expand Up @@ -243,110 +249,6 @@
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
# =====================================================================
Expand Down
94 changes: 94 additions & 0 deletions psutil/_exceptions.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 4 additions & 6 deletions psutil/_psaix.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
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"]
Expand Down Expand Up @@ -76,12 +80,6 @@
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
Expand Down
10 changes: 4 additions & 6 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
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__ = []

Expand Down Expand Up @@ -128,12 +132,6 @@
name=24,
)

# these get overwritten on "import psutil" from the __init__.py file
NoSuchProcess = None
ZombieProcess = None
AccessDenied = None
TimeoutExpired = None


# =====================================================================
# --- named tuples
Expand Down
10 changes: 4 additions & 6 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
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
Expand Down Expand Up @@ -137,12 +141,6 @@ class IOPriority(enum.IntEnum):
"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
Expand Down
10 changes: 4 additions & 6 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
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__ = []
Expand Down Expand Up @@ -84,12 +88,6 @@
volctxsw=7,
)

# these get overwritten on "import psutil" from the __init__.py file
NoSuchProcess = None
ZombieProcess = None
AccessDenied = None
TimeoutExpired = None


# =====================================================================
# --- named tuples
Expand Down
10 changes: 4 additions & 6 deletions psutil/_pssunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
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"]
Expand Down Expand Up @@ -78,12 +82,6 @@
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
Expand Down
8 changes: 3 additions & 5 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
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
Expand Down Expand Up @@ -139,11 +142,6 @@ class Priority(enum.IntEnum):
mem_private=21,
)

# these get overwritten on "import psutil" from the __init__.py file
NoSuchProcess = None
AccessDenied = None
TimeoutExpired = None


# =====================================================================
# --- named tuples
Expand Down