Skip to content

Commit

Permalink
use more readable variable aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Dec 21, 2024
1 parent bbd2c5b commit 0c0dd59
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 60 deletions.
17 changes: 8 additions & 9 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions psutil/_psaix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 19 additions & 18 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 8 additions & 7 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 7 additions & 6 deletions psutil/_pssunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0c0dd59

Please sign in to comment.