def setUp(self): env = os.environ.copy() env["THINK_OF_A_NUMBER"] = str(os.getpid()) self.proc32 = get_test_subprocess([self.python32] + self.test_args, env=env, stdin=subprocess.PIPE) self.proc64 = get_test_subprocess([self.python64] + self.test_args, env=env, stdin=subprocess.PIPE)
def test_get_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime p = get_test_subprocess(["ps", "ax", "-o", "pid"], stdout=subprocess.PIPE) output = p.communicate()[0].strip() if PY3: output = str(output, sys.stdout.encoding) output = output.replace("PID", "") p.wait() pids_ps = [] for pid in output.split("\n"): if pid: pids_ps.append(int(pid.strip())) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) pids_psutil = psutil.get_pid_list() pids_ps.sort() pids_psutil.sort() # on OSX ps doesn't show pid 0 if OSX and 0 not in pids_ps: pids_ps.insert(0, 0) if pids_ps != pids_psutil: difference = [x for x in pids_psutil if x not in pids_ps] + [x for x in pids_ps if x not in pids_psutil] self.fail("difference: " + str(difference))
def test_get_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime p = get_test_subprocess(["ps", "ax", "-o", "pid"], stdout=subprocess.PIPE) output = p.communicate()[0].strip() if PY3: output = str(output, sys.stdout.encoding) output = output.replace('PID', '') p.wait() pids_ps = [] for pid in output.split('\n'): if pid: pids_ps.append(int(pid.strip())) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) pids_psutil = psutil.get_pid_list() pids_ps.sort() pids_psutil.sort() # on OSX ps doesn't show pid 0 if OSX and 0 not in pids_ps: pids_ps.insert(0, 0) if pids_ps != pids_psutil: difference = [x for x in pids_psutil if x not in pids_ps] + \ [x for x in pids_ps if x not in pids_psutil] self.fail("difference: " + str(difference))
def test_get_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime p = get_test_subprocess(["ps", "ax", "-o", "pid"], stdout=subprocess.PIPE) output = p.communicate()[0].strip() if sys.version_info >= (3,): output = str(output, sys.stdout.encoding) output = output.replace('PID', '') p.wait() pids_ps = [] for pid in output.split('\n'): if pid: pids_ps.append(int(pid.strip())) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) # not all systems include pid 0 in their list but psutil does so # we force it if 0 not in pids_ps: pids_ps.append(0) pids_psutil = psutil.get_pid_list() pids_ps.sort() pids_psutil.sort() if pids_ps != pids_psutil: difference = filter(lambda x:x not in pids_ps, pids_psutil) + \ filter(lambda x:x not in pids_psutil, pids_ps) self.fail("difference: " + str(difference))
def test_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime if SUNOS: cmd = ["ps", "ax"] else: cmd = ["ps", "ax", "-o", "pid"] p = get_test_subprocess(cmd, stdout=subprocess.PIPE) output = p.communicate()[0].strip() if PY3: output = str(output, sys.stdout.encoding) pids_ps = [] for line in output.split('\n')[1:]: if line: pid = int(line.split()[0].strip()) pids_ps.append(pid) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) pids_psutil = psutil.pids() pids_ps.sort() pids_psutil.sort() # on OSX ps doesn't show pid 0 if OSX and 0 not in pids_ps: pids_ps.insert(0, 0) if pids_ps != pids_psutil: difference = [x for x in pids_psutil if x not in pids_ps] + \ [x for x in pids_ps if x not in pids_psutil] self.fail("difference: " + str(difference))
def test_get_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime p = get_test_subprocess(["ps", "ax", "-o", "pid"], stdout=subprocess.PIPE) output = p.communicate()[0].strip() if sys.version_info >= (3, ): output = str(output, sys.stdout.encoding) output = output.replace('PID', '') p.wait() pids_ps = [] for pid in output.split('\n'): if pid: pids_ps.append(int(pid.strip())) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) # not all systems include pid 0 in their list but psutil does so # we force it if 0 not in pids_ps: pids_ps.append(0) pids_psutil = psutil.get_pid_list() pids_ps.sort() pids_psutil.sort() if pids_ps != pids_psutil: difference = filter(lambda x:x not in pids_ps, pids_psutil) + \ filter(lambda x:x not in pids_psutil, pids_ps) self.fail("difference: " + str(difference))
def test_ctrl_signals(self): p = psutil.Process(get_test_subprocess().pid) p.send_signal(signal.CTRL_C_EVENT) p.send_signal(signal.CTRL_BREAK_EVENT) p.kill() p.wait() self.assertRaises(psutil.NoSuchProcess, p.send_signal, signal.CTRL_C_EVENT) self.assertRaises(psutil.NoSuchProcess, p.send_signal, signal.CTRL_BREAK_EVENT)
def test_memory_maps(self): sproc = get_test_subprocess() time.sleep(1) p = psutil.Process(sproc.pid) maps = p.get_memory_maps(grouped=False) pmap = sh('pmap -x %s' % p.pid).split('\n') del pmap[0]; del pmap[0] # get rid of header while maps and pmap: this = maps.pop(0) other = pmap.pop(0) addr, _, rss, dirty, mode, path = other.split(None, 5) if not path.startswith('[') and not path.endswith(']'): self.assertEqual(path, os.path.basename(this.path)) self.assertEqual(int(rss) * 1024, this.rss) # test only rwx chars, ignore 's' and 'p' self.assertEqual(mode[:3], this.perms[:3])
def test_memory_maps(self): sproc = get_test_subprocess() time.sleep(1) p = psutil.Process(sproc.pid) maps = p.get_memory_maps(grouped=False) pmap = sh('pmap -x %s' % p.pid).split('\n') del pmap[0] del pmap[0] # get rid of header while maps and pmap: this = maps.pop(0) other = pmap.pop(0) addr, _, rss, dirty, mode, path = other.split(None, 5) if not path.startswith('[') and not path.endswith(']'): self.assertEqual(path, os.path.basename(this.path)) self.assertEqual(int(rss) * 1024, this.rss) # test only rwx chars, ignore 's' and 'p' self.assertEqual(mode[:3], this.perms[:3])
def test_get_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime p = get_test_subprocess(["ps", "ax", "-o", "pid"], stdout=subprocess.PIPE) output = p.communicate()[0].strip() if sys.version_info >= (3,): output = str(output, sys.stdout.encoding) output = output.replace('PID', '') p.wait() pids_ps = [] for pid in output.split('\n'): if pid: pids_ps.append(int(pid.strip())) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) pids_psutil = psutil.get_pid_list() pids_ps.sort() pids_psutil.sort() if pids_ps != pids_psutil: difference = [x for x in pids_psutil if x not in pids_ps] + \ [x for x in pids_ps if x not in pids_psutil] self.fail("difference: " + str(difference))
def setUp(self): sproc = get_test_subprocess() wait_for_pid(sproc.pid) self.pid = sproc.pid
def setUpClass(cls): cls.pid = get_test_subprocess([PYTHON, "-E", "-O"], stdin=subprocess.PIPE).pid wait_for_pid(cls.pid)
def test_proc_name(self): from psutil._pswindows import py2_strencode shutil.copyfile(sys.executable, self.uexe) subp = get_test_subprocess(cmd=[self.uexe]) self.assertEqual(py2_strencode(psutil._psplatform.cext.proc_name(subp.pid)), "psutil-è.exe")
def test_proc_cmdline(self): shutil.copyfile(sys.executable, self.uexe) subp = get_test_subprocess(cmd=[self.uexe]) p = psutil.Process(subp.pid) self.assertIsInstance("".join(p.cmdline()), str) self.assertEqual(p.cmdline(), [self.uexe])
def setUp(self): self.pid = get_test_subprocess([PYTHON, "-E", "-O"]).pid
def setUp(self): self.pid = get_test_subprocess([PYTHON, "-E", "-O"], stdin=subprocess.PIPE).pid
def test_proc_exe(self): shutil.copyfile(sys.executable, self.uexe) subp = get_test_subprocess(cmd=[self.uexe]) p = psutil.Process(subp.pid) self.assertIsInstance(p.name(), str) self.assertEqual(os.path.basename(p.name()), "psutil-è.exe")
socks.append(create_socket(socket.AF_INET6, socket.SOCK_STREAM)) socks.append(create_socket(socket.AF_INET6, socket.SOCK_DGRAM)) if hasattr(socket, 'AF_UNIX'): safe_remove(TESTFN) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.bind(TESTFN) s.listen(1) socks.append(s) try: self.execute('get_connections', kind='all') finally: for s in socks: s.close() p = get_test_subprocess() DEAD_PROC = psutil.Process(p.pid) DEAD_PROC.kill() DEAD_PROC.wait() del p class TestProcessObjectLeaksZombie(TestProcessObjectLeaks): """Same as above but looks for leaks occurring when dealing with zombie processes raising NoSuchProcess exception. """ proc = DEAD_PROC if not POSIX: def test_kill(self):
s.listen(1) socks.append(s) kind = 'all' # TODO: UNIX sockets are temporarily implemented by parsing # 'pfiles' cmd output; we don't want that part of the code to # be executed. if SUNOS: kind = 'inet' try: self.execute('connections', kind=kind) finally: for s in socks: s.close() p = get_test_subprocess() DEAD_PROC = psutil.Process(p.pid) DEAD_PROC.kill() DEAD_PROC.wait() del p class TestProcessObjectLeaksZombie(TestProcessObjectLeaks): """Same as above but looks for leaks occurring when dealing with zombie processes raising NoSuchProcess exception. """ proc = DEAD_PROC def call(self, *args, **kwargs): try: TestProcessObjectLeaks.call(self, *args, **kwargs)
def setUpClass(cls): cls.pid = get_test_subprocess().pid
def setUp(self): self.pid = get_test_subprocess().pid
def setUp(self): self.proc32 = get_test_subprocess([self.python32] + self.test_args) self.proc64 = get_test_subprocess([self.python64] + self.test_args)