Mercurial > cpython-lru_cache-weakref
diff Lib/socket.py @ 47372:e2c5ae30cffe legacy-trunk
Fixed the semantic of timeout for socket.create_connection and
all the upper level libraries that use it, including urllib2.
Added and fixed some tests, and changed docs correspondingly.
Thanks to John J Lee for the patch and the pusing, :)
| author | Facundo Batista <facundobatista@gmail.com> |
|---|---|
| date | Thu, 29 May 2008 16:39:26 +0000 |
| parents | 2729e977fdd9 |
| children | 65901ffffc6a |
line wrap: on
line diff
--- a/Lib/socket.py Thu May 29 14:35:39 2008 +0000 +++ b/Lib/socket.py Thu May 29 16:39:26 2008 +0000 @@ -480,14 +480,17 @@ raise StopIteration return line +_GLOBAL_DEFAULT_TIMEOUT = object() -def create_connection(address, timeout=None): - """Connect to address (host, port) with an optional timeout. +def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT): + """Connect to *address* and return the socket object. - Provides access to socketobject timeout for higher-level - protocols. Passing a timeout will set the timeout on the - socket instance (if not present, or passed as None, the - default global timeout setting will be used). + Convenience function. Connect to *address* (a 2-tuple ``(host, + port)``) and return the socket object. Passing the optional + *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. """ msg = "getaddrinfo returns an empty list" @@ -497,7 +500,7 @@ sock = None try: sock = socket(af, socktype, proto) - if timeout is not None: + if timeout is not _GLOBAL_DEFAULT_TIMEOUT: sock.settimeout(timeout) sock.connect(sa) return sock
