Skip to content

Commit

Permalink
refact: check valid net_connections() kinds from a single point
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Dec 20, 2024
1 parent e957628 commit 5d4be42
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 42 deletions.
11 changes: 10 additions & 1 deletion psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
Supported Python versions are cPython 3.6+ and PyPy.
"""


import collections
import contextlib
import datetime
Expand Down Expand Up @@ -275,6 +274,14 @@ def _pprint_secs(secs):
return datetime.datetime.fromtimestamp(secs).strftime(fmt)


def _check_conn_kind(kind):
"""Check net_connections()'s `kind` parameter."""
kinds = tuple(_common.conn_tmap)
if kind not in kinds:
msg = f"invalid kind argument {kind!r}; valid ones are: {kinds}"
raise ValueError(msg)


# =====================================================================
# --- Process class
# =====================================================================
Expand Down Expand Up @@ -1231,6 +1238,7 @@ def net_connections(self, kind='inet'):
| all | the sum of all the possible families and protocols |
+------------+----------------------------------------------------+
"""
_check_conn_kind(kind)
return self._proc.net_connections(kind)

@_common.deprecated_method(replacement="net_connections")
Expand Down Expand Up @@ -2191,6 +2199,7 @@ def net_connections(kind='inet'):
On macOS this function requires root privileges.
"""
_check_conn_kind(kind)
return _psplatform.net_connections(kind)


Expand Down
2 changes: 1 addition & 1 deletion psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class BatteryTime(enum.IntEnum):
"udp6": ([AF_INET6], [SOCK_DGRAM]),
})

if AF_UNIX is not None:
if AF_UNIX is not None and not SUNOS:
conn_tmap.update({"unix": ([AF_UNIX], [SOCK_STREAM, SOCK_DGRAM])})


Expand Down
6 changes: 0 additions & 6 deletions psutil/_psaix.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,6 @@ def net_connections(kind, _pid=-1):
"""Return socket connections. If pid == -1 return system-wide
connections (as opposed to connections opened by one process only).
"""
cmap = _common.conn_tmap
if kind not in cmap:
raise ValueError(
"invalid %r kind argument; choose between %s"
% (kind, ', '.join([repr(x) for x in cmap]))
)
families, types = _common.conn_tmap[kind]
rawlist = cext.net_connections(_pid)
ret = []
Expand Down
11 changes: 0 additions & 11 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,8 @@ def net_if_stats():

def net_connections(kind):
"""System-wide network connections."""
if kind not in _common.conn_tmap:
raise ValueError(
"invalid %r kind argument; choose between %s"
% (kind, ', '.join([repr(x) for x in conn_tmap]))
)
families, types = conn_tmap[kind]
ret = set()

if OPENBSD:
rawlist = cext.net_connections(-1, families, types)
elif NETBSD:
Expand Down Expand Up @@ -820,11 +814,6 @@ def threads(self):

@wrap_exceptions
def net_connections(self, kind='inet'):
if kind not in conn_tmap:
raise ValueError(
"invalid %r kind argument; choose between %s"
% (kind, ', '.join([repr(x) for x in conn_tmap]))
)
families, types = conn_tmap[kind]
ret = []

Expand Down
5 changes: 0 additions & 5 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,11 +955,6 @@ def process_unix(file, family, inodes, filter_pid=None):
yield (fd, family, type_, path, raddr, status, pid)

def retrieve(self, kind, pid=None):
if kind not in self.tmap:
raise ValueError(
"invalid %r kind argument; choose between %s"
% (kind, ', '.join([repr(x) for x in self.tmap]))
)
self._procfs_path = get_procfs_path()
if pid is not None:
inodes = self.get_proc_inodes(pid)
Expand Down
5 changes: 0 additions & 5 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,6 @@ def open_files(self):

@wrap_exceptions
def net_connections(self, kind='inet'):
if kind not in conn_tmap:
raise ValueError(
"invalid %r kind argument; choose between %s"
% (kind, ', '.join([repr(x) for x in conn_tmap]))
)
families, types = conn_tmap[kind]
rawlist = cext.proc_net_connections(self.pid, families, types)
ret = []
Expand Down
8 changes: 0 additions & 8 deletions psutil/_pssunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,6 @@ def net_connections(kind, _pid=-1):
connections (as opposed to connections opened by one process only).
Only INET sockets are returned (UNIX are not).
"""
cmap = _common.conn_tmap.copy()
if _pid == -1:
cmap.pop('unix', 0)
if kind not in cmap:
raise ValueError(
"invalid %r kind argument; choose between %s"
% (kind, ', '.join([repr(x) for x in cmap]))
)
families, types = _common.conn_tmap[kind]
rawlist = cext.net_connections(_pid)
ret = set()
Expand Down
5 changes: 0 additions & 5 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,6 @@ def net_connections(kind, _pid=-1):
"""Return socket connections. If pid == -1 return system-wide
connections (as opposed to connections opened by one process only).
"""
if kind not in conn_tmap:
raise ValueError(
"invalid %r kind argument; choose between %s"
% (kind, ', '.join([repr(x) for x in conn_tmap]))
)
families, types = conn_tmap[kind]
rawlist = cext.net_connections(_pid, families, types)
ret = set()
Expand Down

0 comments on commit 5d4be42

Please sign in to comment.