4
4
from datetime import datetime
5
5
from functools import wraps
6
6
import gzip
7
- import http .client
8
7
import os
9
8
import re
10
9
from shutil import rmtree
@@ -2275,11 +2274,17 @@ def dec(f):
2275
2274
# But some tests (test_data yahoo) contact incredibly flakey
2276
2275
# servers.
2277
2276
2278
- # and conditionally raise on these exception types
2279
- _network_error_classes = (IOError , http .client .HTTPException , TimeoutError )
2277
+ # and conditionally raise on exception types in _get_default_network_errors
2280
2278
2281
2279
2282
- def can_connect (url , error_classes = _network_error_classes ):
2280
+ def _get_default_network_errors ():
2281
+ # Lazy import for http.client because it imports many things from the stdlib
2282
+ import http .client
2283
+
2284
+ return (IOError , http .client .HTTPException , TimeoutError )
2285
+
2286
+
2287
+ def can_connect (url , error_classes = None ):
2283
2288
"""Try to connect to the given url. True if succeeds, False if IOError
2284
2289
raised
2285
2290
@@ -2294,6 +2299,10 @@ def can_connect(url, error_classes=_network_error_classes):
2294
2299
Return True if no IOError (unable to connect) or URLError (bad url) was
2295
2300
raised
2296
2301
"""
2302
+
2303
+ if error_classes is None :
2304
+ error_classes = _get_default_network_errors ()
2305
+
2297
2306
try :
2298
2307
with urlopen (url ):
2299
2308
pass
@@ -2309,7 +2318,7 @@ def network(
2309
2318
url = "http://www.google.com" ,
2310
2319
raise_on_error = _RAISE_NETWORK_ERROR_DEFAULT ,
2311
2320
check_before_test = False ,
2312
- error_classes = _network_error_classes ,
2321
+ error_classes = None ,
2313
2322
skip_errnos = _network_errno_vals ,
2314
2323
_skip_on_messages = _network_error_messages ,
2315
2324
):
@@ -2397,6 +2406,9 @@ def network(
2397
2406
"""
2398
2407
from pytest import skip
2399
2408
2409
+ if error_classes is None :
2410
+ error_classes = _get_default_network_errors ()
2411
+
2400
2412
t .network = True
2401
2413
2402
2414
@wraps (t )
0 commit comments