summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-02-04 16:04:02 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2019-02-04 16:04:02 +0100
commit584914306e50aec251476014ac3934810039e7b5 (patch)
tree62f5d7c77f98371c52b072648ef420cc8c51b8e3
parent3d65245b72616c956619d6615607ce21561b10d2 (diff)
downloadpsutil-584914306e50aec251476014ac3934810039e7b5.tar.gz
fix #1402: move psutil exceptions back into __init__.py
-rw-r--r--HISTORY.rst3
-rw-r--r--psutil/__init__.py115
-rw-r--r--psutil/_exceptions.py94
-rw-r--r--psutil/_psaix.py10
-rw-r--r--psutil/_psbsd.py10
-rw-r--r--psutil/_pslinux.py10
-rw-r--r--psutil/_psosx.py10
-rw-r--r--psutil/_psposix.py6
-rw-r--r--psutil/_pssunos.py10
-rw-r--r--psutil/_pswindows.py10
10 files changed, 156 insertions, 122 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index ed5b8bc8..73609c76 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -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**
diff --git a/psutil/__init__.py b/psutil/__init__.py
index a0258b20..3259dd0e 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -17,7 +17,7 @@ sensors) in Python. Supported platforms:
- 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
@@ -87,12 +87,6 @@ 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 TimeoutExpired
-from ._exceptions import ZombieProcess
-
if LINUX:
# This is public API and it will be retrieved from _pslinux.py
# via sys.modules.
@@ -229,7 +223,6 @@ POWER_TIME_UNKNOWN = _common.POWER_TIME_UNKNOWN
_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
@@ -253,6 +246,112 @@ 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/_exceptions.py b/psutil/_exceptions.py
deleted file mode 100644
index 6dbbd282..00000000
--- a/psutil/_exceptions.py
+++ /dev/null
@@ -1,94 +0,0 @@
-# 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 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
diff --git a/psutil/_psaix.py b/psutil/_psaix.py
index 9975545a..58ecf17f 100644
--- a/psutil/_psaix.py
+++ b/psutil/_psaix.py
@@ -28,9 +28,6 @@ from ._common import sockfam_to_enum
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"]
@@ -79,6 +76,13 @@ 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 6683a200..0581de29 100644
--- a/psutil/_psbsd.py
+++ b/psutil/_psbsd.py
@@ -28,9 +28,6 @@ from ._common import sockfam_to_enum
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__ = []
@@ -137,6 +134,13 @@ 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 5c8cc20c..011b3541 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -43,9 +43,6 @@ from ._compat import b
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
@@ -161,6 +158,13 @@ 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 015c5b41..72b343f5 100644
--- a/psutil/_psosx.py
+++ b/psutil/_psosx.py
@@ -23,9 +23,6 @@ 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 AccessDenied
-from ._exceptions import NoSuchProcess
-from ._exceptions import ZombieProcess
__extra__all__ = []
@@ -87,6 +84,13 @@ 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 9c3fac27..d362143f 100644
--- a/psutil/_psposix.py
+++ b/psutil/_psposix.py
@@ -15,12 +15,16 @@ from ._common import sdiskusage
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:
diff --git a/psutil/_pssunos.py b/psutil/_pssunos.py
index 730af393..faadecfe 100644
--- a/psutil/_pssunos.py
+++ b/psutil/_pssunos.py
@@ -24,9 +24,6 @@ from ._common import socktype_to_enum
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"]
@@ -85,6 +82,13 @@ 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 664d5b6b..1aeb46ef 100644
--- a/psutil/_pswindows.py
+++ b/psutil/_pswindows.py
@@ -46,9 +46,6 @@ from ._compat import lru_cache
from ._compat import PY3
from ._compat import unicode
from ._compat import xrange
-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
from ._psutil_windows import HIGH_PRIORITY_CLASS
@@ -143,6 +140,13 @@ 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
+
# =====================================================================
# --- named tuples