Author: Armin Rigo <[email protected]>
Branch: py3.5-noninherit
Changeset: r86582:e6181605e018
Date: 2016-08-26 19:27 +0200
http://bitbucket.org/pypy/pypy/changeset/e6181605e018/

Log:    _socket.dup(). and _socket.fromfd()... which no longer exists at
        interp-level, easy

diff --git a/pypy/module/_socket/interp_func.py 
b/pypy/module/_socket/interp_func.py
--- a/pypy/module/_socket/interp_func.py
+++ b/pypy/module/_socket/interp_func.py
@@ -143,24 +143,11 @@
 @unwrap_spec(fd=int)
 def dup(space, fd):
     try:
-        newfd = rsocket.dup(fd)
+        newfd = rsocket.dup(fd, inheritable=False)
     except SocketError as e:
         raise converted_error(space, e)
     return space.wrap(newfd)
 
-@unwrap_spec(fd=int, family=int, type=int, proto=int)
-def fromfd(space, fd, family, type, proto=0):
-    """fromfd(fd, family, type[, proto]) -> socket object
-
-    Create a socket object from the given file descriptor.
-    The remaining arguments are the same as for socket().
-    """
-    try:
-        sock = rsocket.fromfd(fd, family, type, proto)
-    except SocketError as e:
-        raise converted_error(space, e)
-    return space.wrap(W_Socket(space, sock))
-
 @unwrap_spec(family=int, type=int, proto=int)
 def socketpair(space, family=rsocket.socketpair_default_family,
                       type  =rsocket.SOCK_STREAM,
diff --git a/pypy/module/_socket/test/test_sock_app.py 
b/pypy/module/_socket/test/test_sock_app.py
--- a/pypy/module/_socket/test/test_sock_app.py
+++ b/pypy/module/_socket/test/test_sock_app.py
@@ -546,11 +546,15 @@
         s.ioctl(_socket.SIO_KEEPALIVE_VALS, (1, 100, 100))
 
     def test_dup(self):
-        import _socket as socket
+        import _socket as socket, posix
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         s.bind(('localhost', 0))
         fd = socket.dup(s.fileno())
         assert s.fileno() != fd
+        assert posix.get_inheritable(s.fileno()) is False
+        assert posix.get_inheritable(fd) is False
+        posix.close(fd)
+        s.close()
 
     def test_dup_error(self):
         import _socket
diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -372,13 +372,17 @@
         raise OSError(get_saved_errno(), '%s failed' % name)
     return result
 
-@replace_os_function('dup')
-def dup(fd, inheritable=True):
+def _dup(fd, inheritable=True):
     validate_fd(fd)
     if inheritable:
         res = c_dup(fd)
     else:
         res = c_dup_noninheritable(fd)
+    return res
+
+@replace_os_function('dup')
+def dup(fd, inheritable=True):
+    res = _dup(fd, inheritable)
     return handle_posix_error('dup', res)
 
 @replace_os_function('dup2')
diff --git a/rpython/rlib/rsocket.py b/rpython/rlib/rsocket.py
--- a/rpython/rlib/rsocket.py
+++ b/rpython/rlib/rsocket.py
@@ -1169,7 +1169,7 @@
                 make_socket(fd1, family, type, proto, SocketClass))
 
 if _c.WIN32:
-    def dup(fd):
+    def dup(fd, inheritable=True):
         with lltype.scoped_alloc(_c.WSAPROTOCOL_INFO, zero=True) as info:
             if _c.WSADuplicateSocket(fd, rwin32.GetCurrentProcessId(), info):
                 raise last_error()
@@ -1180,15 +1180,16 @@
                 raise last_error()
             return result
 else:
-    def dup(fd):
-        fd = _c.dup(fd)
+    def dup(fd, inheritable=True):
+        fd = rposix._dup(fd, inheritable)
         if fd < 0:
             raise last_error()
         return fd
 
-def fromfd(fd, family, type, proto=0, SocketClass=RSocket):
+def fromfd(fd, family, type, proto=0, SocketClass=RSocket, inheritable=True):
     # Dup the fd so it and the socket can be closed independently
-    return make_socket(dup(fd), family, type, proto, SocketClass)
+    fd = dup(fd, inheritable=inheritable)
+    return make_socket(fd, family, type, proto, SocketClass)
 
 def getdefaulttimeout():
     return defaults.timeout
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to