def test_raises_not_implemented_for_other_wait_result(self): with patch.object(k32process, "WaitForSingleObject", return_value=42): process = \ self.create_python_process("import time; time.sleep(5)") with self.assertRaises(PyWinCFFINotImplementedError): self.assertTrue(pid_exists(process.pid))
def test_raises_not_implemented_for_other_wait_result(self): with patch.object(k32process, "WaitForSingleObject", return_value=42): process = \ self.create_python_process("import time; time.sleep(5)") with self.assertRaises(PyWinCFFINotImplementedError): self.assertTrue(pid_exists(process.pid))
def test_calls_closehandle(self): with patch.object(k32process, "CloseHandle") as mocked: process = \ self.create_python_process("import time; time.sleep(5)") self.assertTrue(pid_exists(process.pid)) self.assertEqual(mocked.call_count, 1)
def test_calls_closehandle(self): with patch.object(k32process, "CloseHandle") as mocked: process = \ self.create_python_process("import time; time.sleep(5)") self.assertTrue(pid_exists(process.pid)) self.assertEqual(mocked.call_count, 1)
def test_returns_true_if_access_is_denied(self): # This will always test for ERROR_ACCESS_DENIED by forcing OpenProcess # to not request any permissions def open_process(_, bInheritHandle, dwProcessId): return OpenProcess(0, bInheritHandle, dwProcessId) process = self.create_python_process("import time; time.sleep(5)") with patch.object(k32process, "OpenProcess", open_process): self.assertTrue(pid_exists(process.pid))
def test_process_never_existed(self): # OpenProcess *might* work even when the process # is no longer alive which is why pid_exists() checks # for an exit code. For cases where the process # never should have existed however we should # expect to get False from pid_exists(). Here # we're attempting to do this with something # that should probably never exist. self.assertFalse(pid_exists(0xFFFFFFFC))
def test_process_never_existed(self): # OpenProcess *might* work even when the process # is no longer alive which is why pid_exists() checks # for an exit code. For cases where the process # never should have existed however we should # expect to get False from pid_exists(). Here # we're attempting to do this with something # that should probably never exist. self.assertFalse(pid_exists(0xFFFFFFFC))
def test_returns_true_if_access_is_denied(self): # This will always test for ERROR_ACCESS_DENIED by forcing OpenProcess # to not request any permissions def open_process(_, bInheritHandle, dwProcessId): return OpenProcess(0, bInheritHandle, dwProcessId) process = self.create_python_process("import time; time.sleep(5)") with patch.object(k32process, "OpenProcess", open_process): self.assertTrue(pid_exists(process.pid))
def test_raises_unhandled_windows_api_error(self): def new_open_process(*args, **kwargs): raise WindowsAPIError("", "", 42) with patch.object(k32process, "OpenProcess", new_open_process): process = \ self.create_python_process("import time; time.sleep(5)") with self.assertRaises(WindowsAPIError): self.assertTrue(pid_exists(process.pid))
def test_raises_unhandled_windows_api_error(self): def new_open_process(*args, **kwargs): raise WindowsAPIError("", "", 42) with patch.object(k32process, "OpenProcess", new_open_process): process = \ self.create_python_process("import time; time.sleep(5)") with self.assertRaises(WindowsAPIError): self.assertTrue(pid_exists(process.pid))
def test_raises_not_implemented_for_wait_abandoned(self): _, library = dist.load() with patch.object(k32process, "WaitForSingleObject", return_value=library.WAIT_ABANDONED): process = \ self.create_python_process("import time; time.sleep(5)") with self.assertRaises(PyWinCFFINotImplementedError): self.assertTrue(pid_exists(process.pid))
def test_raises_not_implemented_for_wait_abandoned(self): _, library = dist.load() with patch.object( k32process, "WaitForSingleObject", return_value=library.WAIT_ABANDONED ): process = \ self.create_python_process("import time; time.sleep(5)") with self.assertRaises(PyWinCFFINotImplementedError): self.assertTrue(pid_exists(process.pid))
def test_environment_unicode(self): fd, output_file = tempfile.mkstemp(suffix=".txt") os.close(fd) self.addCleanup(os.remove, output_file) script = dedent(""" import os with open(os.environ["OUTPUT_FILE"], "w") as file_: file_.write(os.environ["UNICODE_OUTPUT"]) """).strip() fd, script_path = tempfile.mkstemp(suffix=".py") self.addCleanup(os.remove, script_path) with os.fdopen(fd, "w") as file_: file_.write(script) environ = { u"OUTPUT_FILE": text_type(output_file), u"UNICODE_OUTPUT": u"µ", u"PATH": u"", u"SYSTEMROOT": text_type(os.environ.get("SYSTEMROOT")) } process = CreateProcess( lpCommandLine=u"{0} \"{1}\"".format(sys.executable, script_path), lpApplicationName=text_type(sys.executable), lpProcessAttributes=None, lpThreadAttributes=None, bInheritHandles=True, dwCreationFlags=None, lpEnvironment=environ, lpCurrentDirectory=None, lpStartupInfo=None) self.addCleanup(self.cleanup_process, process) while pid_exists(process.lpProcessInformation.dwProcessId): time.sleep(.1) self.assertTrue(isfile(output_file)) with open(output_file) as file_: content = file_.read() if isinstance(content, text_type): self.assertEqual(content, u"µ") else: self.assertEqual(content, b"\xb5")
def test_environment_ascii(self): fd, remove_file = tempfile.mkstemp(suffix=".txt") os.close(fd) script = dedent(""" import os os.remove(os.environ["REMOVE_FILE"]) """).strip() fd, script_path = tempfile.mkstemp(suffix=".py") with os.fdopen(fd, "w") as file_: file_.write(script) self.addCleanup(os.remove, script_path) environ = { text_type("REMOVE_FILE"): text_type(remove_file), text_type("PATH"): text_type(""), text_type("SYSTEMROOT"): text_type(os.environ.get("SYSTEMROOT")) } process = CreateProcess( text_type("%s \"%s\"" % (sys.executable, script_path)), lpApplicationName=None, lpProcessAttributes=None, lpThreadAttributes=None, bInheritHandles=True, dwCreationFlags=None, lpEnvironment=environ, lpCurrentDirectory=None, lpStartupInfo=None ) self.addCleanup(self.cleanup_process, process) while pid_exists(process.lpProcessInformation.dwProcessId): time.sleep(.1) self.assertFalse(isfile(remove_file))
def test_environment_ascii(self): fd, remove_file = tempfile.mkstemp(suffix=".txt") os.close(fd) script = dedent(""" import os os.remove(os.environ["REMOVE_FILE"]) """).strip() fd, script_path = tempfile.mkstemp(suffix=".py") with os.fdopen(fd, "w") as file_: file_.write(script) self.addCleanup(os.remove, script_path) environ = { u"REMOVE_FILE": text_type(remove_file), u"PATH": u"", u"SYSTEMROOT": text_type(os.environ.get("SYSTEMROOT")) } process = CreateProcess( lpCommandLine=u"{0} \"{1}\"".format(sys.executable, script_path), lpApplicationName=text_type(sys.executable), lpProcessAttributes=None, lpThreadAttributes=None, bInheritHandles=True, dwCreationFlags=None, lpEnvironment=environ, lpCurrentDirectory=None, lpStartupInfo=None ) self.addCleanup(self.cleanup_process, process) while pid_exists(process.lpProcessInformation.dwProcessId): time.sleep(.1) self.assertFalse(isfile(remove_file))
def test_working_directory(self): tmpdir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, tmpdir) fd, remove_file = tempfile.mkstemp(dir=tmpdir, suffix=".txt") os.close(fd) process = CreateProcess(text_type("cmd.exe /c del %s" % basename(remove_file)), lpApplicationName=None, lpProcessAttributes=None, lpThreadAttributes=None, bInheritHandles=True, dwCreationFlags=None, lpEnvironment=None, lpCurrentDirectory=text_type(tmpdir), lpStartupInfo=None) self.addCleanup(self.cleanup_process, process) while pid_exists(process.lpProcessInformation.dwProcessId): time.sleep(.1) self.assertFalse(isfile(remove_file))
def test_working_directory(self): tmpdir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, tmpdir) fd, remove_file = tempfile.mkstemp(dir=tmpdir, suffix=".txt") os.close(fd) process = CreateProcess( text_type("cmd.exe /c del %s" % basename(remove_file)), lpApplicationName=None, lpProcessAttributes=None, lpThreadAttributes=None, bInheritHandles=True, dwCreationFlags=None, lpEnvironment=None, lpCurrentDirectory=text_type(tmpdir), lpStartupInfo=None ) self.addCleanup(self.cleanup_process, process) while pid_exists(process.lpProcessInformation.dwProcessId): time.sleep(.1) self.assertFalse(isfile(remove_file))
def test_reserved_pids_always_return_true(self): for pid in k32process.RESERVED_PIDS: self.assertTrue(pid_exists(pid))
def test_returns_false_for_process_with_exit_code_259(self): _, library = dist.load() process = self.create_python_process( "import sys; sys.exit(%d)" % library.STILL_ACTIVE) process.communicate() self.assertFalse(pid_exists(process.pid))
def test_process_dies_while_waiting(self): # This condition should be very rare because of what the default # wait is set to but we check it anyway just in case. _, library = dist.load() process = self.create_python_process("import time; time.sleep(1)") self.assertFalse(pid_exists(process.pid, wait=library.INFINITE))
def test_running_process(self): process = self.create_python_process("import time; time.sleep(5)") self.assertTrue( pid_exists(process.pid))
def test_running_process(self): process = self.create_python_process("import time; time.sleep(5)") self.assertTrue( pid_exists(process.pid))
def test_process_dies_while_waiting(self): # This condition should be very rare because of what the default # wait is set to but we check it anyway just in case. _, library = dist.load() process = self.create_python_process("import time; time.sleep(1)") self.assertFalse(pid_exists(process.pid, wait=library.INFINITE))
def test_reserved_pids_always_return_true(self): for pid in k32process.RESERVED_PIDS: self.assertTrue(pid_exists(pid))
def test_returns_false_for_process_with_exit_code_259(self): _, library = dist.load() process = self.create_python_process( "import sys; sys.exit(%d)" % library.STILL_ACTIVE) process.communicate() self.assertFalse(pid_exists(process.pid))