[Python-checkins] r85107 - in python/branches/py3k-issue9978/Lib/test: test_os.py win_console_handler.py
hirokazu.yamamoto
python-checkins at python.org
Wed Sep 29 16:38:02 CEST 2010
Author: hirokazu.yamamoto
Date: Wed Sep 29 16:38:02 2010
New Revision: 85107
Log:
Better implementation for process waiting. Sometimes test hanged.
Modified:
python/branches/py3k-issue9978/Lib/test/test_os.py
python/branches/py3k-issue9978/Lib/test/win_console_handler.py
Modified: python/branches/py3k-issue9978/Lib/test/test_os.py
==============================================================================
--- python/branches/py3k-issue9978/Lib/test/test_os.py (original)
+++ python/branches/py3k-issue9978/Lib/test/test_os.py Wed Sep 29 16:38:02 2010
@@ -13,7 +13,7 @@
import shutil
from test import support
import contextlib
-from win_console_handler import create_mmap
+import mmap
# Detect whether we're on a Linux system that uses the (now outdated
# and unmaintained) linuxthreads threading library. There's an issue
@@ -1030,15 +1030,17 @@
self._kill(100)
def _kill_with_event(self, event, name):
- m = create_mmap()
+ tagname = "python-test#test_os#%d" % os.getpid()
+ m = mmap.mmap(-1, 1, tagname)
m[0] = 0
# Run a script which has console control handling enabled.
proc = subprocess.Popen([sys.executable,
os.path.join(os.path.dirname(__file__),
- "win_console_handler.py")],
+ "win_console_handler.py"), tagname],
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
# Let the interpreter startup before we send signals. See #3137.
while m[0] == 0:
+ self.assertIsNone(proc.poll(), "subprocess died unexpectedly")
time.sleep(0.5)
os.kill(proc.pid, event)
# proc.send_signal(event) could also be done here.
Modified: python/branches/py3k-issue9978/Lib/test/win_console_handler.py
==============================================================================
--- python/branches/py3k-issue9978/Lib/test/win_console_handler.py (original)
+++ python/branches/py3k-issue9978/Lib/test/win_console_handler.py Wed Sep 29 16:38:02 2010
@@ -8,44 +8,40 @@
similar example in C.
"""
+from ctypes import wintypes
+import signal
+import ctypes
import mmap
+import sys
-def create_mmap():
- return mmap.mmap(-1, 1, "Python/Lib/test/win_console_handler.py")
+# Function prototype for the handler function. Returns BOOL, takes a DWORD.
+HandlerRoutine = wintypes.WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD)
-if __name__ == "__main__":
- from ctypes import wintypes
- import signal
- import ctypes
-
- # Function prototype for the handler function. Returns BOOL, takes a DWORD.
- HandlerRoutine = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD)
-
- def _ctrl_handler(sig):
- """Handle a sig event and return 0 to terminate the process"""
- if sig == signal.CTRL_C_EVENT:
- pass
- elif sig == signal.CTRL_BREAK_EVENT:
- pass
- else:
- print("UNKNOWN EVENT")
- return 0
-
- ctrl_handler = HandlerRoutine(_ctrl_handler)
-
-
- SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
- SetConsoleCtrlHandler.argtypes = (HandlerRoutine, wintypes.BOOL)
- SetConsoleCtrlHandler.restype = wintypes.BOOL
+def _ctrl_handler(sig):
+ """Handle a sig event and return 0 to terminate the process"""
+ if sig == signal.CTRL_C_EVENT:
+ pass
+ elif sig == signal.CTRL_BREAK_EVENT:
+ pass
+ else:
+ print("UNKNOWN EVENT")
+ return 0
+ctrl_handler = HandlerRoutine(_ctrl_handler)
+
+SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
+SetConsoleCtrlHandler.argtypes = (HandlerRoutine, wintypes.BOOL)
+SetConsoleCtrlHandler.restype = wintypes.BOOL
+
+if __name__ == "__main__":
# Add our console control handling function with value 1
if not SetConsoleCtrlHandler(ctrl_handler, 1):
print("Unable to add SetConsoleCtrlHandler")
exit(-1)
- # Awake waiting main process
- m = create_mmap()
+ # Awake main process
+ m = mmap.mmap(-1, 1, sys.argv[1])
m[0] = 1
# Do nothing but wait for the signal
More information about the Python-checkins
mailing list