Author: Brian Kearns <[email protected]>
Branch: 
Changeset: r57367:1d978ce2664a
Date: 2012-09-16 21:32 -0400
http://bitbucket.org/pypy/pypy/changeset/1d978ce2664a/

Log:    rsocket connect() with timeout: use getsockopt() to get result
        rather than calling connect() a second time, enhance tests (inspired
        by cpython changeset 44789:3c9ccc32da9a)

diff --git a/pypy/rlib/rsocket.py b/pypy/rlib/rsocket.py
--- a/pypy/rlib/rsocket.py
+++ b/pypy/rlib/rsocket.py
@@ -810,23 +810,19 @@
             errno = _c.geterrno()
             if self.timeout > 0.0 and res < 0 and errno == _c.EINPROGRESS:
                 timeout = self._select(True)
-                errno = _c.geterrno()
                 if timeout == 0:
-                    addr = address.lock()
-                    res = _c.socketconnect(self.fd, addr, address.addrlen)
-                    address.unlock()
-                    if res < 0:
-                        errno = _c.geterrno()
-                        if errno == _c.EISCONN:
-                            res = 0
+                    res = self.getsockopt_int(_c.SOL_SOCKET, _c.SO_ERROR)
+                    if (res == _c.EISCONN):
+                        res = 0
+                    errno = res
                 elif timeout == -1:
-                    return (errno, False)
+                    return (_c.geterrno(), False)
                 else:
                     return (_c.EWOULDBLOCK, True)
 
-            if res == 0:
-                errno = 0
-            return (errno, False)
+            if res < 0:
+                res = errno
+            return (res, False)
         
     def connect(self, address):
         """Connect the socket to a remote address."""
diff --git a/pypy/rlib/test/test_rsocket.py b/pypy/rlib/test/test_rsocket.py
--- a/pypy/rlib/test/test_rsocket.py
+++ b/pypy/rlib/test/test_rsocket.py
@@ -159,6 +159,7 @@
     assert addr.eq(sock.getsockname())
     sock.listen(1)
     s2 = RSocket(AF_INET, SOCK_STREAM)
+    s2.settimeout(10.0) # test one side with timeouts so select is used, 
shouldn't affect test
     def connecting():
         s2.connect(addr)
         lock.release()
@@ -209,6 +210,7 @@
     addr = INETAddress('127.0.0.1', port)
     assert addr.eq(s1.getsockname())
     s2 = RSocket(AF_INET, SOCK_DGRAM)
+    s2.settimeout(10.0) # test one side with timeouts so select is used, 
shouldn't affect test
     s2.bind(INETAddress('127.0.0.1', INADDR_ANY))
     addr2 = s2.getsockname()
 
@@ -325,6 +327,17 @@
     if errcodesok:
         assert err in (errno.ECONNREFUSED, errno.EADDRNOTAVAIL)
 
+def test_connect_with_timeout_fail():
+    s = RSocket()
+    s.settimeout(0.1)
+    with py.test.raises(SocketTimeout):
+        s.connect(INETAddress('240.240.240.240', 12345))
+
+def test_connect_with_timeout_succeed():
+    s = RSocket()
+    s.settimeout(10.0)
+    s.connect(INETAddress('python.org', 80))
+    s.close()
 
 def test_getsetsockopt():
     import struct
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to