@@ -251,24 +251,21 @@ def _download_file(url, required_length, STRICT_REQUIRED_LENGTH=True):
251
251
# Always stream to control how requests are downloaded:
252
252
# http://docs.python-requests.org/en/master/user/advanced/#body-content-workflow
253
253
#
254
- # We will always manually close Responses, so no need for a context
255
- # manager.
256
- #
257
254
# Always set the timeout. This timeout value is interpreted by requests as:
258
255
# - connect timeout (max delay before first byte is received)
259
256
# - read (gap) timeout (max delay between bytes received)
260
257
# These are NOT overall/total, wall-clock timeouts for any single read.
261
258
# http://docs.python-requests.org/en/master/user/advanced/#timeouts
262
- response = session .get (
263
- url , stream = True , timeout = tuf .settings .SOCKET_TIMEOUT )
259
+ with session .get (url , stream = True ,
260
+ timeout = tuf .settings .SOCKET_TIMEOUT ) as response :
264
261
265
- # Check response status.
266
- response .raise_for_status ()
262
+ # Check response status.
263
+ response .raise_for_status ()
267
264
268
- # Download the contents of the URL, up to the required length, to a
269
- # temporary file, and get the total number of downloaded bytes.
270
- total_downloaded , average_download_speed = \
271
- _download_fixed_amount_of_data (response , temp_file , required_length )
265
+ # Download the contents of the URL, up to the required length, to a
266
+ # temporary file, and get the total number of downloaded bytes.
267
+ total_downloaded , average_download_speed = \
268
+ _download_fixed_amount_of_data (response , temp_file , required_length )
272
269
273
270
# Does the total number of downloaded bytes match the required length?
274
271
_check_downloaded_length (total_downloaded , required_length ,
@@ -382,16 +379,8 @@ def _download_fixed_amount_of_data(response, temp_file, required_length):
382
379
break
383
380
384
381
except urllib3 .exceptions .ReadTimeoutError as e :
385
- # Whatever happens, make sure that we always close the connection.
386
- response .close ()
387
382
raise tuf .exceptions .SlowRetrievalError (str (e ))
388
383
389
- except :
390
- # Whatever happens, make sure that we always close the connection.
391
- response .close ()
392
- raise
393
-
394
- response .close ()
395
384
return number_of_bytes_received , average_download_speed
396
385
397
386
0 commit comments