@@ -782,6 +782,7 @@ def connect_ftp(self, user, passwd, host, port, dirs,
782782 headers = r .info ()
783783 self .assertEqual (headers .get ("Content-type" ), mimetype )
784784 self .assertEqual (int (headers ["Content-length" ]), len (data ))
785+ r .close ()
785786
786787 @support .requires_resource ("network" )
787788 def test_ftp_error (self ):
@@ -1247,10 +1248,11 @@ def test_redirect(self):
12471248 try :
12481249 method (req , MockFile (), code , "Blah" ,
12491250 MockHeaders ({"location" : to_url }))
1250- except urllib .error .HTTPError :
1251+ except urllib .error .HTTPError as err :
12511252 # 307 and 308 in response to POST require user OK
12521253 self .assertIn (code , (307 , 308 ))
12531254 self .assertIsNotNone (data )
1255+ err .close ()
12541256 self .assertEqual (o .req .get_full_url (), to_url )
12551257 try :
12561258 self .assertEqual (o .req .get_method (), "GET" )
@@ -1286,9 +1288,10 @@ def redirect(h, req, url=to_url):
12861288 while 1 :
12871289 redirect (h , req , "http://example.com/" )
12881290 count = count + 1
1289- except urllib .error .HTTPError :
1291+ except urllib .error .HTTPError as err :
12901292 # don't stop until max_repeats, because cookies may introduce state
12911293 self .assertEqual (count , urllib .request .HTTPRedirectHandler .max_repeats )
1294+ err .close ()
12921295
12931296 # detect endless non-repeating chain of redirects
12941297 req = Request (from_url , origin_req_host = "example.com" )
@@ -1298,9 +1301,10 @@ def redirect(h, req, url=to_url):
12981301 while 1 :
12991302 redirect (h , req , "http://example.com/%d" % count )
13001303 count = count + 1
1301- except urllib .error .HTTPError :
1304+ except urllib .error .HTTPError as err :
13021305 self .assertEqual (count ,
13031306 urllib .request .HTTPRedirectHandler .max_redirections )
1307+ err .close ()
13041308
13051309 def test_invalid_redirect (self ):
13061310 from_url = "http://example.com/a.html"
@@ -1314,9 +1318,11 @@ def test_invalid_redirect(self):
13141318
13151319 for scheme in invalid_schemes :
13161320 invalid_url = scheme + '://' + schemeless_url
1317- self .assertRaises (urllib .error .HTTPError , h .http_error_302 ,
1321+ with self .assertRaises (urllib .error .HTTPError ) as cm :
1322+ h .http_error_302 (
13181323 req , MockFile (), 302 , "Security Loophole" ,
13191324 MockHeaders ({"location" : invalid_url }))
1325+ cm .exception .close ()
13201326
13211327 for scheme in valid_schemes :
13221328 valid_url = scheme + '://' + schemeless_url
@@ -1912,11 +1918,13 @@ def test_HTTPError_interface(self):
19121918 self .assertEqual (str (err ), expected_errmsg )
19131919 expected_errmsg = '<HTTPError %s: %r>' % (err .code , err .msg )
19141920 self .assertEqual (repr (err ), expected_errmsg )
1921+ err .close ()
19151922
19161923 def test_gh_98778 (self ):
19171924 x = urllib .error .HTTPError ("url" , 405 , "METHOD NOT ALLOWED" , None , None )
19181925 self .assertEqual (getattr (x , "__notes__" , ()), ())
19191926 self .assertIsInstance (x .fp .read (), bytes )
1927+ x .close ()
19201928
19211929 def test_parse_proxy (self ):
19221930 parse_proxy_test_cases = [
0 commit comments