Skip to content

Commit

Permalink
fix #1402: move psutil exceptions back into __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 4, 2019
1 parent 3d65245 commit 5849143
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 122 deletions.
3 changes: 2 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ XXXX-XX-XX

- 1394_: [Windows] Process.exe() returns "[Error 0] The operation completed
successfully" when Python process runs in "Virtual Secure Mode".
- 1402_: psutil exceptions' repr() show the internal private module path.

5.5.0
=====

2019-0-23
2019-01-23

**Enhancements**

Expand Down
115 changes: 107 additions & 8 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- Sun Solaris
- AIX
Works with Python versions from 2.6 to 3.X.
Works with Python versions from 2.6 to 3.4+.
"""

from __future__ import division
Expand Down Expand Up @@ -87,12 +87,6 @@
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.
Expand Down Expand Up @@ -229,7 +223,6 @@
_TOTAL_PHYMEM = None
_timer = getattr(time, 'monotonic', time.time)


# Sanity check in case the user messed up with psutil installation
# or did something weird with sys.path. In this case we might end
# up importing a python module using a C extension module which
Expand All @@ -252,6 +245,112 @@
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 = "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
# =====================================================================
Expand Down
94 changes: 0 additions & 94 deletions psutil/_exceptions.py

This file was deleted.

10 changes: 7 additions & 3 deletions psutil/_psaix.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
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"]
Expand Down Expand Up @@ -79,6 +76,13 @@
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
Expand Down
10 changes: 7 additions & 3 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
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__ = []

Expand Down Expand Up @@ -137,6 +134,13 @@
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
Expand Down
10 changes: 7 additions & 3 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
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
Expand Down Expand Up @@ -161,6 +158,13 @@ class IOPriority(enum.IntEnum):
"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
Expand Down
10 changes: 7 additions & 3 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
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__ = []
Expand Down Expand Up @@ -87,6 +84,13 @@
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
Expand Down
6 changes: 5 additions & 1 deletion psutil/_psposix.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
from ._common import usage_percent
from ._compat import PY3
from ._compat import unicode
from ._exceptions import TimeoutExpired


__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:
Expand Down
10 changes: 7 additions & 3 deletions psutil/_pssunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
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"]
Expand Down Expand Up @@ -85,6 +82,13 @@
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
Expand Down
Loading

0 comments on commit 5849143

Please sign in to comment.