From b070015104ea01689fee9f7c91709c0e2d35a9a8 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sun, 16 Apr 2023 02:31:37 +0200 Subject: Fix #2237, OpenBSD, cwd(): return None instead of FileNotFoundError --- HISTORY.rst | 2 ++ psutil/arch/openbsd/proc.c | 10 ++++++++-- psutil/tests/test_contracts.py | 3 +-- psutil/tests/test_system.py | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 324961bc..d9463817 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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 ===== diff --git a/psutil/arch/openbsd/proc.c b/psutil/arch/openbsd/proc.c index 344d6010..e66cac66 100644 --- a/psutil/arch/openbsd/proc.c +++ b/psutil/arch/openbsd/proc.c @@ -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); } diff --git a/psutil/tests/test_contracts.py b/psutil/tests/test_contracts.py index 392eb69b..6859f69f 100755 --- a/psutil/tests/test_contracts.py +++ b/psutil/tests/test_contracts.py @@ -438,8 +438,7 @@ class TestFetchAllProcesses(PsutilTestCase): 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, '', {}): diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py index c2b1df84..414c86e9 100755 --- a/psutil/tests/test_system.py +++ b/psutil/tests/test_system.py @@ -187,7 +187,7 @@ class TestProcessAPIs(PsutilTestCase): # 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) -- cgit v1.2.1