diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2019-06-28 15:12:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-28 15:12:56 +0200 |
commit | c10df5aa04e1ced58d19501fa42f08c1b909b83d (patch) | |
tree | 053e13469d3959524d0cdb556aafe55ba753256c /psutil/_psposix.py | |
parent | adc28e4bd6534641f2c85ccc42529e6f25e97867 (diff) | |
download | psutil-c10df5aa04e1ced58d19501fa42f08c1b909b83d.tar.gz |
PEP-3151: backport FS exceptions to Python 2 (#1544)
Diffstat (limited to 'psutil/_psposix.py')
-rw-r--r-- | psutil/_psposix.py | 63 |
1 files changed, 28 insertions, 35 deletions
diff --git a/psutil/_psposix.py b/psutil/_psposix.py index d362143f..24570224 100644 --- a/psutil/_psposix.py +++ b/psutil/_psposix.py @@ -4,7 +4,6 @@ """Routines common to all posix systems.""" -import errno import glob import os import sys @@ -13,6 +12,11 @@ import time from ._common import memoize from ._common import sdiskusage from ._common import usage_percent +from ._compat import ChildProcessError +from ._compat import FileNotFoundError +from ._compat import InterruptedError +from ._compat import PermissionError +from ._compat import ProcessLookupError from ._compat import PY3 from ._compat import unicode @@ -36,19 +40,13 @@ def pid_exists(pid): return True try: os.kill(pid, 0) - except OSError as err: - if err.errno == errno.ESRCH: - # ESRCH == No such process - return False - elif err.errno == errno.EPERM: - # EPERM clearly means there's a process to deny access to - return True - else: - # According to "man 2 kill" possible error values are - # (EINVAL, EPERM, ESRCH) therefore we should never get - # here. If we do let's be explicit in considering this - # an error. - raise err + except ProcessLookupError: + return False + except PermissionError: + # EPERM clearly means there's a process to deny access to + return True + # According to "man 2 kill" possible error values are + # (EINVAL, EPERM, ESRCH) else: return True @@ -84,24 +82,20 @@ def wait_pid(pid, timeout=None, proc_name=None): while True: try: retpid, status = waitcall() - except OSError as err: - if err.errno == errno.EINTR: - delay = check_timeout(delay) - continue - elif err.errno == errno.ECHILD: - # This has two meanings: - # - pid is not a child of os.getpid() in which case - # we keep polling until it's gone - # - pid never existed in the first place - # In both cases we'll eventually return None as we - # can't determine its exit status code. - while True: - if pid_exists(pid): - delay = check_timeout(delay) - else: - return - else: - raise + except InterruptedError: + delay = check_timeout(delay) + except ChildProcessError: + # This has two meanings: + # - pid is not a child of os.getpid() in which case + # we keep polling until it's gone + # - pid never existed in the first place + # In both cases we'll eventually return None as we + # can't determine its exit status code. + while True: + if pid_exists(pid): + delay = check_timeout(delay) + else: + return else: if retpid == 0: # WNOHANG was used, pid is still running @@ -180,7 +174,6 @@ def get_terminal_map(): assert name not in ret, name try: ret[os.stat(name).st_rdev] = name - except OSError as err: - if err.errno != errno.ENOENT: - raise + except FileNotFoundError: + pass return ret |