summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2023-04-16 02:31:37 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2023-04-16 02:31:37 +0200
commitb070015104ea01689fee9f7c91709c0e2d35a9a8 (patch)
treeaa42e2eb2829f70d06acbea72a21a93389289fdd
parentb1c1a005e5919291abc2edb5e6611806bce3346b (diff)
downloadpsutil-b070015104ea01689fee9f7c91709c0e2d35a9a8.tar.gz
Fix #2237, OpenBSD, cwd(): return None instead of FileNotFoundError
-rw-r--r--HISTORY.rst2
-rw-r--r--psutil/arch/openbsd/proc.c10
-rwxr-xr-xpsutil/tests/test_contracts.py3
-rwxr-xr-xpsutil/tests/test_system.py2
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)