diff options
Diffstat (limited to 'psutil')
-rw-r--r-- | psutil/__init__.py | 8 | ||||
-rw-r--r-- | psutil/tests/__init__.py | 7 | ||||
-rwxr-xr-x | psutil/tests/test_misc.py | 20 |
3 files changed, 22 insertions, 13 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py index b03ffaee..411c6395 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -396,18 +396,22 @@ class Process(object): except AttributeError: info = {} # Python 2.6 info["pid"] = self.pid + if self._name: + info['name'] = self._name with self.oneshot(): try: info["name"] = self.name() info["status"] = self.status() - if self._create_time: - info['started'] = _pprint_secs(self._create_time) except ZombieProcess: info["status"] = "zombie" except NoSuchProcess: info["status"] = "terminated" except AccessDenied: pass + if self._exitcode not in (_SENTINEL, None): + info["exitcode"] = self._exitcode + if self._create_time: + info['started'] = _pprint_secs(self._create_time) return "%s.%s(%s)" % ( self.__class__.__module__, self.__class__.__name__, diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index ed06df77..ffb73f5b 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -874,17 +874,14 @@ class PsutilTestCase(TestCase): self.addCleanup(terminate, sproc) # executed first return sproc - def assertPidGone(self, pid): - assert not psutil.pid_exists(pid), pid - self.assertNotIn(pid, psutil.pids()) - def assertProcessGone(self, proc): self.assertRaises(psutil.NoSuchProcess, psutil.Process, proc.pid) if isinstance(proc, (psutil.Process, psutil.Popen)): assert not proc.is_running() self.assertRaises(psutil.NoSuchProcess, proc.status) proc.wait(timeout=0) # assert not raise TimeoutExpired - self.assertPidGone(proc.pid) + assert not psutil.pid_exists(proc.pid), proc.pid + self.assertNotIn(proc.pid, psutil.pids()) @unittest.skipIf(PYPY, "unreliable on PYPY") diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py index 4fb8ba5a..959d12e5 100755 --- a/psutil/tests/test_misc.py +++ b/psutil/tests/test_misc.py @@ -57,20 +57,25 @@ import psutil.tests class TestMisc(PsutilTestCase): def test_process__repr__(self, func=repr): - p = psutil.Process() + p = psutil.Process(self.spawn_testproc().pid) r = func(p) self.assertIn("psutil.Process", r) self.assertIn("pid=%s" % p.pid, r) - self.assertIn("name=", r) + self.assertIn("name='%s'" % p.name(), r) self.assertIn("status=", r) - self.assertIn(p.name(), r) - self.assertIn("status='running'", r) + self.assertNotIn("exitcode=", r) + p.terminate() + code = p.wait() + r = func(p) + self.assertIn("status='terminated'", r) + self.assertIn("exitcode=%s" % code, 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) + self.assertIn("status='zombie'", r) self.assertNotIn("name=", r) with mock.patch.object(psutil.Process, "name", side_effect=psutil.NoSuchProcess(os.getpid())): @@ -303,7 +308,10 @@ class TestMisc(PsutilTestCase): else: with self.assertRaises(Exception): sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) - sock.bind(("::1", 0)) + try: + sock.bind(("::1", 0)) + finally: + sock.close() def test_isfile_strict(self): from psutil._common import isfile_strict |