diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2014-05-17 20:02:30 -0400 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2014-05-17 20:02:30 -0400 |
commit | 1ca3a09ad16d07cf09655472f0b328b608a8aef8 (patch) | |
tree | bac74e2fc90fee33103d39c6df3e33bf51bd65e2 /ez_setup.py | |
parent | 59003e44f1c75df5ebd6d8fd5cb065385f98affc (diff) | |
download | python-setuptools-bitbucket-1ca3a09ad16d07cf09655472f0b328b608a8aef8.tar.gz |
Use context managers to reliably close files
Diffstat (limited to 'ez_setup.py')
-rw-r--r-- | ez_setup.py | 32 |
1 files changed, 10 insertions, 22 deletions
diff --git a/ez_setup.py b/ez_setup.py index 732fffc9..a05069b2 100644 --- a/ez_setup.py +++ b/ez_setup.py @@ -183,14 +183,11 @@ def has_powershell(): if platform.system() != 'Windows': return False cmd = ['powershell', '-Command', 'echo test'] - devnull = open(os.path.devnull, 'wb') - try: + with open(os.path.devnull, 'wb') as devnull: try: subprocess.check_call(cmd, stdout=devnull, stderr=devnull) except Exception: return False - finally: - devnull.close() return True download_file_powershell.viable = has_powershell @@ -201,14 +198,11 @@ def download_file_curl(url, target): def has_curl(): cmd = ['curl', '--version'] - devnull = open(os.path.devnull, 'wb') - try: + with open(os.path.devnull, 'wb') as devnull: try: subprocess.check_call(cmd, stdout=devnull, stderr=devnull) except Exception: return False - finally: - devnull.close() return True download_file_curl.viable = has_curl @@ -219,14 +213,11 @@ def download_file_wget(url, target): def has_wget(): cmd = ['wget', '--version'] - devnull = open(os.path.devnull, 'wb') - try: + with open(os.path.devnull, 'wb') as devnull: try: subprocess.check_call(cmd, stdout=devnull, stderr=devnull) except Exception: return False - finally: - devnull.close() return True download_file_wget.viable = has_wget @@ -240,19 +231,16 @@ def download_file_insecure(url, target): from urllib.request import urlopen except ImportError: from urllib2 import urlopen - src = dst = None + src = urlopen(url) try: - src = urlopen(url) - # Read/write all in one block, so we don't create a corrupt file - # if the download is interrupted. + # Read all the data in one block. data = src.read() - dst = open(target, "wb") - dst.write(data) finally: - if src: - src.close() - if dst: - dst.close() + src.close() + + # Write all the data in one block to avoid creating a partial file. + with open(target, "wb") as dst: + dst.write(data) download_file_insecure.viable = lambda: True |