From 37cce29b78be8bde55abc2512ca318b760d3829d Mon Sep 17 00:00:00 2001 From: David Murphy Date: Tue, 22 Oct 2024 14:55:07 -0600 Subject: [PATCH] Revert requirements for psutil and allow for connections / net_connections based on psutil version (#55) * Revert requirements for psutil and allow for connections / net_connections based on psutil version * Added changelog entry --- changelog/55.improvement.rst | 1 + requirements/base.txt | 2 +- src/pytestshellutils/shell.py | 48 ++++++++++++++++++++++++----------- 3 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 changelog/55.improvement.rst diff --git a/changelog/55.improvement.rst b/changelog/55.improvement.rst new file mode 100644 index 0000000..c18f1c0 --- /dev/null +++ b/changelog/55.improvement.rst @@ -0,0 +1 @@ +Revert requirements for psutil and allow for connections / net_connections based on psutil version diff --git a/requirements/base.txt b/requirements/base.txt index 080c496..3b620f3 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -1,5 +1,5 @@ pytest>=7.4.0 attrs>=22.1.0 -psutil>=6.0.0 +psutil>=5.0.0 pytest-helpers-namespace pytest-skip-markers diff --git a/src/pytestshellutils/shell.py b/src/pytestshellutils/shell.py index 3a772a9..77cbccd 100644 --- a/src/pytestshellutils/shell.py +++ b/src/pytestshellutils/shell.py @@ -1278,21 +1278,39 @@ def _terminate_processes_matching_listen_ports(self) -> None: # If any processes were not terminated and are listening on the ports # we have set on listen_ports, terminate those processes. found_processes = [] - for process in psutil.process_iter(["net_connections"]): - try: - for connection in process.net_connections(): - if connection.status != psutil.CONN_LISTEN: - # We only care about listening services - continue - if connection.laddr.port in self.check_ports: - found_processes.append(process) - # We already found one connection, no need to check the others - break - except psutil.AccessDenied: # pragma: no cover - # We've been denied access to this process net_connections. Carry on. - continue - except psutil.ZombieProcess: - continue + psutil_majorver, _, _ = psutil.version_info + if psutil_majorver < 6: + for process in psutil.process_iter(["connections"]): + try: + for connection in process.connections(): + if connection.status != psutil.CONN_LISTEN: + # We only care about listening services + continue + if connection.laddr.port in self.check_ports: + found_processes.append(process) + # We already found one connection, no need to check the others + break + except psutil.AccessDenied: # pragma: no cover + # We've been denied access to this process connections. Carry on. + continue + except psutil.ZombieProcess: + continue + else: + for process in psutil.process_iter(["net_connections"]): + try: + for connection in process.net_connections(): + if connection.status != psutil.CONN_LISTEN: + # We only care about listening services + continue + if connection.laddr.port in self.check_ports: + found_processes.append(process) + # We already found one connection, no need to check the others + break + except psutil.AccessDenied: # pragma: no cover + # We've been denied access to this process net_connections. Carry on. + continue + except psutil.ZombieProcess: + continue if found_processes: log.debug( "The following processes were found listening on ports %s: %s",