Skip to content

Commit

Permalink
Merge pull request #79 from sneakers-the-rat/bugfix-detect-proc
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr committed Aug 17, 2023
2 parents 9ef1e31 + 4619ac9 commit ed3066f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions news/78.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Detect proc format eagerly so it throws the error at the correct location.
21 changes: 14 additions & 7 deletions src/shellingham/posix/proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,17 @@ class ProcFormatError(EnvironmentError):
def iter_process_parents(pid, max_depth=10):
"""Try to look up the process tree via the /proc interface."""
stat_name = detect_proc()
for _ in range(max_depth):
ppid = _get_ppid(pid, stat_name)
args = _get_cmdline(pid)
yield Process(args=args, pid=pid, ppid=ppid)
if ppid == "0":
break
pid = ppid

# Inner generator function so we correctly throw an error eagerly if proc
# is not supported, rather than on the first call to the iterator. This
# allows the call site detects the correct implementation.
def _iter_process_parents(pid, max_depth):
for _ in range(max_depth):
ppid = _get_ppid(pid, stat_name)
args = _get_cmdline(pid)
yield Process(args=args, pid=pid, ppid=ppid)
if ppid == "0":
break
pid = ppid

return _iter_process_parents(pid, max_depth)

0 comments on commit ed3066f

Please sign in to comment.