@@ -789,6 +789,7 @@ def connect_ftp(self, user, passwd, host, port, dirs,
789789 headers = r .info ()
790790 self .assertEqual (headers .get ("Content-type" ), mimetype )
791791 self .assertEqual (int (headers ["Content-length" ]), len (data ))
792+ r .close ()
792793
793794 def test_file (self ):
794795 import email .utils
@@ -1229,10 +1230,11 @@ def test_redirect(self):
12291230 try :
12301231 method (req , MockFile (), code , "Blah" ,
12311232 MockHeaders ({"location" : to_url }))
1232- except urllib .error .HTTPError :
1233+ except urllib .error .HTTPError as err :
12331234 # 307 and 308 in response to POST require user OK
12341235 self .assertIn (code , (307 , 308 ))
12351236 self .assertIsNotNone (data )
1237+ err .close ()
12361238 self .assertEqual (o .req .get_full_url (), to_url )
12371239 try :
12381240 self .assertEqual (o .req .get_method (), "GET" )
@@ -1268,9 +1270,10 @@ def redirect(h, req, url=to_url):
12681270 while 1 :
12691271 redirect (h , req , "http://example.com/" )
12701272 count = count + 1
1271- except urllib .error .HTTPError :
1273+ except urllib .error .HTTPError as err :
12721274 # don't stop until max_repeats, because cookies may introduce state
12731275 self .assertEqual (count , urllib .request .HTTPRedirectHandler .max_repeats )
1276+ err .close ()
12741277
12751278 # detect endless non-repeating chain of redirects
12761279 req = Request (from_url , origin_req_host = "example.com" )
@@ -1280,9 +1283,10 @@ def redirect(h, req, url=to_url):
12801283 while 1 :
12811284 redirect (h , req , "http://example.com/%d" % count )
12821285 count = count + 1
1283- except urllib .error .HTTPError :
1286+ except urllib .error .HTTPError as err :
12841287 self .assertEqual (count ,
12851288 urllib .request .HTTPRedirectHandler .max_redirections )
1289+ err .close ()
12861290
12871291 def test_invalid_redirect (self ):
12881292 from_url = "http://example.com/a.html"
@@ -1296,9 +1300,11 @@ def test_invalid_redirect(self):
12961300
12971301 for scheme in invalid_schemes :
12981302 invalid_url = scheme + '://' + schemeless_url
1299- self .assertRaises (urllib .error .HTTPError , h .http_error_302 ,
1303+ with self .assertRaises (urllib .error .HTTPError ) as cm :
1304+ h .http_error_302 (
13001305 req , MockFile (), 302 , "Security Loophole" ,
13011306 MockHeaders ({"location" : invalid_url }))
1307+ cm .exception .close ()
13021308
13031309 for scheme in valid_schemes :
13041310 valid_url = scheme + '://' + schemeless_url
@@ -1894,11 +1900,13 @@ def test_HTTPError_interface(self):
18941900 self .assertEqual (str (err ), expected_errmsg )
18951901 expected_errmsg = '<HTTPError %s: %r>' % (err .code , err .msg )
18961902 self .assertEqual (repr (err ), expected_errmsg )
1903+ err .close ()
18971904
18981905 def test_gh_98778 (self ):
18991906 x = urllib .error .HTTPError ("url" , 405 , "METHOD NOT ALLOWED" , None , None )
19001907 self .assertEqual (getattr (x , "__notes__" , ()), ())
19011908 self .assertIsInstance (x .fp .read (), bytes )
1909+ x .close ()
19021910
19031911 def test_parse_proxy (self ):
19041912 parse_proxy_test_cases = [
0 commit comments