[pypy-commit] pypy release-pypy2.7-5.x: Win32: fix making sockets non-inheritable

arigo pypy.commits at gmail.com
Wed Nov 9 03:50:48 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: release-pypy2.7-5.x
Changeset: r88254:536a00a9e525
Date: 2016-11-08 20:02 +0100
http://bitbucket.org/pypy/pypy/changeset/536a00a9e525/

Log:	Win32: fix making sockets non-inheritable

diff --git a/rpython/rlib/rposix_stat.py b/rpython/rlib/rposix_stat.py
--- a/rpython/rlib/rposix_stat.py
+++ b/rpython/rlib/rposix_stat.py
@@ -641,6 +641,7 @@
 
     @specialize.arg(0)
     def win32_xstat(traits, path, traverse=False):
+        # XXX 'traverse' is ignored
         win32traits = make_win32_traits(traits)
         with lltype.scoped_alloc(
                 win32traits.WIN32_FILE_ATTRIBUTE_DATA) as data:
diff --git a/rpython/rlib/rsocket.py b/rpython/rlib/rsocket.py
--- a/rpython/rlib/rsocket.py
+++ b/rpython/rlib/rsocket.py
@@ -1062,11 +1062,32 @@
     return result
 make_socket._annspecialcase_ = 'specialize:arg(4)'
 
-def sock_set_inheritable(fd, inheritable):
-    try:
-        rposix.set_inheritable(fd, inheritable)
-    except OSError as e:
-        raise CSocketError(e.errno)
+if _c.WIN32:
+    def sock_set_inheritable(fd, inheritable):
+        handle = rffi.cast(rwin32.HANDLE, fd)
+        try:
+            rwin32.set_handle_inheritable(handle, inheritable)
+        except WindowsError:
+            raise RSocketError("SetHandleInformation failed")   # xxx
+
+    def sock_get_inheritable(fd):
+        handle = rffi.cast(rwin32.HANDLE, fd)
+        try:
+            return rwin32.get_handle_inheritable(handle)
+        except WindowsError:
+            raise RSocketError("GetHandleInformation failed")   # xxx
+else:
+    def sock_set_inheritable(fd, inheritable):
+        try:
+            rposix.set_inheritable(fd, inheritable)
+        except OSError as e:
+            raise CSocketError(e.errno)
+
+    def sock_get_inheritable(fd):
+        try:
+            return rposix.get_inheritable(fd)
+        except OSError as e:
+            raise CSocketError(e.errno)
 
 class SocketError(Exception):
     applevelerrcls = 'error'
diff --git a/rpython/rlib/rwin32.py b/rpython/rlib/rwin32.py
--- a/rpython/rlib/rwin32.py
+++ b/rpython/rlib/rwin32.py
@@ -481,6 +481,10 @@
 
     def set_inheritable(fd, inheritable):
         handle = get_osfhandle(fd)
+        set_handle_inheritable(handle, inheritable)
+
+    def set_handle_inheritable(handle, inheritable):
+        assert lltype.typeOf(handle) is HANDLE
         if inheritable:
             flags = HANDLE_FLAG_INHERIT
         else:
@@ -490,6 +494,10 @@
 
     def get_inheritable(fd):
         handle = get_osfhandle(fd)
+        return get_handle_inheritable(handle)
+
+    def get_handle_inheritable(handle):
+        assert lltype.typeOf(handle) is HANDLE
         pflags = lltype.malloc(LPDWORD.TO, 1, flavor='raw')
         try:
             if not _GetHandleInformation(handle, pflags):
diff --git a/rpython/rlib/test/test_rsocket.py b/rpython/rlib/test/test_rsocket.py
--- a/rpython/rlib/test/test_rsocket.py
+++ b/rpython/rlib/test/test_rsocket.py
@@ -124,8 +124,8 @@
         py.test.skip('No socketpair on Windows')
     for inh in [False, True]:
         s1, s2 = socketpair(inheritable=inh)
-        assert rposix.get_inheritable(s1.fd) == inh
-        assert rposix.get_inheritable(s2.fd) == inh
+        assert sock_get_inheritable(s1.fd) == inh
+        assert sock_get_inheritable(s2.fd) == inh
         s1.close()
         s2.close()
 
@@ -391,7 +391,7 @@
 def test_inheritable():
     for inh in [False, True]:
         s1 = RSocket(inheritable=inh)
-        assert rposix.get_inheritable(s1.fd) == inh
+        assert sock_get_inheritable(s1.fd) == inh
         s1.close()
 
 def test_getaddrinfo_http():


More information about the pypy-commit mailing list