[pypy-svn] r55000 - pypy/branch/win32port/pypy/lib
afa at codespeak.net
afa at codespeak.net
Tue May 20 16:04:24 CEST 2008
Author: afa
Date: Tue May 20 16:04:20 2008
New Revision: 55000
Modified:
pypy/branch/win32port/pypy/lib/_subprocess.py
Log:
Refactor _subprocess.py
Modified: pypy/branch/win32port/pypy/lib/_subprocess.py
==============================================================================
--- pypy/branch/win32port/pypy/lib/_subprocess.py (original)
+++ pypy/branch/win32port/pypy/lib/_subprocess.py Tue May 20 16:04:20 2008
@@ -1,4 +1,85 @@
-import ctypes as _ctypes
+"""
+Support routines for subprocess module.
+Currently, this extension module is only required when using the
+subprocess module on Windows.
+"""
+
+
+# Declare external Win32 functions
+
+import ctypes
+
+_kernel32 = ctypes.WinDLL('kernel32')
+
+_CloseHandle = _kernel32.CloseHandle
+_CloseHandle.argtypes = [ctypes.c_int]
+_CloseHandle.restype = ctypes.c_int
+
+_CreatePipe = _kernel32.CreatePipe
+_CreatePipe.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int),
+ ctypes.c_void_p, ctypes.c_int]
+_CreatePipe.restype = ctypes.c_int
+
+_GetCurrentProcess = _kernel32.GetCurrentProcess
+_GetCurrentProcess.argtypes = []
+_GetCurrentProcess.restype = ctypes.c_int
+
+_DuplicateHandle = _kernel32.DuplicateHandle
+_DuplicateHandle.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int,
+ ctypes.POINTER(ctypes.c_int),
+ ctypes.c_int, ctypes.c_int, ctypes.c_int]
+_DuplicateHandle.restype = ctypes.c_int
+
+_WaitForSingleObject = _kernel32.WaitForSingleObject
+_WaitForSingleObject.argtypes = [ctypes.c_int, ctypes.c_int]
+_WaitForSingleObject.restype = ctypes.c_int
+
+_GetExitCodeProcess = _kernel32.GetExitCodeProcess
+_GetExitCodeProcess.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_int)]
+_GetExitCodeProcess.restype = ctypes.c_int
+
+_GetStdHandle = _kernel32.GetStdHandle
+_GetStdHandle.argtypes = [ctypes.c_int]
+_GetStdHandle.restype = ctypes.c_int
+
+class _STARTUPINFO(ctypes.Structure):
+ _fields_ = [('cb', ctypes.c_int),
+ ('lpReserved', ctypes.c_void_p),
+ ('lpDesktop', ctypes.c_char_p),
+ ('lpTitle', ctypes.c_char_p),
+ ('dwX', ctypes.c_int),
+ ('dwY', ctypes.c_int),
+ ('dwXSize', ctypes.c_int),
+ ('dwYSize', ctypes.c_int),
+ ('dwXCountChars', ctypes.c_int),
+ ('dwYCountChars', ctypes.c_int),
+ ("dwFillAttribute", ctypes.c_int),
+ ("dwFlags", ctypes.c_int),
+ ("wShowWindow", ctypes.c_short),
+ ("cbReserved2", ctypes.c_short),
+ ("lpReserved2", ctypes.c_void_p),
+ ("hStdInput", ctypes.c_int),
+ ("hStdOutput", ctypes.c_int),
+ ("hStdError", ctypes.c_int)
+ ]
+
+class _PROCESS_INFORMATION(ctypes.Structure):
+ _fields_ = [("hProcess", ctypes.c_int),
+ ("hThread", ctypes.c_int),
+ ("dwProcessID", ctypes.c_int),
+ ("dwThreadID", ctypes.c_int)]
+
+_CreateProcess = _kernel32.CreateProcessA
+_CreateProcess.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_void_p, ctypes.c_void_p,
+ ctypes.c_int, ctypes.c_int, ctypes.c_char_p, ctypes.c_char_p,
+ ctypes.POINTER(_STARTUPINFO), ctypes.POINTER(_PROCESS_INFORMATION)]
+_CreateProcess.restype = ctypes.c_int
+
+del ctypes
+
+# Now the _subprocess module implementation
+
+from ctypes import c_int as _c_int, byref as _byref
class _handle:
def __init__(self, handle):
@@ -12,24 +93,15 @@
return handle
def Close(self):
- _CloseHandle = _ctypes.WinDLL('kernel32').CloseHandle
- _CloseHandle.argtypes = [_ctypes.c_int]
- _CloseHandle.restype = _ctypes.c_int
-
if self.handle not in (-1, None):
_CloseHandle(self.handle)
self.handle = None
def CreatePipe(attributes, size):
- _CreatePipe = _ctypes.WinDLL('kernel32').CreatePipe
- _CreatePipe.argtypes = [_ctypes.POINTER(_ctypes.c_int), _ctypes.POINTER(_ctypes.c_int),
- _ctypes.c_void_p, _ctypes.c_int]
- _CreatePipe.restype = _ctypes.c_int
+ read = _c_int()
+ write = _c_int()
- read = _ctypes.c_int()
- write = _ctypes.c_int()
-
- res = _CreatePipe(_ctypes.byref(read), _ctypes.byref(write), None, size)
+ res = _CreatePipe(_byref(read), _byref(write), None, size)
if not res:
raise WindowsError("Error")
@@ -37,24 +109,14 @@
return _handle(read.value), _handle(write.value)
def GetCurrentProcess():
- _GetCurrentProcess = _ctypes.WinDLL('kernel32').GetCurrentProcess
- _GetCurrentProcess.argtypes = []
- _GetCurrentProcess.restype = _ctypes.c_int
-
return _handle(_GetCurrentProcess())
def DuplicateHandle(source_process, source, target_process, access, inherit, options=0):
- _DuplicateHandle = _ctypes.WinDLL('kernel32').DuplicateHandle
- _DuplicateHandle.argtypes = [_ctypes.c_int, _ctypes.c_int, _ctypes.c_int,
- _ctypes.POINTER(_ctypes.c_int),
- _ctypes.c_int, _ctypes.c_int, _ctypes.c_int]
- _DuplicateHandle.restype = _ctypes.c_int
-
- target = _ctypes.c_int()
+ target = _c_int()
res = _DuplicateHandle(int(source_process), int(source), int(target_process),
- _ctypes.byref(target),
+ _byref(target),
access, inherit, options)
if not res:
@@ -66,40 +128,7 @@
def CreateProcess(name, command_line, process_attr, thread_attr,
inherit, flags, env, start_dir, startup_info):
- _CreateProcess = _ctypes.WinDLL('kernel32').CreateProcessA
- _CreateProcess.argtypes = [_ctypes.c_char_p, _ctypes.c_char_p, _ctypes.c_void_p, _ctypes.c_void_p,
- _ctypes.c_int, _ctypes.c_int, _ctypes.c_char_p, _ctypes.c_char_p,
- _ctypes.c_void_p, _ctypes.c_void_p]
- _CreateProcess.restype = _ctypes.c_int
-
- class STARTUPINFO(_ctypes.Structure):
- _fields_ = [('cb', _ctypes.c_int),
- ('lpReserved', _ctypes.c_void_p),
- ('lpDesktop', _ctypes.c_char_p),
- ('lpTitle', _ctypes.c_char_p),
- ('dwX', _ctypes.c_int),
- ('dwY', _ctypes.c_int),
- ('dwXSize', _ctypes.c_int),
- ('dwYSize', _ctypes.c_int),
- ('dwXCountChars', _ctypes.c_int),
- ('dwYCountChars', _ctypes.c_int),
- ("dwFillAttribute", _ctypes.c_int),
- ("dwFlags", _ctypes.c_int),
- ("wShowWindow", _ctypes.c_short),
- ("cbReserved2", _ctypes.c_short),
- ("lpReserved2", _ctypes.c_void_p),
- ("hStdInput", _ctypes.c_int),
- ("hStdOutput", _ctypes.c_int),
- ("hStdError", _ctypes.c_int)
- ]
-
- class PROCESS_INFORMATION(_ctypes.Structure):
- _fields_ = [("hProcess", _ctypes.c_int),
- ("hThread", _ctypes.c_int),
- ("dwProcessID", _ctypes.c_int),
- ("dwThreadID", _ctypes.c_int)]
-
- si = STARTUPINFO()
+ si = _STARTUPINFO()
si.dwFlags = startup_info.dwFlags
si.wShowWindow = getattr(startup_info, 'wShowWindow', 0)
if startup_info.hStdInput:
@@ -109,7 +138,7 @@
if startup_info.hStdError:
si.hStdError = startup_info.hStdError.handle
- pi = PROCESS_INFORMATION()
+ pi = _PROCESS_INFORMATION()
if env is not None:
envbuf = ""
@@ -120,7 +149,7 @@
envbuf = None
res = _CreateProcess(name, command_line, None, None, inherit, flags, envbuf,
- start_dir, _ctypes.byref(si), _ctypes.byref(pi))
+ start_dir, _byref(si), _byref(pi))
if not res:
raise WindowsError("Error")
@@ -129,10 +158,6 @@
STARTF_USESTDHANDLES = 0x100
def WaitForSingleObject(handle, milliseconds):
- _WaitForSingleObject = _ctypes.WinDLL('kernel32').WaitForSingleObject
- _WaitForSingleObject.argtypes = [_ctypes.c_int, _ctypes.c_int]
- _WaitForSingleObject.restype = _ctypes.c_int
-
res = _WaitForSingleObject(handle.handle, milliseconds)
if res < 0:
@@ -143,13 +168,9 @@
WAIT_OBJECT_0 = 0
def GetExitCodeProcess(handle):
- _GetExitCodeProcess = _ctypes.WinDLL('kernel32').GetExitCodeProcess
- _GetExitCodeProcess.argtypes = [_ctypes.c_int, _ctypes.POINTER(_ctypes.c_int)]
- _GetExitCodeProcess.restype = _ctypes.c_int
-
- code = _ctypes.c_int()
+ code = _c_int()
- res = _GetExitCodeProcess(handle.handle, _ctypes.byref(code))
+ res = _GetExitCodeProcess(handle.handle, _byref(code))
if not res:
raise WindowsError("Error")
@@ -157,10 +178,6 @@
return code.value
def GetStdHandle(stdhandle):
- _GetStdHandle = _ctypes.WinDLL('kernel32').GetStdHandle
- _GetStdHandle.argtypes = [_ctypes.c_int]
- _GetStdHandle.restype = _ctypes.c_int
-
res = _GetStdHandle(stdhandle)
if not res:
More information about the Pypy-commit
mailing list