Commit f9f94d71 authored by Jason R. Coombs's avatar Jason R. Coombs

Use context manager

--HG--
extra : amend_source : 5e98bee2918d9eeb073c8c896a849c5f68da6634
parent 032cd636
...@@ -632,7 +632,7 @@ class PackageIndex(Environment): ...@@ -632,7 +632,7 @@ class PackageIndex(Environment):
shutil.copy2(filename, dst) shutil.copy2(filename, dst)
filename=dst filename=dst
file = open(os.path.join(tmpdir, 'setup.py'), 'w') with open(os.path.join(tmpdir, 'setup.py'), 'w') as file:
file.write( file.write(
"from setuptools import setup\n" "from setuptools import setup\n"
"setup(name=%r, version=%r, py_modules=[%r])\n" "setup(name=%r, version=%r, py_modules=[%r])\n"
...@@ -641,7 +641,6 @@ class PackageIndex(Environment): ...@@ -641,7 +641,6 @@ class PackageIndex(Environment):
os.path.splitext(basename)[0] os.path.splitext(basename)[0]
) )
) )
file.close()
return filename return filename
elif match: elif match:
...@@ -660,7 +659,7 @@ class PackageIndex(Environment): ...@@ -660,7 +659,7 @@ class PackageIndex(Environment):
def _download_to(self, url, filename): def _download_to(self, url, filename):
self.info("Downloading %s", url) self.info("Downloading %s", url)
# Download the file # Download the file
fp, tfp, info = None, None, None fp, info = None, None
try: try:
checker = HashChecker.from_url(url) checker = HashChecker.from_url(url)
fp = self.open_url(strip_fragment(url)) fp = self.open_url(strip_fragment(url))
...@@ -677,7 +676,7 @@ class PackageIndex(Environment): ...@@ -677,7 +676,7 @@ class PackageIndex(Environment):
sizes = get_all_headers(headers, 'Content-Length') sizes = get_all_headers(headers, 'Content-Length')
size = max(map(int, sizes)) size = max(map(int, sizes))
self.reporthook(url, filename, blocknum, bs, size) self.reporthook(url, filename, blocknum, bs, size)
tfp = open(filename,'wb') with open(filename,'wb') as tfp:
while True: while True:
block = fp.read(bs) block = fp.read(bs)
if block: if block:
...@@ -691,7 +690,6 @@ class PackageIndex(Environment): ...@@ -691,7 +690,6 @@ class PackageIndex(Environment):
return headers return headers
finally: finally:
if fp: fp.close() if fp: fp.close()
if tfp: tfp.close()
def reporthook(self, url, filename, blocknum, blksize, size): def reporthook(self, url, filename, blocknum, blksize, size):
pass # no-op pass # no-op
...@@ -1040,9 +1038,8 @@ def local_open(url): ...@@ -1040,9 +1038,8 @@ def local_open(url):
files = [] files = []
for f in os.listdir(filename): for f in os.listdir(filename):
if f=='index.html': if f=='index.html':
fp = open(os.path.join(filename,f),'r') with open(os.path.join(filename,f),'r') as fp:
body = fp.read() body = fp.read()
fp.close()
break break
elif os.path.isdir(os.path.join(filename,f)): elif os.path.isdir(os.path.join(filename,f)):
f+='/' f+='/'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment