Skip to content

Commit ed3066f

Browse files
authored
Merge pull request #79 from sneakers-the-rat/bugfix-detect-proc
2 parents 9ef1e31 + 4619ac9 commit ed3066f

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

news/78.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Detect proc format eagerly so it throws the error at the correct location.

src/shellingham/posix/proc.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,17 @@ class ProcFormatError(EnvironmentError):
6767
def iter_process_parents(pid, max_depth=10):
6868
"""Try to look up the process tree via the /proc interface."""
6969
stat_name = detect_proc()
70-
for _ in range(max_depth):
71-
ppid = _get_ppid(pid, stat_name)
72-
args = _get_cmdline(pid)
73-
yield Process(args=args, pid=pid, ppid=ppid)
74-
if ppid == "0":
75-
break
76-
pid = ppid
70+
71+
# Inner generator function so we correctly throw an error eagerly if proc
72+
# is not supported, rather than on the first call to the iterator. This
73+
# allows the call site detects the correct implementation.
74+
def _iter_process_parents(pid, max_depth):
75+
for _ in range(max_depth):
76+
ppid = _get_ppid(pid, stat_name)
77+
args = _get_cmdline(pid)
78+
yield Process(args=args, pid=pid, ppid=ppid)
79+
if ppid == "0":
80+
break
81+
pid = ppid
82+
83+
return _iter_process_parents(pid, max_depth)

0 commit comments

Comments
 (0)