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 | 02abac6610d70781ee9be1a1f10377b7b04bfd0a (patch) | |
| tree | 3429b35fca3b9d5c1431fbbb23aeee121f4e3278 | |
| parent | 95ca77339cdcd24fc1193b701c979e5f6fa72335 (diff) | |
| download | python-setuptools-git-02abac6610d70781ee9be1a1f10377b7b04bfd0a.tar.gz | |
Use context managers to reliably close files
| -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 |
