Skip to content

Commit

Permalink
Fix #2237, OpenBSD, cwd(): return None instead of FileNotFoundError
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Apr 16, 2023
1 parent b1c1a00 commit b070015
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
*used* are too high. We now match values shown by *htop* CLI utility.
- 2236_, [NetBSD]: `Process.num_threads()`_ and `Process.threads()`_ return
threads that are already terminated.
- 2237_, [OpenBSD]: `Process.cwd()`_ may raise ``FileNotFoundError`` if cwd no
longer exists. Return ``None`` instead.

5.9.4
=====
Expand Down
10 changes: 8 additions & 2 deletions psutil/arch/openbsd/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,14 @@ psutil_proc_cwd(PyObject *self, PyObject *args) {

int name[] = { CTL_KERN, KERN_PROC_CWD, pid };
if (sysctl(name, 3, path, &pathlen, NULL, 0) != 0) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
if (errno == ENOENT) {
psutil_debug("sysctl(KERN_PROC_CWD) -> ENOENT converted to None");
Py_RETURN_NONE; // mimic os.cpu_count()
}
else {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
}
return PyUnicode_DecodeFSDefault(path);
}
3 changes: 1 addition & 2 deletions psutil/tests/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,7 @@ def test_all(self):
name, info['pid'], repr(value))
s += '-' * 70
s += "\n%s" % traceback.format_exc()
s = "\n".join((" " * 4) + i for i in s.splitlines())
s += '\n'
s = "\n".join((" " * 4) + i for i in s.splitlines()) + "\n"
failures.append(s)
else:
if value not in (0, 0.0, [], None, '', {}):
Expand Down
2 changes: 1 addition & 1 deletion psutil/tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def test_pid_exists_2(self):
# if it is no longer in psutil.pids()
time.sleep(.1)
self.assertNotIn(pid, psutil.pids())
pids = range(max(pids) + 5000, max(pids) + 6000)
pids = range(max(pids) + 15000, max(pids) + 16000)
for pid in pids:
self.assertFalse(psutil.pid_exists(pid), msg=pid)

Expand Down

0 comments on commit b070015

Please sign in to comment.