From 0c0dd591bd079f4619a6f399d20c3c5f201c30c5 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 21 Dec 2024 14:22:38 +0100 Subject: [PATCH] use more readable variable aliases --- psutil/__init__.py | 17 ++++++++--------- psutil/_psaix.py | 9 +++++---- psutil/_psbsd.py | 37 +++++++++++++++++++------------------ psutil/_pslinux.py | 15 ++++++++------- psutil/_psosx.py | 13 +++++++------ psutil/_pssunos.py | 13 +++++++------ psutil/_pswindows.py | 20 ++++++++++---------- 7 files changed, 64 insertions(+), 60 deletions(-) diff --git a/psutil/__init__.py b/psutil/__init__.py index e3c8da847..ad4bd7388 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -325,9 +325,9 @@ def _init(self, pid, _ignore_nsp=False): raise ValueError(msg) try: _psplatform.cext.check_pid_range(pid) - except OverflowError as e: + except OverflowError as err: msg = "process PID out of range" - raise NoSuchProcess(pid, msg=msg) from e + raise NoSuchProcess(pid, msg=msg) from err self._pid = pid self._name = None @@ -1260,18 +1260,17 @@ def _send_signal(self, sig): raise ValueError(msg) try: os.kill(self.pid, sig) - except ProcessLookupError as e: + except ProcessLookupError as err: if OPENBSD and pid_exists(self.pid): # We do this because os.kill() lies in case of # zombie processes. - raise ZombieProcess( - self.pid, self._name, self._ppid - ) from e + ppid = self._ppid + raise ZombieProcess(self.pid, self._name, ppid) from err else: self._gone = True - raise NoSuchProcess(self.pid, self._name) from e - except PermissionError as e: - raise AccessDenied(self.pid, self._name) from e + raise NoSuchProcess(self.pid, self._name) from err + except PermissionError as err: + raise AccessDenied(self.pid, self._name) from err def send_signal(self, sig): """Send a signal *sig* to process pre-emptively checking diff --git a/psutil/_psaix.py b/psutil/_psaix.py index 52640f494..39fa69d10 100644 --- a/psutil/_psaix.py +++ b/psutil/_psaix.py @@ -317,18 +317,19 @@ def wrap_exceptions(fun): @functools.wraps(fun) def wrapper(self, *args, **kwargs): + pid, ppid, name = self.pid, self._ppid, self._name try: return fun(self, *args, **kwargs) except (FileNotFoundError, ProcessLookupError) as err: # ENOENT (no such file or directory) gets raised on open(). # ESRCH (no such process) can get raised on read() if # process is gone in meantime. - if not pid_exists(self.pid): - raise NoSuchProcess(self.pid, self._name) from err + if not pid_exists(pid): + raise NoSuchProcess(pid, name) from err else: - raise ZombieProcess(self.pid, self._name, self._ppid) from err + raise ZombieProcess(pid, name, ppid) from err except PermissionError as err: - raise AccessDenied(self.pid, self._name) from err + raise AccessDenied(pid, name) from err return wrapper diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index abd79ed35..84ad4c603 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -590,18 +590,19 @@ def wrap_exceptions(fun): @functools.wraps(fun) def wrapper(self, *args, **kwargs): + pid, ppid, name = self.pid, self._ppid, self._name try: return fun(self, *args, **kwargs) - except ProcessLookupError as e: - if is_zombie(self.pid): - raise ZombieProcess(self.pid, self._name, self._ppid) from e + except ProcessLookupError as err: + if is_zombie(pid): + raise ZombieProcess(pid, name, ppid) from err else: - raise NoSuchProcess(self.pid, self._name) from e - except PermissionError as e: - raise AccessDenied(self.pid, self._name) from e - except OSError as e: - if self.pid == 0 and 0 in pids(): - raise AccessDenied(self.pid, self._name) from e + raise NoSuchProcess(pid, name) from err + except PermissionError as err: + raise AccessDenied(pid, name) from err + except OSError as err: + if pid == 0 and 0 in pids(): + raise AccessDenied(pid, name) from err raise return wrapper @@ -610,18 +611,19 @@ def wrapper(self, *args, **kwargs): @contextlib.contextmanager def wrap_exceptions_procfs(inst): """Same as above, for routines relying on reading /proc fs.""" + pid, name, ppid = inst.pid, inst._name, inst._ppid try: yield - except (ProcessLookupError, FileNotFoundError) as e: + except (ProcessLookupError, FileNotFoundError) as err: # ENOENT (no such file or directory) gets raised on open(). # ESRCH (no such process) can get raised on read() if # process is gone in meantime. if is_zombie(inst.pid): - raise ZombieProcess(inst.pid, inst._name, inst._ppid) from e + raise ZombieProcess(pid, name, ppid) from err else: - raise NoSuchProcess(inst.pid, inst._name) from e - except PermissionError as e: - raise AccessDenied(inst.pid, inst._name) from e + raise NoSuchProcess(pid, name) from err + except PermissionError as err: + raise AccessDenied(pid, name) from err class Process: @@ -698,12 +700,11 @@ def cmdline(self): return cext.proc_cmdline(self.pid) except OSError as err: if err.errno == errno.EINVAL: - # fmt: off + pid, name, ppid = self.pid, self._name, self._ppid if is_zombie(self.pid): - raise ZombieProcess(self.pid, self._name, self._ppid) from err # noqa: E501 + raise ZombieProcess(pid, name, ppid) from err elif not pid_exists(self.pid): - raise NoSuchProcess(self.pid, self._name, self._ppid) from err # noqa: E501 - # fmt: on + raise NoSuchProcess(pid, name, ppid) from err else: # XXX: this happens with unicode tests. It means the C # routine is unable to decode invalid unicode chars. diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 6e02bd5f6..489000fdc 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -1642,20 +1642,21 @@ def wrap_exceptions(fun): @functools.wraps(fun) def wrapper(self, *args, **kwargs): + pid, name = self.pid, self._name try: return fun(self, *args, **kwargs) - except PermissionError as e: - raise AccessDenied(self.pid, self._name) from e - except ProcessLookupError as e: + except PermissionError as err: + raise AccessDenied(pid, name) from err + except ProcessLookupError as err: self._raise_if_zombie() - raise NoSuchProcess(self.pid, self._name) from e - except FileNotFoundError as e: + raise NoSuchProcess(pid, name) from err + except FileNotFoundError as err: self._raise_if_zombie() # /proc/PID directory may still exist, but the files within # it may not, indicating the process is gone, see: # https://github.com/giampaolo/psutil/issues/2418 - if not os.path.exists(f"{self._procfs_path}/{self.pid}/stat"): - raise NoSuchProcess(self.pid, self._name) from e + if not os.path.exists(f"{self._procfs_path}/{pid}/stat"): + raise NoSuchProcess(pid, name) from err raise return wrapper diff --git a/psutil/_psosx.py b/psutil/_psosx.py index 57012346a..95cf3ae17 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -343,15 +343,16 @@ def wrap_exceptions(fun): @functools.wraps(fun) def wrapper(self, *args, **kwargs): + pid, ppid, name = self.pid, self._ppid, self._name try: return fun(self, *args, **kwargs) - except ProcessLookupError as e: - if is_zombie(self.pid): - raise ZombieProcess(self.pid, self._name, self._ppid) from e + except ProcessLookupError as err: + if is_zombie(pid): + raise ZombieProcess(pid, name, ppid) from err else: - raise NoSuchProcess(self.pid, self._name) from e - except PermissionError as e: - raise AccessDenied(self.pid, self._name) from e + raise NoSuchProcess(pid, name) from err + except PermissionError as err: + raise AccessDenied(pid, name) from err return wrapper diff --git a/psutil/_pssunos.py b/psutil/_pssunos.py index afbbf4ec0..331e33e1c 100644 --- a/psutil/_pssunos.py +++ b/psutil/_pssunos.py @@ -350,22 +350,23 @@ def wrap_exceptions(fun): @functools.wraps(fun) def wrapper(self, *args, **kwargs): + pid, ppid, name = self.pid, self._ppid, self._name try: return fun(self, *args, **kwargs) except (FileNotFoundError, ProcessLookupError) as err: # ENOENT (no such file or directory) gets raised on open(). # ESRCH (no such process) can get raised on read() if # process is gone in meantime. - if not pid_exists(self.pid): - raise NoSuchProcess(self.pid, self._name) from err + if not pid_exists(pid): + raise NoSuchProcess(pid, name) from err else: - raise ZombieProcess(self.pid, self._name, self._ppid) from err + raise ZombieProcess(pid, name, ppid) from err except PermissionError as err: - raise AccessDenied(self.pid, self._name) from err + raise AccessDenied(pid, name) from err except OSError as err: - if self.pid == 0: + if pid == 0: if 0 in pids(): - raise AccessDenied(self.pid, self._name) from err + raise AccessDenied(pid, name) from err else: raise raise diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index 083d563b8..f02eba357 100644 --- a/psutil/_pswindows.py +++ b/psutil/_pswindows.py @@ -519,19 +519,19 @@ def _wrap_exceptions(self): """ try: yield - except OSError as ex: - if is_permission_err(ex): + except OSError as err: + name = self._name + if is_permission_err(err): msg = ( - f"service {self._name!r} is not querable (not enough" - " privileges)" + f"service {name!r} is not querable (not enough privileges)" ) - raise AccessDenied(pid=None, name=self._name, msg=msg) from ex - elif ex.winerror in { + raise AccessDenied(pid=None, name=name, msg=msg) from err + elif err.winerror in { cext.ERROR_INVALID_NAME, cext.ERROR_SERVICE_DOES_NOT_EXIST, }: - msg = f"service {self._name!r} does not exist" - raise NoSuchProcess(pid=None, name=self._name, msg=msg) from ex + msg = f"service {name!r} does not exist" + raise NoSuchProcess(pid=None, name=name, msg=msg) from err else: raise @@ -879,9 +879,9 @@ def wait(self, timeout=None): # May also be None if OpenProcess() failed with # ERROR_INVALID_PARAMETER, meaning PID is already gone. exit_code = cext.proc_wait(self.pid, cext_timeout) - except cext.TimeoutExpired as e: + except cext.TimeoutExpired as err: # WaitForSingleObject() returned WAIT_TIMEOUT. Just raise. - raise TimeoutExpired(timeout, self.pid, self._name) from e + raise TimeoutExpired(timeout, self.pid, self._name) from err except cext.TimeoutAbandoned: # WaitForSingleObject() returned WAIT_ABANDONED, see: # https://github.com/giampaolo/psutil/issues/1224