Skip to content

Commit b070015

Browse files
committed
Fix #2237, OpenBSD, cwd(): return None instead of FileNotFoundError
1 parent b1c1a00 commit b070015

4 files changed

Lines changed: 12 additions & 5 deletions

File tree

HISTORY.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*used* are too high. We now match values shown by *htop* CLI utility.
4040
- 2236_, [NetBSD]: `Process.num_threads()`_ and `Process.threads()`_ return
4141
threads that are already terminated.
42+
- 2237_, [OpenBSD]: `Process.cwd()`_ may raise ``FileNotFoundError`` if cwd no
43+
longer exists. Return ``None`` instead.
4244

4345
5.9.4
4446
=====

psutil/arch/openbsd/proc.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,14 @@ psutil_proc_cwd(PyObject *self, PyObject *args) {
305305

306306
int name[] = { CTL_KERN, KERN_PROC_CWD, pid };
307307
if (sysctl(name, 3, path, &pathlen, NULL, 0) != 0) {
308-
PyErr_SetFromErrno(PyExc_OSError);
309-
return NULL;
308+
if (errno == ENOENT) {
309+
psutil_debug("sysctl(KERN_PROC_CWD) -> ENOENT converted to None");
310+
Py_RETURN_NONE; // mimic os.cpu_count()
311+
}
312+
else {
313+
PyErr_SetFromErrno(PyExc_OSError);
314+
return NULL;
315+
}
310316
}
311317
return PyUnicode_DecodeFSDefault(path);
312318
}

psutil/tests/test_contracts.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,7 @@ def test_all(self):
438438
name, info['pid'], repr(value))
439439
s += '-' * 70
440440
s += "\n%s" % traceback.format_exc()
441-
s = "\n".join((" " * 4) + i for i in s.splitlines())
442-
s += '\n'
441+
s = "\n".join((" " * 4) + i for i in s.splitlines()) + "\n"
443442
failures.append(s)
444443
else:
445444
if value not in (0, 0.0, [], None, '', {}):

psutil/tests/test_system.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def test_pid_exists_2(self):
187187
# if it is no longer in psutil.pids()
188188
time.sleep(.1)
189189
self.assertNotIn(pid, psutil.pids())
190-
pids = range(max(pids) + 5000, max(pids) + 6000)
190+
pids = range(max(pids) + 15000, max(pids) + 16000)
191191
for pid in pids:
192192
self.assertFalse(psutil.pid_exists(pid), msg=pid)
193193

0 commit comments

Comments
 (0)