44from datetime import datetime
55from functools import wraps
66import gzip
7- import http .client
87import os
98import re
109from shutil import rmtree
@@ -2275,11 +2274,17 @@ def dec(f):
22752274# But some tests (test_data yahoo) contact incredibly flakey
22762275# servers.
22772276
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
22802278
22812279
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 ):
22832288 """Try to connect to the given url. True if succeeds, False if IOError
22842289 raised
22852290
@@ -2294,6 +2299,10 @@ def can_connect(url, error_classes=_network_error_classes):
22942299 Return True if no IOError (unable to connect) or URLError (bad url) was
22952300 raised
22962301 """
2302+
2303+ if error_classes is None :
2304+ error_classes = _get_default_network_errors ()
2305+
22972306 try :
22982307 with urlopen (url ):
22992308 pass
@@ -2309,7 +2318,7 @@ def network(
23092318 url = "http://www.google.com" ,
23102319 raise_on_error = _RAISE_NETWORK_ERROR_DEFAULT ,
23112320 check_before_test = False ,
2312- error_classes = _network_error_classes ,
2321+ error_classes = None ,
23132322 skip_errnos = _network_errno_vals ,
23142323 _skip_on_messages = _network_error_messages ,
23152324):
@@ -2397,6 +2406,9 @@ def network(
23972406 """
23982407 from pytest import skip
23992408
2409+ if error_classes is None :
2410+ error_classes = _get_default_network_errors ()
2411+
24002412 t .network = True
24012413
24022414 @wraps (t )
0 commit comments