Skip to content

Commit

Permalink
#854: Process.as_dict() raises ValueError if passed an erroneous attr…
Browse files Browse the repository at this point in the history
…s name.
  • Loading branch information
giampaolo committed Jul 10, 2016
1 parent e481df9 commit 077eadd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
8 changes: 8 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Bug tracker at https://github.com/giampaolo/psutil/issues

4.3.1 - XXXX-XX-XX
==================

**Bug fixes**

- #854: Process.as_dict() raises ValueError if passed an erroneous attrs name.


4.3.0 - 2016-06-18
==================

Expand Down
24 changes: 16 additions & 8 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
]
__all__.extend(_psplatform.__extra__all__)
__author__ = "Giampaolo Rodola'"
__version__ = "4.3.0"
__version__ = "4.3.1"
version_info = tuple([int(num) for num in __version__.split('.')])
AF_LINK = _psplatform.AF_LINK
_TOTAL_PHYMEM = None
Expand Down Expand Up @@ -462,17 +462,25 @@ def as_dict(self, attrs=None, ad_value=None):
['send_signal', 'suspend', 'resume', 'terminate', 'kill', 'wait',
'is_running', 'as_dict', 'parent', 'children', 'rlimit'])
valid_names = _process_attrnames - excluded_names
if attrs is not None:
if not isinstance(attrs, (list, tuple, set, frozenset)):
raise TypeError("invalid attrs type %s" % type(attrs))
attrs = set(attrs)
invalid_names = attrs - valid_names
if invalid_names:
raise ValueError("invalid attr name%s %s" % (
"s" if len(invalid_names) > 1 else "",
", ".join(map(repr, invalid_names))))

retdict = dict()
ls = set(attrs) if attrs else _process_attrnames
ls = attrs or valid_names
for name in ls:
if name not in valid_names:
continue
try:
attr = getattr(self, name)
if callable(attr):
ret = attr()
if name == 'pid':
ret = self.pid
else:
ret = attr
meth = getattr(self, name)
ret = meth()
except (AccessDenied, ZombieProcess):
ret = ad_value
except NotImplementedError:
Expand Down
14 changes: 12 additions & 2 deletions psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,14 +1218,24 @@ def test_as_dict(self):
if not isinstance(d['connections'], list):
self.assertEqual(d['connections'], 'foo')

with mock.patch('psutil.getattr', create=True,
with mock.patch('psutil.Process.name', create=True,
side_effect=NotImplementedError):
# By default APIs raising NotImplementedError are
# supposed to be skipped.
self.assertEqual(p.as_dict(), {})
d = p.as_dict()
self.assertNotIn('name', list(d.keys()))
# ...unless the user explicitly asked for some attr.
with self.assertRaises(NotImplementedError):
p.as_dict(attrs=["name"])
# errors
with self.assertRaises(TypeError) as cm:
p.as_dict('name')
with self.assertRaises(ValueError) as cm:
p.as_dict(['foo'])
self.assertEqual(str(cm.exception), "invalid attr name 'foo'")
with self.assertRaises(ValueError) as cm:
p.as_dict(['foo', 'bar'])
self.assertEqual(str(cm.exception), "invalid attr names 'foo', 'bar'")

def test_halfway_terminated_process(self):
# Test that NoSuchProcess exception gets raised in case the
Expand Down

0 comments on commit 077eadd

Please sign in to comment.