Skip to content

Commit

Permalink
fix #642: repr() of exceptions is incorrect.
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Jul 9, 2015
1 parent cea4000 commit 2cbbd9f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
- #639: [Linux] Process.cmdline() can be truncated.
- #640: [Linux] *connections functions may swallow errors and return an
incomplete list of connnections.
- #642: repr() of exceptions is incorrect.

3.0.1 - 2015-06-18
Expand Down
8 changes: 6 additions & 2 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,12 @@ class Error(Exception):
def __init__(self, msg=""):
self.msg = msg

def __str__(self):
return self.msg
def __repr__(self):
ret = "%s.%s %s" % (self.__class__.__module__,
self.__class__.__name__, self.msg)
return ret.strip()

__str__ = __repr__


class NoSuchProcess(Error):
Expand Down
75 changes: 73 additions & 2 deletions test/test_psutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2635,8 +2635,79 @@ def test__str__(self):
self.assertIn("pid", str(p))
assert meth.called

def test__repr__(self):
repr(psutil.Process())
def test_process__repr__(self, func=repr):
p = psutil.Process()
r = func(p)
self.assertIn("psutil.Process", r)
self.assertIn("pid=%s" % p.pid, r)
self.assertIn("name='%s'" % p.name(), r)
with mock.patch.object(psutil.Process, "name",
side_effect=psutil.ZombieProcess(os.getpid())):
p = psutil.Process()
r = func(p)
self.assertIn("pid=%s" % p.pid, r)
self.assertIn("zombie", r)
with mock.patch.object(psutil.Process, "name",
side_effect=psutil.NoSuchProcess(os.getpid())):
p = psutil.Process()
r = func(p)
self.assertIn("pid=%s" % p.pid, r)
self.assertIn("terminated", r)

def test_process__str__(self):
self.test_process__repr__(func=str)

def test_no_such_process__repr__(self, func=repr):
self.assertEqual(
repr(psutil.NoSuchProcess(321)),
"psutil.NoSuchProcess process no longer exists (pid=321)")
self.assertEqual(
repr(psutil.NoSuchProcess(321, name='foo')),
"psutil.NoSuchProcess process no longer exists (pid=321, "
"name='foo')")
self.assertEqual(
repr(psutil.NoSuchProcess(321, msg='foo')),
"psutil.NoSuchProcess foo")

def test_zombie_process__repr__(self, func=repr):
self.assertEqual(
repr(psutil.ZombieProcess(321)),
"psutil.ZombieProcess process still exists but it's a zombie "
"(pid=321)")
self.assertEqual(
repr(psutil.ZombieProcess(321, name='foo')),
"psutil.ZombieProcess process still exists but it's a zombie "
"(pid=321, name='foo')")
self.assertEqual(
repr(psutil.ZombieProcess(321, name='foo', ppid=1)),
"psutil.ZombieProcess process still exists but it's a zombie "
"(pid=321, name='foo', ppid=1)")
self.assertEqual(
repr(psutil.ZombieProcess(321, msg='foo')),
"psutil.ZombieProcess foo")

def test_access_denied__repr__(self, func=repr):
self.assertEqual(
repr(psutil.AccessDenied(321)),
"psutil.AccessDenied (pid=321)")
self.assertEqual(
repr(psutil.AccessDenied(321, name='foo')),
"psutil.AccessDenied (pid=321, name='foo')")
self.assertEqual(
repr(psutil.AccessDenied(321, msg='foo')),
"psutil.AccessDenied foo")

def test_timeout_expired__repr__(self, func=repr):
self.assertEqual(
repr(psutil.TimeoutExpired(321)),
"psutil.TimeoutExpired timeout after 321 seconds")
self.assertEqual(
repr(psutil.TimeoutExpired(321, pid=111)),
"psutil.TimeoutExpired timeout after 321 seconds (pid=111)")
self.assertEqual(
repr(psutil.TimeoutExpired(321, pid=111, name='foo')),
"psutil.TimeoutExpired timeout after 321 seconds "
"(pid=111, name='foo')")

def test__eq__(self):
p1 = psutil.Process()
Expand Down

0 comments on commit 2cbbd9f

Please sign in to comment.