Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions
test.test_os.Win32KillTests Class Reference

List of all members.

Public Member Functions

def test_kill_sigterm
def test_kill_int
def test_CTRL_C_EVENT
def test_CTRL_BREAK_EVENT

Private Member Functions

def _kill
def _kill_with_event

Detailed Description

Definition at line 955 of file test_os.py.


Member Function Documentation

def test.test_os.Win32KillTests._kill (   self,
  sig 
) [private]

Definition at line 956 of file test_os.py.

00956 
00957     def _kill(self, sig):
00958         # Start sys.executable as a subprocess and communicate from the
00959         # subprocess to the parent that the interpreter is ready. When it
00960         # becomes ready, send *sig* via os.kill to the subprocess and check
00961         # that the return code is equal to *sig*.
00962         import ctypes
00963         from ctypes import wintypes
00964         import msvcrt
00965 
00966         # Since we can't access the contents of the process' stdout until the
00967         # process has exited, use PeekNamedPipe to see what's inside stdout
00968         # without waiting. This is done so we can tell that the interpreter
00969         # is started and running at a point where it could handle a signal.
00970         PeekNamedPipe = ctypes.windll.kernel32.PeekNamedPipe
00971         PeekNamedPipe.restype = wintypes.BOOL
00972         PeekNamedPipe.argtypes = (wintypes.HANDLE, # Pipe handle
00973                                   ctypes.POINTER(ctypes.c_char), # stdout buf
00974                                   wintypes.DWORD, # Buffer size
00975                                   ctypes.POINTER(wintypes.DWORD), # bytes read
00976                                   ctypes.POINTER(wintypes.DWORD), # bytes avail
00977                                   ctypes.POINTER(wintypes.DWORD)) # bytes left
00978         msg = "running"
00979         proc = subprocess.Popen([sys.executable, "-c",
00980                                  "import sys;"
00981                                  "sys.stdout.write('{}');"
00982                                  "sys.stdout.flush();"
00983                                  "input()".format(msg)],
00984                                 stdout=subprocess.PIPE,
00985                                 stderr=subprocess.PIPE,
00986                                 stdin=subprocess.PIPE)
00987         self.addCleanup(proc.stdout.close)
00988         self.addCleanup(proc.stderr.close)
00989         self.addCleanup(proc.stdin.close)
00990 
00991         count, max = 0, 100
00992         while count < max and proc.poll() is None:
00993             # Create a string buffer to store the result of stdout from the pipe
00994             buf = ctypes.create_string_buffer(len(msg))
00995             # Obtain the text currently in proc.stdout
00996             # Bytes read/avail/left are left as NULL and unused
00997             rslt = PeekNamedPipe(msvcrt.get_osfhandle(proc.stdout.fileno()),
00998                                  buf, ctypes.sizeof(buf), None, None, None)
00999             self.assertNotEqual(rslt, 0, "PeekNamedPipe failed")
01000             if buf.value:
01001                 self.assertEqual(msg, buf.value.decode())
01002                 break
01003             time.sleep(0.1)
01004             count += 1
01005         else:
01006             self.fail("Did not receive communication from the subprocess")
01007 
01008         os.kill(proc.pid, sig)
01009         self.assertEqual(proc.wait(), sig)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_os.Win32KillTests._kill_with_event (   self,
  event,
  name 
) [private]

Definition at line 1018 of file test_os.py.

01018 
01019     def _kill_with_event(self, event, name):
01020         tagname = "test_os_%s" % uuid.uuid1()
01021         m = mmap.mmap(-1, 1, tagname)
01022         m[0] = 0
01023         # Run a script which has console control handling enabled.
01024         proc = subprocess.Popen([sys.executable,
01025                    os.path.join(os.path.dirname(__file__),
01026                                 "win_console_handler.py"), tagname],
01027                    creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
01028         # Let the interpreter startup before we send signals. See #3137.
01029         count, max = 0, 100
01030         while count < max and proc.poll() is None:
01031             if m[0] == 1:
01032                 break
01033             time.sleep(0.1)
01034             count += 1
01035         else:
01036             # Forcefully kill the process if we weren't able to signal it.
01037             os.kill(proc.pid, signal.SIGINT)
01038             self.fail("Subprocess didn't finish initialization")
01039         os.kill(proc.pid, event)
01040         # proc.send_signal(event) could also be done here.
01041         # Allow time for the signal to be passed and the process to exit.
01042         time.sleep(0.5)
01043         if not proc.poll():
01044             # Forcefully kill the process if we weren't able to signal it.
01045             os.kill(proc.pid, signal.SIGINT)
01046             self.fail("subprocess did not stop on {}".format(name))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1066 of file test_os.py.

01066 
01067     def test_CTRL_BREAK_EVENT(self):
01068         self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT")
01069 
01070 
01071 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
@support.skip_unless_symlink

Here is the call graph for this function:

Definition at line 1048 of file test_os.py.

01048 
01049     def test_CTRL_C_EVENT(self):
01050         from ctypes import wintypes
01051         import ctypes
01052 
01053         # Make a NULL value by creating a pointer with no argument.
01054         NULL = ctypes.POINTER(ctypes.c_int)()
01055         SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
01056         SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int),
01057                                           wintypes.BOOL)
01058         SetConsoleCtrlHandler.restype = wintypes.BOOL
01059 
01060         # Calling this with NULL and FALSE causes the calling process to
01061         # handle CTRL+C, rather than ignore it. This property is inherited
01062         # by subprocesses.
01063         SetConsoleCtrlHandler(NULL, 0)
01064 
01065         self._kill_with_event(signal.CTRL_C_EVENT, "CTRL_C_EVENT")

Here is the call graph for this function:

Definition at line 1014 of file test_os.py.

01014 
01015     def test_kill_int(self):
01016         # os.kill on Windows can take an int which gets set as the exit code
01017         self._kill(100)

Here is the call graph for this function:

Definition at line 1010 of file test_os.py.

01010 
01011     def test_kill_sigterm(self):
01012         # SIGTERM doesn't mean anything special, but make sure it works
01013         self._kill(signal.SIGTERM)

Here is the call graph for this function:


The documentation for this class was generated from the following file: