[PATCH 2 of 3] win32: add a method to set the state of a named pipe

Matt Harbison mharbison72 at gmail.com
Mon Apr 6 21:49:01 CDT 2015


# HG changeset patch
# User Matt Harbison <matt_harbison at yahoo.com>
# Date 1428372757 14400
#      Mon Apr 06 22:12:37 2015 -0400
# Node ID 5ad3c1f7fa75a06307f430ee51c19d85e6c78d14
# Parent  a2904b4de0c3852644c3021901739da23e41e543
win32: add a method to set the state of a named pipe

This will be used in an upcoming patch to do nonblocking reads from the child
process, like on posix platforms.

diff --git a/mercurial/win32.py b/mercurial/win32.py
--- a/mercurial/win32.py
+++ b/mercurial/win32.py
@@ -178,6 +178,10 @@
     ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, _LPSTR, _DWORD]
 _kernel32.GetNamedPipeHandleStateA.restype = _BOOL
 
+_kernel32.SetNamedPipeHandleState.argtypes = [_HANDLE, ctypes.c_void_p,
+    ctypes.c_void_p, ctypes.c_void_p]
+_kernel32.SetNamedPipeHandleState.restype = _BOOL
+
 _kernel32.CreateProcessA.argtypes = [_LPCSTR, _LPCSTR, ctypes.c_void_p,
     ctypes.c_void_p, _BOOL, _DWORD, ctypes.c_void_p, _LPCSTR, ctypes.c_void_p,
     ctypes.c_void_p]
@@ -252,6 +256,16 @@
 
     return state.value
 
+def setpipestate(pipe, state):
+    handle = msvcrt.get_osfhandle(pipe.fileno())
+    if handle == -1:
+        raise ctypes.WinError()
+
+    status = _DWORD(state)
+    if not _kernel32.SetNamedPipeHandleState(handle, ctypes.byref(status), None,
+                                             None):
+        raise ctypes.WinError()
+
 def oslink(src, dst):
     try:
         if not _kernel32.CreateHardLinkA(dst, src, None):


More information about the Mercurial-devel mailing list