1111import time
1212
1313
14- def _open_with_retry (func , host , * args , ** kwargs ):
15- # Connecting to remote hosts is flaky. Make it more robust
16- # by retrying the connection several times.
17- last_exc = None
18- for i in range (3 ):
19- try :
20- return func (host , * args , ** kwargs )
21- except IOError as err :
22- last_exc = err
23- continue
24- except :
25- raise
26- raise last_exc
27-
28-
2914class URLTimeoutTest (unittest .TestCase ):
3015
3116 TIMEOUT = 10.0
@@ -37,7 +22,8 @@ def tearDown(self):
3722 socket .setdefaulttimeout (None )
3823
3924 def testURLread (self ):
40- f = _open_with_retry (urllib .request .urlopen , "http://www.python.org/" )
25+ with support .transient_internet ("www.python.org" ):
26+ f = urllib .request .urlopen ("http://www.python.org/" )
4127 x = f .read ()
4228
4329class urlopenNetworkTests (unittest .TestCase ):
@@ -55,8 +41,10 @@ class urlopenNetworkTests(unittest.TestCase):
5541
5642 """
5743
58- def urlopen (self , * args ):
59- return _open_with_retry (urllib .request .urlopen , * args )
44+ def urlopen (self , * args , ** kwargs ):
45+ resource = args [0 ]
46+ with support .transient_internet (resource ):
47+ return urllib .request .urlopen (* args , ** kwargs )
6048
6149 def test_basic (self ):
6250 # Simple test expected to pass.
@@ -119,7 +107,7 @@ def test_fileno(self):
119107 # test can't pass on Windows.
120108 return
121109 # Make sure fd returned by fileno is valid.
122- open_url = self .urlopen ("http://www.python.org/" )
110+ open_url = self .urlopen ("http://www.python.org/" , timeout = None )
123111 fd = open_url .fileno ()
124112 FILE = os .fdopen (fd , encoding = 'utf-8' )
125113 try :
@@ -146,7 +134,9 @@ class urlretrieveNetworkTests(unittest.TestCase):
146134 """Tests urllib.request.urlretrieve using the network."""
147135
148136 def urlretrieve (self , * args ):
149- return _open_with_retry (urllib .request .urlretrieve , * args )
137+ resource = args [0 ]
138+ with support .transient_internet (resource ):
139+ return urllib .request .urlretrieve (* args )
150140
151141 def test_basic (self ):
152142 # Test basic functionality.
0 commit comments