diff options
author | Elliott Sales de Andrade <quantum.analyst@gmail.com> | 2016-08-24 21:39:05 -0400 |
---|---|---|
committer | Elliott Sales de Andrade <quantum.analyst@gmail.com> | 2016-08-27 18:29:56 -0400 |
commit | 181d9a891d6791125946abe3ff630fba3a34ebbe (patch) | |
tree | 18274198688c21c9963d6e9878e8cea92f1dea72 | |
parent | fe0aa7daff3395f3036d636d9d2d1baef2feb104 (diff) | |
download | meson-181d9a891d6791125946abe3ff630fba3a34ebbe.zip meson-181d9a891d6791125946abe3ff630fba3a34ebbe.tar.gz meson-181d9a891d6791125946abe3ff630fba3a34ebbe.tar.bz2 |
Ensure URLs are closed with a context manager.
-rw-r--r-- | mesonbuild/wrap/wrap.py | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/mesonbuild/wrap/wrap.py b/mesonbuild/wrap/wrap.py index 33f52f3..f03af67 100644 --- a/mesonbuild/wrap/wrap.py +++ b/mesonbuild/wrap/wrap.py @@ -13,6 +13,7 @@ # limitations under the License. from .. import mlog +import contextlib import urllib.request, os, hashlib, shutil import subprocess import sys @@ -137,26 +138,26 @@ class Resolver: resp = open_wrapdburl(url) else: resp = urllib.request.urlopen(url) - dlsize = int(resp.info()['Content-Length']) - print('Download size:', dlsize) - print('Downloading: ', end='') - sys.stdout.flush() - printed_dots = 0 - blocks = [] - downloaded = 0 - while True: - block = resp.read(blocksize) - if block == b'': - break - downloaded += len(block) - blocks.append(block) - ratio = int(downloaded/dlsize * 10) - while printed_dots < ratio: - print('.', end='') - sys.stdout.flush() - printed_dots += 1 - print('') - resp.close() + with contextlib.closing(resp) as resp: + dlsize = int(resp.info()['Content-Length']) + print('Download size:', dlsize) + print('Downloading: ', end='') + sys.stdout.flush() + printed_dots = 0 + blocks = [] + downloaded = 0 + while True: + block = resp.read(blocksize) + if block == b'': + break + downloaded += len(block) + blocks.append(block) + ratio = int(downloaded/dlsize * 10) + while printed_dots < ratio: + print('.', end='') + sys.stdout.flush() + printed_dots += 1 + print('') return b''.join(blocks) def get_hash(self, data): |