Skip to content

Commit

Permalink
fix #582: turn *connections() constants into enums on python 3.4+
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 7, 2015
1 parent 266112c commit 97ecaf5
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 21 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
**Enhancements**

- #581: add .gitignore. (patch by Gabi Davar)
- #582: connection constants returned by psutil.net_connections() and
psutil.Process.connections() were turned from int to enums on Python > 3.4.


2.2.1 - 2015-02-02
Expand Down
16 changes: 8 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ Network
'lo': netio(bytes_sent=2838627, bytes_recv=2838627, packets_sent=30567, packets_recv=30567, errin=0, errout=0, dropin=0, dropout=0)}
>>>
>>> psutil.net_connections()
[pconn(fd=115, family=2, type=1, laddr=('10.0.0.1', 48776), raddr=('93.186.135.91', 80), status='ESTABLISHED', pid=1254),
pconn(fd=117, family=2, type=1, laddr=('10.0.0.1', 43761), raddr=('72.14.234.100', 80), status='CLOSING', pid=2987),
pconn(fd=-1, family=2, type=1, laddr=('10.0.0.1', 60759), raddr=('72.14.234.104', 80), status='ESTABLISHED', pid=None),
pconn(fd=-1, family=2, type=1, laddr=('10.0.0.1', 51314), raddr=('72.14.234.83', 443), status='SYN_SENT', pid=None)
[pconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 48776), raddr=('93.186.135.91', 80), status='ESTABLISHED', pid=1254),
pconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 43761), raddr=('72.14.234.100', 80), status='CLOSING', pid=2987),
pconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 60759), raddr=('72.14.234.104', 80), status='ESTABLISHED', pid=None),
pconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 51314), raddr=('72.14.234.83', 443), status='SYN_SENT', pid=None)
...]
Other system info
Expand Down Expand Up @@ -229,10 +229,10 @@ Process management
[popenfile(path='/home/giampaolo/svn/psutil/somefile', fd=3)]
>>>
>>> p.connections()
[pconn(fd=115, family=2, type=1, laddr=('10.0.0.1', 48776), raddr=('93.186.135.91', 80), status='ESTABLISHED'),
pconn(fd=117, family=2, type=1, laddr=('10.0.0.1', 43761), raddr=('72.14.234.100', 80), status='CLOSING'),
pconn(fd=119, family=2, type=1, laddr=('10.0.0.1', 60759), raddr=('72.14.234.104', 80), status='ESTABLISHED'),
pconn(fd=123, family=2, type=1, laddr=('10.0.0.1', 51314), raddr=('72.14.234.83', 443), status='SYN_SENT')]
[pconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 48776), raddr=('93.186.135.91', 80), status='ESTABLISHED'),
pconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 43761), raddr=('72.14.234.100', 80), status='CLOSING'),
pconn(fd=119, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 60759), raddr=('72.14.234.104', 80), status='ESTABLISHED'),
pconn(fd=123, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 51314), raddr=('72.14.234.83', 443), status='SYN_SENT')]
>>>
>>> p.num_threads()
4
Expand Down
16 changes: 8 additions & 8 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ Network

>>> import psutil
>>> psutil.net_connections()
[pconn(fd=115, family=2, type=1, laddr=('10.0.0.1', 48776), raddr=('93.186.135.91', 80), status='ESTABLISHED', pid=1254),
pconn(fd=117, family=2, type=1, laddr=('10.0.0.1', 43761), raddr=('72.14.234.100', 80), status='CLOSING', pid=2987),
pconn(fd=-1, family=2, type=1, laddr=('10.0.0.1', 60759), raddr=('72.14.234.104', 80), status='ESTABLISHED', pid=None),
pconn(fd=-1, family=2, type=1, laddr=('10.0.0.1', 51314), raddr=('72.14.234.83', 443), status='SYN_SENT', pid=None)
[pconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 48776), raddr=('93.186.135.91', 80), status='ESTABLISHED', pid=1254),
pconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 43761), raddr=('72.14.234.100', 80), status='CLOSING', pid=2987),
pconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 60759), raddr=('72.14.234.104', 80), status='ESTABLISHED', pid=None),
pconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 51314), raddr=('72.14.234.83', 443), status='SYN_SENT', pid=None)
...]

.. note:: (OSX) :class:`psutil.AccessDenied` is always raised unless running
Expand Down Expand Up @@ -1041,10 +1041,10 @@ Process class
>>> p.name()
'firefox'
>>> p.connections()
[pconn(fd=115, family=2, type=1, laddr=('10.0.0.1', 48776), raddr=('93.186.135.91', 80), status='ESTABLISHED'),
pconn(fd=117, family=2, type=1, laddr=('10.0.0.1', 43761), raddr=('72.14.234.100', 80), status='CLOSING'),
pconn(fd=119, family=2, type=1, laddr=('10.0.0.1', 60759), raddr=('72.14.234.104', 80), status='ESTABLISHED'),
pconn(fd=123, family=2, type=1, laddr=('10.0.0.1', 51314), raddr=('72.14.234.83', 443), status='SYN_SENT')]
[pconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 48776), raddr=('93.186.135.91', 80), status='ESTABLISHED'),
pconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 43761), raddr=('72.14.234.100', 80), status='CLOSING'),
pconn(fd=119, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 60759), raddr=('72.14.234.104', 80), status='ESTABLISHED'),
pconn(fd=123, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=('10.0.0.1', 51314), raddr=('72.14.234.83', 443), status='SYN_SENT')]

.. method:: is_running()

Expand Down
30 changes: 29 additions & 1 deletion psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import threading
except ImportError:
import dummy_threading as threading
try:
import enum # py >= 3.4
except ImportError:
enum = None

from collections import namedtuple
from socket import AF_INET, SOCK_STREAM, SOCK_DGRAM
Expand Down Expand Up @@ -160,6 +164,30 @@ def isfile_strict(path):
return stat.S_ISREG(st.st_mode)


def sockfam_to_enum(num):
"""Convert a numeric socket family value to an IntEnum member.
If it's not a known member, return the numeric value itself.
"""
if enum is None:
return num
try:
return socket.AddressFamily(num)
except ValueError:
return num


def socktype_to_enum(num):
"""Convert a numeric socket type value to an IntEnum member.
If it's not a known member, return the numeric value itself.
"""
if enum is None:
return num
try:
return socket.AddressType(num)
except ValueError:
return num


# --- Process.connections() 'kind' parameter mapping

conn_tmap = {
Expand All @@ -184,7 +212,7 @@ def isfile_strict(path):
"unix": ([AF_UNIX], [SOCK_STREAM, SOCK_DGRAM]),
})

del AF_INET, AF_INET6, AF_UNIX, SOCK_STREAM, SOCK_DGRAM, socket
del AF_INET, AF_INET6, AF_UNIX, SOCK_STREAM, SOCK_DGRAM


# --- namedtuples for psutil.* system-related functions
Expand Down
7 changes: 6 additions & 1 deletion psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

from psutil import _common
from psutil import _psposix
from psutil._common import conn_tmap, usage_percent
from psutil._common import (conn_tmap, usage_percent, sockfam_to_enum,
socktype_to_enum)
import _psutil_bsd as cext
import _psutil_posix

Expand Down Expand Up @@ -191,6 +192,8 @@ def net_connections(kind):
# TODO: apply filter at C level
if fam in families and type in types:
status = TCP_STATUSES[status]
fam = sockfam_to_enum(fam)
type = socktype_to_enum(type)
nt = _common.sconn(fd, fam, type, laddr, raddr, status, pid)
ret.append(nt)
return ret
Expand Down Expand Up @@ -312,6 +315,8 @@ def connections(self, kind='inet'):
ret = []
for item in rawlist:
fd, fam, type, laddr, raddr, status = item
fam = sockfam_to_enum(fam)
type = socktype_to_enum(type)
status = TCP_STATUSES[status]
nt = _common.pconn(fd, fam, type, laddr, raddr, status)
ret.append(nt)
Expand Down
5 changes: 4 additions & 1 deletion psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

from psutil import _common
from psutil import _psposix
from psutil._common import conn_tmap, usage_percent, isfile_strict
from psutil._common import (conn_tmap, usage_percent, isfile_strict,
sockfam_to_enum, socktype_to_enum)
import _psutil_osx as cext
import _psutil_posix

Expand Down Expand Up @@ -292,6 +293,8 @@ def connections(self, kind='inet'):
for item in rawlist:
fd, fam, type, laddr, raddr, status = item
status = TCP_STATUSES[status]
fam = sockfam_to_enum(fam)
type = socktype_to_enum(type)
nt = _common.pconn(fd, fam, type, laddr, raddr, status)
ret.append(nt)
return ret
Expand Down
5 changes: 4 additions & 1 deletion psutil/_pssunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

from psutil import _common
from psutil import _psposix
from psutil._common import usage_percent, isfile_strict
from psutil._common import (usage_percent, isfile_strict, sockfam_to_enum,
socktype_to_enum)
from psutil._compat import PY3
import _psutil_posix
import _psutil_sunos as cext
Expand Down Expand Up @@ -218,6 +219,8 @@ def net_connections(kind, _pid=-1):
if type_ not in types:
continue
status = TCP_STATUSES[status]
fam = sockfam_to_enum(fam)
type_ = socktype_to_enum(type_)
if _pid == -1:
nt = _common.sconn(fd, fam, type_, laddr, raddr, status, pid)
else:
Expand Down
5 changes: 4 additions & 1 deletion psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from collections import namedtuple

from psutil import _common
from psutil._common import conn_tmap, usage_percent, isfile_strict
from psutil._common import (conn_tmap, usage_percent, isfile_strict,
sockfam_to_enum, socktype_to_enum)
from psutil._compat import PY3, xrange, lru_cache
import _psutil_windows as cext

Expand Down Expand Up @@ -171,6 +172,8 @@ def net_connections(kind, _pid=-1):
for item in rawlist:
fd, fam, type, laddr, raddr, status, pid = item
status = TCP_STATUSES[status]
fam = sockfam_to_enum(fam)
type = socktype_to_enum(type)
if _pid == -1:
nt = _common.sconn(fd, fam, type, laddr, raddr, status, pid)
else:
Expand Down
6 changes: 6 additions & 0 deletions test/test_psutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
import ipaddress # python >= 3.3
except ImportError:
ipaddress = None
try:
import enum # python >= 3.4
except ImportError:
enum = None

if sys.version_info < (2, 7):
import unittest2 as unittest # https://pypi.python.org/pypi/unittest2
Expand Down Expand Up @@ -282,6 +286,8 @@ def reap_children(search_all=False):

def check_ip_address(addr, family):
"""Attempts to check IP address's validity."""
if enum is not None:
assert isinstance(family, enum.IntEnum), family
if family == AF_INET:
octs = [int(x) for x in addr.split('.')]
assert len(octs) == 4, addr
Expand Down

0 comments on commit 97ecaf5

Please sign in to comment.