summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-02-15 15:49:46 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2016-02-15 15:49:46 +0100
commit897e758a6a5ff749bfd0c8c5ea934d5a27e51f17 (patch)
tree2bafa75f287f9478aceb6b7bc559c47886bacef0
parent27f21ef061dca340b1ad5c706ae0916560b6dc1a (diff)
downloadpsutil-897e758a6a5ff749bfd0c8c5ea934d5a27e51f17.tar.gz
fix #766: [Linux] net_connections() can't handle malformed /proc/net/unix file.
-rw-r--r--HISTORY.rst1
-rw-r--r--Makefile2
-rw-r--r--psutil/_pslinux.py3
-rw-r--r--psutil/tests/test_linux.py19
4 files changed, 24 insertions, 1 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index faff03e7..0e4e2d8c 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -34,6 +34,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
- #759: [Linux] Process.memory_maps() may return paths ending with " (deleted)"
- #761: [Windows] psutil.boot_time() wraps to 0 after 49 days.
- #764: [NetBSD] fix compilation on NetBSD-6.x.
+- #766: [Linux] net_connections() can't handle malformed /proc/net/unix file.
- #767: [Linux] disk_io_counters() may raise ValueError on 2.6 kernels and it's
broken on 2.4 kernels.
- #770: [NetBSD] disk_io_counters() metrics didn't update.
diff --git a/Makefile b/Makefile
index c38bc206..f72611a8 100644
--- a/Makefile
+++ b/Makefile
@@ -82,7 +82,7 @@ test-by-name: install
# Run specific platform tests only.
test-platform: install
- $(PYTHON) psutil/tests/test_`python -c 'import psutil; print([x.lower() for x in ("FREEBSD", "LINUX", "NETBSD", "OPENBSD", "OSX", "SUNOS", "WINDOWS") if getattr(psutil, x)][0])'`.py
+ $(PYTHON) psutil/tests/test_`$(PYTHON) -c 'import psutil; print([x.lower() for x in ("FREEBSD", "LINUX", "NETBSD", "OPENBSD", "OSX", "SUNOS", "WINDOWS") if getattr(psutil, x)][0])'`.py
# Same as above but for test_memory_leaks.py script.
test-memleaks-by-name: install
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index 3ffdf25c..d82cb680 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -635,6 +635,9 @@ class Connections:
try:
_, _, _, _, type_, _, inode = tokens[0:7]
except ValueError:
+ if ' ' not in line:
+ # see: https://github.com/giampaolo/psutil/issues/766
+ continue
raise RuntimeError(
"error while parsing %s; malformed line %r" % (
file, line))
diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py
index 149b0f51..d80337fc 100644
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -342,6 +342,25 @@ class TestSystemNetwork(unittest.TestCase):
pass
psutil.net_connections(kind='inet6')
+ def test_net_connections_mocked(self):
+ def open_mock(name, *args, **kwargs):
+ if name == '/proc/net/unix':
+ return io.StringIO(textwrap.dedent(u"""\
+ 0: 00000003 000 000 0001 03 462170 @/tmp/dbus-Qw2hMPIU3n
+ 0: 00000003 000 000 0001 03 35010 @/tmp/dbus-tB2X8h69BQ
+ 0: 00000003 000 000 0001 03 34424 @/tmp/dbus-cHy80Y8O
+ 000000000000000000000000000000000000000000000000000000
+ """))
+ else:
+ return orig_open(name, *args, **kwargs)
+ return orig_open(name, *args)
+
+ orig_open = open
+ patch_point = 'builtins.open' if PY3 else '__builtin__.open'
+ with mock.patch(patch_point, side_effect=open_mock) as m:
+ psutil.net_connections(kind='unix')
+ assert m.called
+
# =====================================================================
# system disk