@@ -782,6 +782,7 @@ def connect_ftp(self, user, passwd, host, port, dirs,
782
782
headers = r .info ()
783
783
self .assertEqual (headers .get ("Content-type" ), mimetype )
784
784
self .assertEqual (int (headers ["Content-length" ]), len (data ))
785
+ r .close ()
785
786
786
787
@support .requires_resource ("network" )
787
788
def test_ftp_error (self ):
@@ -1247,10 +1248,11 @@ def test_redirect(self):
1247
1248
try :
1248
1249
method (req , MockFile (), code , "Blah" ,
1249
1250
MockHeaders ({"location" : to_url }))
1250
- except urllib .error .HTTPError :
1251
+ except urllib .error .HTTPError as err :
1251
1252
# 307 and 308 in response to POST require user OK
1252
1253
self .assertIn (code , (307 , 308 ))
1253
1254
self .assertIsNotNone (data )
1255
+ err .close ()
1254
1256
self .assertEqual (o .req .get_full_url (), to_url )
1255
1257
try :
1256
1258
self .assertEqual (o .req .get_method (), "GET" )
@@ -1286,9 +1288,10 @@ def redirect(h, req, url=to_url):
1286
1288
while 1 :
1287
1289
redirect (h , req , "http://example.com/" )
1288
1290
count = count + 1
1289
- except urllib .error .HTTPError :
1291
+ except urllib .error .HTTPError as err :
1290
1292
# don't stop until max_repeats, because cookies may introduce state
1291
1293
self .assertEqual (count , urllib .request .HTTPRedirectHandler .max_repeats )
1294
+ err .close ()
1292
1295
1293
1296
# detect endless non-repeating chain of redirects
1294
1297
req = Request (from_url , origin_req_host = "example.com" )
@@ -1298,9 +1301,10 @@ def redirect(h, req, url=to_url):
1298
1301
while 1 :
1299
1302
redirect (h , req , "http://example.com/%d" % count )
1300
1303
count = count + 1
1301
- except urllib .error .HTTPError :
1304
+ except urllib .error .HTTPError as err :
1302
1305
self .assertEqual (count ,
1303
1306
urllib .request .HTTPRedirectHandler .max_redirections )
1307
+ err .close ()
1304
1308
1305
1309
def test_invalid_redirect (self ):
1306
1310
from_url = "http://example.com/a.html"
@@ -1314,9 +1318,11 @@ def test_invalid_redirect(self):
1314
1318
1315
1319
for scheme in invalid_schemes :
1316
1320
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 (
1318
1323
req , MockFile (), 302 , "Security Loophole" ,
1319
1324
MockHeaders ({"location" : invalid_url }))
1325
+ cm .exception .close ()
1320
1326
1321
1327
for scheme in valid_schemes :
1322
1328
valid_url = scheme + '://' + schemeless_url
@@ -1912,11 +1918,13 @@ def test_HTTPError_interface(self):
1912
1918
self .assertEqual (str (err ), expected_errmsg )
1913
1919
expected_errmsg = '<HTTPError %s: %r>' % (err .code , err .msg )
1914
1920
self .assertEqual (repr (err ), expected_errmsg )
1921
+ err .close ()
1915
1922
1916
1923
def test_gh_98778 (self ):
1917
1924
x = urllib .error .HTTPError ("url" , 405 , "METHOD NOT ALLOWED" , None , None )
1918
1925
self .assertEqual (getattr (x , "__notes__" , ()), ())
1919
1926
self .assertIsInstance (x .fp .read (), bytes )
1927
+ x .close ()
1920
1928
1921
1929
def test_parse_proxy (self ):
1922
1930
parse_proxy_test_cases = [
0 commit comments