summaryrefslogtreecommitdiff
path: root/lorry
diff options
context:
space:
mode:
authorKyle Mckay <kyle.mckay@codethink.co.uk>2021-11-04 09:44:55 +0000
committerKyle Mckay <kyle.mckay@codethink.co.uk>2021-11-16 11:17:05 +0000
commit7d85bd957e72e2669e2b20364275dc248df26328 (patch)
treee057e0ceb2689b362bbc4d69bf9fc4927364322b /lorry
parent36c5fb0d66737274565035c0a7a1791801110c2d (diff)
downloadlorry-7d85bd957e72e2669e2b20364275dc248df26328.tar.gz
Use context manager for raw file requests
- Automaitcally clean up when done - Improve code readability - Use of parentheses around a with statement is a Python 3.10 change (though supported in 3.9)
Diffstat (limited to 'lorry')
-rwxr-xr-xlorry5
1 files changed, 2 insertions, 3 deletions
diff --git a/lorry b/lorry
index b38c2ee..cadc0f2 100755
--- a/lorry
+++ b/lorry
@@ -808,8 +808,8 @@ class Lorry(cliapp.Application):
new_files[src] = file_dest
self.progress('.. attempting to fetch %s' % basename)
try:
- with open(file_dest, 'wb') as raw_file:
- urlfile = urllib.request.urlopen(url)
+ with open(file_dest, 'wb') as raw_file, \
+ urllib.request.urlopen(url) as urlfile:
raw_file.write(urlfile.read())
try:
# HTTP dates use (one of) the email date formats
@@ -818,7 +818,6 @@ class Lorry(cliapp.Application):
urlfile.info()['Last-Modified']))
except (KeyError, ValueError, TypeError):
url_date = None
- urlfile.close()
if url_date:
os.utime(file_dest, (url_date, url_date))
except Exception: