Commit 6c97510b authored by Jason Madden's avatar Jason Madden

Close various files when finished writing to them. This avoids...

Close various files when finished writing to them. This avoids ResourceWarnings on Python 3, and better supports doctests under PyPy.
parent dac64121
......@@ -10,6 +10,9 @@ Unreleased
- Add ``BUILDOUT_HOME`` as an alternate way to control how the user default
configuration is found.
- Close various files when finished writing to them. This avoids
ResourceWarnings on Python 3, and better supports doctests under PyPy.
2.2.1 (2013-09-05)
==================
......
......@@ -868,7 +868,8 @@ def develop(setup, dest,
os.rename(setup_cfg+'-develop-aside', setup_cfg)
undo.append(restore_old_setup)
else:
open(setup_cfg, 'w')
f = open(setup_cfg, 'w')
f.close()
undo.append(lambda: os.remove(setup_cfg))
setuptools.command.setopt.edit_config(
setup_cfg, dict(build_ext=build_ext))
......@@ -1134,7 +1135,10 @@ def _create_script(contents, dest):
generated = []
script = dest
changed = not (os.path.exists(dest) and open(dest).read() == contents)
changed = True
if os.path.exists(dest):
with open(dest) as f:
changed = f.read() != contents
if is_win32:
# generate exe file and give the script a magic name:
......@@ -1147,15 +1151,18 @@ def _create_script(contents, dest):
except AttributeError:
# fall back for compatibility with older Distribute versions
new_data = pkg_resources.resource_string('setuptools', 'cli.exe')
if (not os.path.exists(win32_exe) or
(open(win32_exe, 'rb').read() != new_data)
):
win32_changed = True
if os.path.exists(win32_exe):
with open(win32_exe, 'rb') as f:
win32_changed = f.reod() != new_data
if win32_changed:
# Only write it if it's different.
open(win32_exe, 'wb').write(new_data)
generated.append(win32_exe)
if changed:
open(dest, 'w').write(contents)
with open(dest, 'w') as f:
f.write(contents)
logger.info(
"Generated script %r.",
# Normalize for windows
......@@ -1218,18 +1225,23 @@ def _pyscript(path, dest, rsetup, initialization=''):
relative_paths_setup = rsetup,
initialization=initialization,
)
changed = not (os.path.exists(dest) and open(dest).read() == contents)
changed = True
if os.path.exists(dest):
with open(dest) as f:
changed = f.read() != contents
if is_win32:
# generate exe file and give the script a magic name:
exe = script + '.exe'
open(exe, 'wb').write(
with open(exe, 'wb') as f:
f.write(
pkg_resources.resource_string('setuptools', 'cli.exe')
)
generated.append(exe)
if changed:
open(dest, 'w').write(contents)
with open(dest, 'w') as f:
f.write(contents)
try:
os.chmod(dest, _execute_permission())
except (AttributeError, os.error):
......
......@@ -56,7 +56,8 @@ def cat(dir, *names):
and os.path.exists(path+'-script.py')
):
path = path+'-script.py'
print_(open(path).read(), end='')
with open(path) as f:
print_(f.read(), end='')
def ls(dir, *subs):
if subs:
......@@ -240,9 +241,8 @@ def buildoutSetUp(test):
os.chdir(sample)
# Create a basic buildout.cfg to avoid a warning from buildout:
open('buildout.cfg', 'w').write(
"[buildout]\nparts =\n"
)
with open('buildout.cfg', 'w') as f:
f.write("[buildout]\nparts =\n")
# Use the buildout bootstrap command to create a buildout
zc.buildout.buildout.Buildout(
......@@ -375,7 +375,8 @@ class Handler(BaseHTTPRequestHandler):
self.send_header('Content-Length', str(len(out)))
self.send_header('Content-Type', 'text/html')
else:
out = open(path, 'rb').read()
with open(path, 'rb') as f:
out = f.read()
self.send_header('Content-Length', len(out))
if path.endswith('.egg'):
self.send_header('Content-Type', 'application/zip')
......@@ -471,8 +472,8 @@ def install(project, destination):
shutil.copyfile(dist.location, destination)
else:
# copy link
open(os.path.join(destination, project+'.egg-link'), 'w'
).write(dist.location)
with open(os.path.join(destination, project+'.egg-link'), 'w') as f:
f.write(dist.location)
def install_develop(project, destination):
if not isinstance(destination, str):
......@@ -481,8 +482,8 @@ def install_develop(project, destination):
dist = pkg_resources.working_set.find(
pkg_resources.Requirement.parse(project))
open(os.path.join(destination, project+'.egg-link'), 'w'
).write(dist.location)
with open(os.path.join(destination, project+'.egg-link'), 'w') as f:
f.write(dist.location)
def _normalize_path(match):
path = match.group(1)
......
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