summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-06-10 17:40:04 +0000
committerGeorg Brandl <georg@python.org>2008-06-10 17:40:04 +0000
commitf78e02b79862ac555fe052240bda55fa770a2d6f (patch)
tree885defd9b02e7865a9475b1d18b9c91a1fb8e93a /Lib
parent4066186c8934d66ac1ac795c7380a68468ad157a (diff)
downloadcpython-git-f78e02b79862ac555fe052240bda55fa770a2d6f.tar.gz
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r63562 | martin.v.loewis | 2008-05-23 17:06:50 +0200 (Fri, 23 May 2008) | 2 lines Patch #1722225: Support QNX 6. ........ r63570 | trent.nelson | 2008-05-23 22:33:14 +0200 (Fri, 23 May 2008) | 1 line Introduce a user macro named $(externalsDir), which should point to the root directory of where all the external sources should live. Developers can change this value if their external sources live elsewhere. The default of '..\..' matches the current status quo. ........ r63728 | gregory.p.smith | 2008-05-26 23:16:34 +0200 (Mon, 26 May 2008) | 4 lines Fix issue2589: there was a potential integer overflow leading to memory corruption on esoteric platforms and incorrect behavior on normal platforms. ........ r63734 | gregory.p.smith | 2008-05-27 00:07:28 +0200 (Tue, 27 May 2008) | 3 lines Fix issue2588: Do not execute str[size-1] = '\0' when a 0 size is passed in. (The assert won't prevent this in non-debug builds). ........ r63784 | raymond.hettinger | 2008-05-29 10:38:23 +0200 (Thu, 29 May 2008) | 1 line Fix two typos. ........ r63788 | facundo.batista | 2008-05-29 18:39:26 +0200 (Thu, 29 May 2008) | 6 lines 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, :) ........ r63802 | mark.dickinson | 2008-05-30 04:46:53 +0200 (Fri, 30 May 2008) | 2 lines Fix typo in testSum ........ r63817 | raymond.hettinger | 2008-05-30 20:20:50 +0200 (Fri, 30 May 2008) | 8 lines * Mark intermedidate computes values (hi, lo, yr) as volatile. * Expand comments. * Swap variable names in the sum_exact code so that x and y are consistently chosen as the larger and smaller magnitude values respectively. ........ r63827 | raymond.hettinger | 2008-05-31 05:24:31 +0200 (Sat, 31 May 2008) | 1 line Implement heapq in terms of less-than (to match list.sort()). ........ r63839 | gerhard.haering | 2008-05-31 23:33:27 +0200 (Sat, 31 May 2008) | 2 lines Fixed rowcount for SELECT statements. They're -1 now (again), for better DB-API 2.0 compliance. ........ r63887 | gregory.p.smith | 2008-06-02 06:05:52 +0200 (Mon, 02 Jun 2008) | 4 lines Fix issue 2782: be less strict about the format string type in strftime. Accept unicode and anything else ParseTuple "s#" can deal with. This matches the time.strftime behavior. ........ r63975 | neal.norwitz | 2008-06-06 06:47:01 +0200 (Fri, 06 Jun 2008) | 3 lines Aldo Cortesi confirmed this is still needed for OpenBSD 4.2 and 4.3. (I didn't regen configure, since I don't have a working autoconf.) ........ r63998 | raymond.hettinger | 2008-06-06 23:47:51 +0200 (Fri, 06 Jun 2008) | 1 line Issue 3501: Make heapq support both __le__ and __lt__. ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ftplib.py9
-rw-r--r--Lib/heapq.py13
-rw-r--r--Lib/http/client.py5
-rw-r--r--Lib/poplib.py3
-rwxr-xr-xLib/smtplib.py6
-rw-r--r--Lib/socket.py18
-rw-r--r--Lib/sqlite3/test/dbapi.py9
-rw-r--r--Lib/telnetlib.py16
-rw-r--r--Lib/test/test_datetime.py4
-rw-r--r--Lib/test/test_ftplib.py43
-rw-r--r--Lib/test/test_httplib.py29
-rw-r--r--Lib/test/test_math.py2
-rw-r--r--Lib/test/test_poplib.py25
-rw-r--r--Lib/test/test_smtplib.py34
-rw-r--r--Lib/test/test_socket.py32
-rw-r--r--Lib/test/test_telnetlib.py34
-rw-r--r--Lib/test/test_urllib.py40
-rw-r--r--Lib/test/test_urllib2.py8
-rw-r--r--Lib/test/test_urllib2net.py46
-rw-r--r--Lib/urllib.py3
-rw-r--r--Lib/urllib2.py4
21 files changed, 224 insertions, 159 deletions
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
index eec20f581d..4955727147 100644
--- a/Lib/ftplib.py
+++ b/Lib/ftplib.py
@@ -44,6 +44,7 @@ try:
from socket import getfqdn; socket.getfqdn = getfqdn; del getfqdn
except ImportError:
import socket
+from socket import _GLOBAL_DEFAULT_TIMEOUT
__all__ = ["FTP","Netrc"]
@@ -71,7 +72,6 @@ all_errors = (Error, IOError, EOFError)
# Line terminators (we always output CRLF, but accept any of CRLF, CR, LF)
CRLF = '\r\n'
-
# The class itself
class FTP:
@@ -110,14 +110,15 @@ class FTP:
# Initialize host to localhost, port to standard ftp port
# Optional arguments are host (for connect()),
# and user, passwd, acct (for login())
- def __init__(self, host='', user='', passwd='', acct='', timeout=None):
+ def __init__(self, host='', user='', passwd='', acct='',
+ timeout=_GLOBAL_DEFAULT_TIMEOUT):
self.timeout = timeout
if host:
self.connect(host)
if user:
self.login(user, passwd, acct)
- def connect(self, host='', port=0, timeout=None):
+ def connect(self, host='', port=0, timeout=-999):
'''Connect to host. Arguments are:
- host: hostname to connect to (string, default previous host)
- port: port to connect to (integer, default previous port)
@@ -126,7 +127,7 @@ class FTP:
self.host = host
if port > 0:
self.port = port
- if timeout is not None:
+ if timeout != -999:
self.timeout = timeout
self.sock = socket.create_connection((self.host, self.port), self.timeout)
self.af = self.sock.family
diff --git a/Lib/heapq.py b/Lib/heapq.py
index 71a12e7bf4..380fe1268b 100644
--- a/Lib/heapq.py
+++ b/Lib/heapq.py
@@ -167,7 +167,7 @@ def heapreplace(heap, item):
def heappushpop(heap, item):
"""Fast version of a heappush followed by a heappop."""
- if heap and item > heap[0]:
+ if heap and heap[0] < item:
item, heap[0] = heap[0], item
_siftup(heap, 0)
return item
@@ -240,10 +240,11 @@ def _siftdown(heap, startpos, pos):
while pos > startpos:
parentpos = (pos - 1) >> 1
parent = heap[parentpos]
- if parent <= newitem:
- break
- heap[pos] = parent
- pos = parentpos
+ if newitem < parent:
+ heap[pos] = parent
+ pos = parentpos
+ continue
+ break
heap[pos] = newitem
# The child indices of heap index pos are already heaps, and we want to make
@@ -294,7 +295,7 @@ def _siftup(heap, pos):
while childpos < endpos:
# Set childpos to index of smaller child.
rightpos = childpos + 1
- if rightpos < endpos and heap[rightpos] <= heap[childpos]:
+ if rightpos < endpos and not heap[childpos] < heap[rightpos]:
childpos = rightpos
# Move the smaller child up.
heap[pos] = heap[childpos]
diff --git a/Lib/http/client.py b/Lib/http/client.py
index de27c17b4c..c6e40e1cbb 100644
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -664,7 +664,8 @@ class HTTPConnection:
debuglevel = 0
strict = 0
- def __init__(self, host, port=None, strict=None, timeout=None):
+ def __init__(self, host, port=None, strict=None,
+ timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
self.timeout = timeout
self.sock = None
self._buffer = []
@@ -996,7 +997,7 @@ else:
default_port = HTTPS_PORT
def __init__(self, host, port=None, key_file=None, cert_file=None,
- strict=None, timeout=None):
+ strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
HTTPConnection.__init__(self, host, port, strict, timeout)
self.key_file = key_file
self.cert_file = cert_file
diff --git a/Lib/poplib.py b/Lib/poplib.py
index aa4600c4a8..bd82841ef1 100644
--- a/Lib/poplib.py
+++ b/Lib/poplib.py
@@ -76,7 +76,8 @@ class POP3:
"""
- def __init__(self, host, port=POP3_PORT, timeout=None):
+ def __init__(self, host, port=POP3_PORT,
+ timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
self.host = host
self.port = port
self.sock = socket.create_connection((host, port), timeout)
diff --git a/Lib/smtplib.py b/Lib/smtplib.py
index a3cc65d295..e67f0d6520 100755
--- a/Lib/smtplib.py
+++ b/Lib/smtplib.py
@@ -220,7 +220,8 @@ class SMTP:
ehlo_resp = None
does_esmtp = 0
- def __init__(self, host='', port=0, local_hostname=None, timeout=None):
+ def __init__(self, host='', port=0, local_hostname=None,
+ timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
"""Initialize a new instance.
If specified, `host' is the name of the remote host to which to
@@ -744,7 +745,8 @@ if _have_ssl:
certificate chain file for the SSL connection.
"""
def __init__(self, host='', port=0, local_hostname=None,
- keyfile=None, certfile=None, timeout=None):
+ keyfile=None, certfile=None,
+ timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
self.keyfile = keyfile
self.certfile = certfile
SMTP.__init__(self, host, port, local_hostname, timeout)
diff --git a/Lib/socket.py b/Lib/socket.py
index eb876731e3..fcbc545d5a 100644
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -265,13 +265,17 @@ def getfqdn(name=''):
return name
-def create_connection(address, timeout=None):
- """Connect to address (host, port) with an optional timeout.
+_GLOBAL_DEFAULT_TIMEOUT = 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).
+def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT):
+ """Connect to *address* and return the socket object.
+
+ 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"
@@ -281,7 +285,7 @@ def create_connection(address, timeout=None):
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
diff --git a/Lib/sqlite3/test/dbapi.py b/Lib/sqlite3/test/dbapi.py
index 8327aa195b..d276a336c9 100644
--- a/Lib/sqlite3/test/dbapi.py
+++ b/Lib/sqlite3/test/dbapi.py
@@ -292,6 +292,15 @@ class CursorTests(unittest.TestCase):
self.cu.execute("update test set name='bar'")
self.failUnlessEqual(self.cu.rowcount, 2)
+ def CheckRowcountSelect(self):
+ """
+ pysqlite does not know the rowcount of SELECT statements, because we
+ don't fetch all rows after executing the select statement. The rowcount
+ has thus to be -1.
+ """
+ self.cu.execute("select 5 union select 6")
+ self.failUnlessEqual(self.cu.rowcount, -1)
+
def CheckRowcountExecutemany(self):
self.cu.execute("delete from test")
self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)])
diff --git a/Lib/telnetlib.py b/Lib/telnetlib.py
index 33029e1047..56c5d702b6 100644
--- a/Lib/telnetlib.py
+++ b/Lib/telnetlib.py
@@ -184,13 +184,13 @@ class Telnet:
"""
- def __init__(self, host=None, port=0, timeout=None):
+ def __init__(self, host=None, port=0,
+ timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
"""Constructor.
When called without arguments, create an unconnected instance.
- With a hostname argument, it connects the instance; a port
- number is optional.
-
+ With a hostname argument, it connects the instance; port number
+ and timeout are optional.
"""
self.debuglevel = DEBUGLEVEL
self.host = host
@@ -208,23 +208,21 @@ class Telnet:
if host is not None:
self.open(host, port, timeout)
- def open(self, host, port=0, timeout=None):
+ def open(self, host, port=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
"""Connect to a host.
The optional second argument is the port number, which
defaults to the standard telnet port (23).
Don't try to reopen an already connected instance.
-
"""
self.eof = 0
if not port:
port = TELNET_PORT
self.host = host
self.port = port
- if timeout is not None:
- self.timeout = timeout
- self.sock = socket.create_connection((host, port), self.timeout)
+ self.timeout = timeout
+ self.sock = socket.create_connection((host, port), timeout)
def __del__(self):
"""Destructor -- close the connection."""
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py
index 2048fa0a42..4b8ae82df7 100644
--- a/Lib/test/test_datetime.py
+++ b/Lib/test/test_datetime.py
@@ -845,9 +845,13 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
self.assertRaises(TypeError, t.strftime, "one", "two") # too many args
self.assertRaises(TypeError, t.strftime, 42) # arg wrong type
+ # test that unicode input is allowed (issue 2782)
+ self.assertEqual(t.strftime("%m"), "03")
+
# A naive object replaces %z and %Z w/ empty strings.
self.assertEqual(t.strftime("'%z' '%Z'"), "'' ''")
+
def test_format(self):
dt = self.theclass(2007, 9, 10)
self.assertEqual(dt.__format__(''), str(dt))
diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py
index b91588a310..35e4746a8c 100644
--- a/Lib/test/test_ftplib.py
+++ b/Lib/test/test_ftplib.py
@@ -54,35 +54,52 @@ class GeneralTests(TestCase):
# connects
ftp = ftplib.FTP(HOST)
self.evt.wait()
- ftp.sock.close()
+ ftp.close()
def testTimeoutDefault(self):
- # default
- ftp = ftplib.FTP(HOST)
+ # default -- use global socket timeout
+ self.assert_(socket.getdefaulttimeout() is None)
+ socket.setdefaulttimeout(30)
+ try:
+ ftp = ftplib.FTP("localhost")
+ finally:
+ socket.setdefaulttimeout(None)
+ self.assertEqual(ftp.sock.gettimeout(), 30)
+ self.evt.wait()
+ ftp.close()
+
+ def testTimeoutNone(self):
+ # no timeout -- do not use global socket timeout
+ self.assert_(socket.getdefaulttimeout() is None)
+ socket.setdefaulttimeout(30)
+ try:
+ ftp = ftplib.FTP("localhost", timeout=None)
+ finally:
+ socket.setdefaulttimeout(None)
self.assertTrue(ftp.sock.gettimeout() is None)
self.evt.wait()
- ftp.sock.close()
+ ftp.close()
def testTimeoutValue(self):
# a value
ftp = ftplib.FTP(HOST, timeout=30)
self.assertEqual(ftp.sock.gettimeout(), 30)
self.evt.wait()
- ftp.sock.close()
+ ftp.close()
def testTimeoutConnect(self):
ftp = ftplib.FTP()
ftp.connect(HOST, timeout=30)
self.assertEqual(ftp.sock.gettimeout(), 30)
self.evt.wait()
- ftp.sock.close()
+ ftp.close()
def testTimeoutDifferentOrder(self):
ftp = ftplib.FTP(timeout=30)
ftp.connect(HOST)
self.assertEqual(ftp.sock.gettimeout(), 30)
self.evt.wait()
- ftp.sock.close()
+ ftp.close()
def testTimeoutDirectAccess(self):
ftp = ftplib.FTP()
@@ -90,18 +107,6 @@ class GeneralTests(TestCase):
ftp.connect(HOST)
self.assertEqual(ftp.sock.gettimeout(), 30)
self.evt.wait()
- ftp.sock.close()
-
- def testTimeoutNone(self):
- # None, having other default
- previous = socket.getdefaulttimeout()
- socket.setdefaulttimeout(30)
- try:
- ftp = ftplib.FTP(HOST, timeout=None)
- finally:
- socket.setdefaulttimeout(previous)
- self.assertEqual(ftp.sock.gettimeout(), 30)
- self.evt.wait()
ftp.close()
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index ff4cf9bd71..054825a5a3 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -215,27 +215,32 @@ class TimeoutTest(TestCase):
# This will prove that the timeout gets through HTTPConnection
# and into the socket.
- # default
- httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT)
- httpConn.connect()
- self.assertTrue(httpConn.sock.gettimeout() is None)
- httpConn.close()
-
- # a value
- httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30)
- httpConn.connect()
+ # default -- use global socket timeout
+ self.assert_(socket.getdefaulttimeout() is None)
+ socket.setdefaulttimeout(30)
+ try:
+ httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT)
+ httpConn.connect()
+ finally:
+ socket.setdefaulttimeout(None)
self.assertEqual(httpConn.sock.gettimeout(), 30)
httpConn.close()
- # None, having other default
- previous = socket.getdefaulttimeout()
+ # no timeout -- do not use global socket default
+ self.assert_(socket.getdefaulttimeout() is None)
socket.setdefaulttimeout(30)
try:
httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT,
timeout=None)
httpConn.connect()
finally:
- socket.setdefaulttimeout(previous)
+ socket.setdefaulttimeout(None)
+ self.assertEqual(httpConn.sock.gettimeout(), None)
+ httpConn.close()
+
+ # a value
+ httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30)
+ httpConn.connect()
self.assertEqual(httpConn.sock.gettimeout(), 30)
httpConn.close()
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index ae29cda667..f4c27bb389 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -736,7 +736,7 @@ class MathTests(unittest.TestCase):
OverflowError),
([2.**1023, 2.**1023, -1e307], OverflowError),
([1e16, 1., 1e-16], 10000000000000002.0),
- ([1e16-2., 1.-2.**53, -(1e16-2.), -(1.-2.**53)], 0.0),
+ ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
]
for i, (vals, s) in enumerate(test_values):
diff --git a/Lib/test/test_poplib.py b/Lib/test/test_poplib.py
index 14b46e3162..f9c52f911f 100644
--- a/Lib/test/test_poplib.py
+++ b/Lib/test/test_poplib.py
@@ -40,28 +40,29 @@ class GeneralTests(TestCase):
pop.sock.close()
def testTimeoutDefault(self):
- # default
- pop = poplib.POP3(HOST, self.port)
- self.assertTrue(pop.sock.gettimeout() is None)
- pop.sock.close()
-
- def testTimeoutValue(self):
- # a value
- pop = poplib.POP3(HOST, self.port, timeout=30)
+ self.assertTrue(socket.getdefaulttimeout() is None)
+ socket.setdefaulttimeout(30)
+ try:
+ pop = poplib.POP3("localhost", self.port)
+ finally:
+ socket.setdefaulttimeout(None)
self.assertEqual(pop.sock.gettimeout(), 30)
pop.sock.close()
def testTimeoutNone(self):
- # None, having other default
- previous = socket.getdefaulttimeout()
+ self.assertTrue(socket.getdefaulttimeout() is None)
socket.setdefaulttimeout(30)
try:
pop = poplib.POP3(HOST, self.port, timeout=None)
finally:
- socket.setdefaulttimeout(previous)
- self.assertEqual(pop.sock.gettimeout(), 30)
+ socket.setdefaulttimeout(None)
+ self.assertTrue(pop.sock.gettimeout() is None)
pop.sock.close()
+ def testTimeoutValue(self):
+ pop = poplib.POP3("localhost", self.port, timeout=30)
+ self.assertEqual(pop.sock.gettimeout(), 30)
+ pop.sock.close()
def test_main(verbose=None):
diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py
index 3d38d89d27..81302520e6 100644
--- a/Lib/test/test_smtplib.py
+++ b/Lib/test/test_smtplib.py
@@ -54,41 +54,43 @@ class GeneralTests(TestCase):
def testBasic1(self):
# connects
smtp = smtplib.SMTP(HOST, self.port)
- smtp.sock.close()
+ smtp.close()
def testBasic2(self):
# connects, include port in host name
smtp = smtplib.SMTP("%s:%s" % (HOST, self.port))
- smtp.sock.close()
+ smtp.close()
def testLocalHostName(self):
# check that supplied local_hostname is used
smtp = smtplib.SMTP(HOST, self.port, local_hostname="testhost")
self.assertEqual(smtp.local_hostname, "testhost")
- smtp.sock.close()
+ smtp.close()
def testTimeoutDefault(self):
- # default
- smtp = smtplib.SMTP(HOST, self.port)
- self.assertTrue(smtp.sock.gettimeout() is None)
- smtp.sock.close()
-
- def testTimeoutValue(self):
- # a value
- smtp = smtplib.SMTP(HOST, self.port, timeout=30)
+ self.assertTrue(socket.getdefaulttimeout() is None)
+ socket.setdefaulttimeout(30)
+ try:
+ smtp = smtplib.SMTP(HOST, self.port)
+ finally:
+ socket.setdefaulttimeout(None)
self.assertEqual(smtp.sock.gettimeout(), 30)
- smtp.sock.close()
+ smtp.close()
def testTimeoutNone(self):
- # None, having other default
- previous = socket.getdefaulttimeout()
+ self.assertTrue(socket.getdefaulttimeout() is None)
socket.setdefaulttimeout(30)
try:
smtp = smtplib.SMTP(HOST, self.port, timeout=None)
finally:
- socket.setdefaulttimeout(previous)
+ socket.setdefaulttimeout(None)
+ self.assertTrue(smtp.sock.gettimeout() is None)
+ smtp.close()
+
+ def testTimeoutValue(self):
+ smtp = smtplib.SMTP(HOST, self.port, timeout=30)
self.assertEqual(smtp.sock.gettimeout(), 30)
- smtp.sock.close()
+ smtp.close()
# Test server thread using the specified SMTP server class
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index d6645822e5..e62788ee27 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -929,8 +929,25 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
testTimeoutDefault = _justAccept
def _testTimeoutDefault(self):
- self.cli = socket.create_connection((HOST, self.port))
- self.assertTrue(self.cli.gettimeout() is None)
+ # passing no explicit timeout uses socket's global default
+ self.assert_(socket.getdefaulttimeout() is None)
+ socket.setdefaulttimeout(42)
+ try:
+ self.cli = socket.create_connection((HOST, self.port))
+ finally:
+ socket.setdefaulttimeout(None)
+ self.assertEquals(self.cli.gettimeout(), 42)
+
+ testTimeoutNone = _justAccept
+ def _testTimeoutNone(self):
+ # None timeout means the same as sock.settimeout(None)
+ self.assert_(socket.getdefaulttimeout() is None)
+ socket.setdefaulttimeout(30)
+ try:
+ self.cli = socket.create_connection((HOST, self.port), timeout=None)
+ finally:
+ socket.setdefaulttimeout(None)
+ self.assertEqual(self.cli.gettimeout(), None)
testTimeoutValueNamed = _justAccept
def _testTimeoutValueNamed(self):
@@ -942,17 +959,6 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
self.cli = socket.create_connection((HOST, self.port), 30)
self.assertEqual(self.cli.gettimeout(), 30)
- testTimeoutNone = _justAccept
- def _testTimeoutNone(self):
- previous = socket.getdefaulttimeout()
- socket.setdefaulttimeout(30)
- try:
- self.cli = socket.create_connection((HOST, self.port), timeout=None)
- finally:
- socket.setdefaulttimeout(previous)
- self.assertEqual(self.cli.gettimeout(), 30)
-
-
class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest):
def __init__(self, methodName='runTest'):
diff --git a/Lib/test/test_telnetlib.py b/Lib/test/test_telnetlib.py
index c24d41d02d..4ae6222f6c 100644
--- a/Lib/test/test_telnetlib.py
+++ b/Lib/test/test_telnetlib.py
@@ -40,34 +40,36 @@ class GeneralTests(TestCase):
telnet.sock.close()
def testTimeoutDefault(self):
- # default
- telnet = telnetlib.Telnet(HOST, self.port)
- self.assertTrue(telnet.sock.gettimeout() is None)
- telnet.sock.close()
-
- def testTimeoutValue(self):
- # a value
- telnet = telnetlib.Telnet(HOST, self.port, timeout=30)
- self.assertEqual(telnet.sock.gettimeout(), 30)
- telnet.sock.close()
-
- def testTimeoutDifferentOrder(self):
- telnet = telnetlib.Telnet(timeout=30)
- telnet.open(HOST, self.port)
+ self.assertTrue(socket.getdefaulttimeout() is None)
+ socket.setdefaulttimeout(30)
+ try:
+ telnet = telnetlib.Telnet("localhost", self.port)
+ finally:
+ socket.setdefaulttimeout(None)
self.assertEqual(telnet.sock.gettimeout(), 30)
telnet.sock.close()
def testTimeoutNone(self):
# None, having other default
- previous = socket.getdefaulttimeout()
+ self.assertTrue(socket.getdefaulttimeout() is None)
socket.setdefaulttimeout(30)
try:
telnet = telnetlib.Telnet(HOST, self.port, timeout=None)
finally:
- socket.setdefaulttimeout(previous)
+ socket.setdefaulttimeout(None)
+ self.assertTrue(telnet.sock.gettimeout() is None)
+ telnet.sock.close()
+
+ def testTimeoutValue(self):
+ telnet = telnetlib.Telnet("localhost", self.port, timeout=30)
self.assertEqual(telnet.sock.gettimeout(), 30)
telnet.sock.close()
+ def testTimeoutOpen(self):
+ telnet = telnetlib.Telnet()
+ telnet.open("localhost", self.port, timeout=30)
+ self.assertEqual(telnet.sock.gettimeout(), 30)
+ telnet.sock.close()
def test_main(verbose=None):
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index b14510fce2..26b51cb77c 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -568,6 +568,7 @@ class Pathname_Tests(unittest.TestCase):
# . Facundo
#
# def server(evt):
+# import socket, time
# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# serv.settimeout(3)
# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@@ -592,6 +593,7 @@ class Pathname_Tests(unittest.TestCase):
# class FTPWrapperTests(unittest.TestCase):
#
# def setUp(self):
+# import ftplib, time, threading
# ftplib.FTP.port = 9093
# self.evt = threading.Event()
# threading.Thread(target=server, args=(self.evt,)).start()
@@ -603,31 +605,37 @@ class Pathname_Tests(unittest.TestCase):
# def testBasic(self):
# # connects
# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
-# ftp.ftp.sock.close()
+# ftp.close()
#
-# def testTimeoutDefault(self):
-# # default
-# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
-# self.assertTrue(ftp.ftp.sock.gettimeout() is None)
-# ftp.ftp.sock.close()
-#
-# def testTimeoutValue(self):
-# # a value
-# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [], timeout=30)
+# def testTimeoutNone(self):
+# # global default timeout is ignored
+# import socket
+# self.assert_(socket.getdefaulttimeout() is None)
+# socket.setdefaulttimeout(30)
+# try:
+# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
+# finally:
+# socket.setdefaulttimeout(None)
# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
-# ftp.ftp.sock.close()
+# ftp.close()
#
-# def testTimeoutNone(self):
-# # None, having other default
-# previous = socket.getdefaulttimeout()
+# def testTimeoutDefault(self):
+# # global default timeout is used
+# import socket
+# self.assert_(socket.getdefaulttimeout() is None)
# socket.setdefaulttimeout(30)
# try:
# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
# finally:
-# socket.setdefaulttimeout(previous)
+# socket.setdefaulttimeout(None)
# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
-# ftp.ftp.close()
+# ftp.close()
#
+# def testTimeoutValue(self):
+# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
+# timeout=30)
+# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
+# ftp.close()
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index 33868002fc..c2d594b400 100644
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -3,6 +3,7 @@ from test import support
import os
import io
+import socket
import urllib2
from urllib2 import Request, OpenerDirector
@@ -546,14 +547,15 @@ class HandlerTests(unittest.TestCase):
class NullFTPHandler(urllib2.FTPHandler):
def __init__(self, data): self.data = data
- def connect_ftp(self, user, passwd, host, port, dirs, timeout=None):
+ def connect_ftp(self, user, passwd, host, port, dirs,
+ timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
self.user, self.passwd = user, passwd
self.host, self.port = host, port
self.dirs = dirs
self.ftpwrapper = MockFTPWrapper(self.data)
return self.ftpwrapper
- import ftplib, socket
+ import ftplib
data = "rheum rhaponicum"
h = NullFTPHandler(data)
o = h.parent = MockOpener()
@@ -686,7 +688,7 @@ class HandlerTests(unittest.TestCase):
self.req_headers = []
self.data = None
self.raise_on_endheaders = False
- def __call__(self, host, timeout=None):
+ def __call__(self, host, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
self.host = host
self.timeout = timeout
return self
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py
index 990d3da536..752406c446 100644
--- a/Lib/test/test_urllib2net.py
+++ b/Lib/test/test_urllib2net.py
@@ -188,46 +188,58 @@ class OtherNetworkTests(unittest.TestCase):
class TimeoutTest(unittest.TestCase):
def test_http_basic(self):
+ self.assertTrue(socket.getdefaulttimeout() is None)
u = _urlopen_with_retry("http://www.python.org")
self.assertTrue(u.fp.raw.fp._sock.gettimeout() is None)
- def test_http_NoneWithdefault(self):
- prev = socket.getdefaulttimeout()
+ def test_http_default_timeout(self):
+ self.assertTrue(socket.getdefaulttimeout() is None)
+ socket.setdefaulttimeout(60)
+ try:
+ u = _urlopen_with_retry("http://www.python.org")
+ finally:
+ socket.setdefaulttimeout(None)
+ self.assertEqual(u.fp.raw.fp._sock.gettimeout(), 60)
+
+ def test_http_no_timeout(self):
+ self.assertTrue(socket.getdefaulttimeout() is None)
socket.setdefaulttimeout(60)
try:
u = _urlopen_with_retry("http://www.python.org", timeout=None)
- self.assertTrue(u.fp.raw.fp._sock.gettimeout(), 60)
finally:
- socket.setdefaulttimeout(prev)
+ socket.setdefaulttimeout(None)
+ self.assertTrue(u.fp.raw.fp._sock.gettimeout() is None)
- def test_http_Value(self):
+ def test_http_timeout(self):
u = _urlopen_with_retry("http://www.python.org", timeout=120)
self.assertEqual(u.fp.raw.fp._sock.gettimeout(), 120)
- def test_http_NoneNodefault(self):
- u = _urlopen_with_retry("http://www.python.org", timeout=None)
- self.assertTrue(u.fp.raw.fp._sock.gettimeout() is None)
-
FTP_HOST = "ftp://ftp.mirror.nl/pub/mirror/gnu/"
def test_ftp_basic(self):
+ self.assertTrue(socket.getdefaulttimeout() is None)
u = _urlopen_with_retry(self.FTP_HOST)
self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
- def test_ftp_NoneWithdefault(self):
- prev = socket.getdefaulttimeout()
+ def test_ftp_default_timeout(self):
+ self.assertTrue(socket.getdefaulttimeout() is None)
socket.setdefaulttimeout(60)
try:
- u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
- self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
+ u = _urlopen_with_retry(self.FTP_HOST)
finally:
- socket.setdefaulttimeout(prev)
+ socket.setdefaulttimeout(None)
+ self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
- def test_ftp_NoneNodefault(self):
- u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
+ def test_ftp_no_timeout(self):
+ self.assertTrue(socket.getdefaulttimeout() is None)
+ socket.setdefaulttimeout(60)
+ try:
+ u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
+ finally:
+ socket.setdefaulttimeout(None)
self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
- def test_ftp_Value(self):
+ def test_ftp_timeout(self):
u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
diff --git a/Lib/urllib.py b/Lib/urllib.py
index 2005baf194..fb93604902 100644
--- a/Lib/urllib.py
+++ b/Lib/urllib.py
@@ -776,7 +776,8 @@ def noheaders():
class ftpwrapper:
"""Class used by open_ftp() for cache of open FTP connections."""
- def __init__(self, user, passwd, host, port, dirs, timeout=None):
+ def __init__(self, user, passwd, host, port, dirs,
+ timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
self.user = user
self.passwd = passwd
self.host = host
diff --git a/Lib/urllib2.py b/Lib/urllib2.py
index 948c6c3c35..244d737819 100644
--- a/Lib/urllib2.py
+++ b/Lib/urllib2.py
@@ -115,7 +115,7 @@ from urllib import localhost, url2pathname, getproxies
__version__ = sys.version[:3]
_opener = None
-def urlopen(url, data=None, timeout=None):
+def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
global _opener
if _opener is None:
_opener = build_opener()
@@ -357,7 +357,7 @@ class OpenerDirector:
if result is not None:
return result
- def open(self, fullurl, data=None, timeout=None):
+ def open(self, fullurl, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
# accept a URL or a Request object
if isinstance(fullurl, str):
req = Request(fullurl, data)