[Jython-checkins] jython: Updating the create_connection function to version 2.7

alan.kennedy jython-checkins at python.org
Sun Apr 1 17:22:16 CEST 2012


http://hg.python.org/jython/rev/72e9076d1b5d
changeset:   6518:72e9076d1b5d
user:        Alan Kennedy <alan at xhaus.com>
date:        Sun Apr 01 14:30:40 2012 +0100
summary:
  Updating the create_connection function to version 2.7

files:
  Lib/socket.py |  19 ++++++++++++++-----
  1 files changed, 14 insertions(+), 5 deletions(-)


diff --git a/Lib/socket.py b/Lib/socket.py
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -1664,7 +1664,8 @@
 
 _GLOBAL_DEFAULT_TIMEOUT = object()
 
-def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT):
+def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
+                      source_address=None):
     """Connect to *address* and return the socket object.
 
     Convenience function.  Connect to *address* (a 2-tuple ``(host,
@@ -1672,11 +1673,13 @@
     *timeout* parameter will set the timeout on the socket instance
     before attempting to connect.  If no *timeout* is supplied, the
     global default timeout setting returned by :func:`getdefaulttimeout`
-    is used.
+    is used.  If *source_address* is set it must be a tuple of (host, port)
+    for the socket to bind as a source address before making the connection.
+    An host of '' or port 0 tells the OS to use the default.
     """
 
-    msg = "getaddrinfo returns an empty list"
     host, port = address
+    err = None
     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
         af, socktype, proto, canonname, sa = res
         sock = None
@@ -1684,14 +1687,20 @@
             sock = socket(af, socktype, proto)
             if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
                 sock.settimeout(timeout)
+            if source_address:
+                sock.bind(source_address)
             sock.connect(sa)
             return sock
 
-        except error, msg:
+        except error as _:
+            err = _
             if sock is not None:
                 sock.close()
 
-    raise error, msg
+    if err is not None:
+        raise err
+    else:
+        raise error("getaddrinfo returns an empty list")
 
 # Define the SSL support
 

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list